ITeye博客

ASP.Net TextBox只读时不能通过后台赋值取值

boss5518831534 · 2012-09-06 10:57 · .NET · Web前端 · Markdown

今天在开发一个asp.net的页面遇到了TextBox设置了ReadOnly属性,在js中赋值后,后台代码取不到值的情况,经过在网上查找,找到了一下几个处理办法。 收藏起来。 1、不设置ReadOnly,设置onfocus=this.blur() C#代码 <asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox> <asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox> 文本框不变灰色,但也无法手动修改内容,可以在后台通过Text属性正常赋值取值 2、设置了ReadOnly属性后,通过Request来取值,如下: 前台代码: <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox> <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox> 后台代码: string Text = Request.Form.Trim(); string Text = Request.Form.Trim(); 3、在Page_Load()正设置文本框的只读属性,能正常读取,如下: C#代码 protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox1.Attributes.Add("readonly","true");
}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。