asp.net TextBox只读时不能通过后台赋值取值解决办法


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?给页面的TextBox设置ReadOnly=”True”时,在后台代码中不能赋值取值,下边几种方法可以避免:

1、不设置ReadOnly,设置onfocus=this.blur()

C#代码 复制代码
  1. <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来取值,如下:

前台代码:

C#代码 复制代码
  1. <asp:TextBox?ID=”TextBox1″?runat=”server”?ReadOnly=”True”?></asp:TextBox>??
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>

后台代码:

C#代码 复制代码
  1. string?Text?=?Request.Form[“TextBox1”].Trim();??
string Text = Request.Form["TextBox1"].Trim();

3、在Page_Load()正设置文本框的只读属性,能正常读取,如下:

C#代码 复制代码
  1. protected?void?Page_Load(object?sender,?EventArgs?e) ??
  2. { ??
  3. ????if?(!Page.IsPostBack) ??
  4. ????{ ??
  5. ????????TextBox1.Attributes.Add(“readonly”,”true”); ??
  6. ????} ??
  7. }?