给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免:
1、不设置ReadOnly,设置onfocus=this.blur()
C#代码
- <asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>
[c#] view
plaincopyprint?
- <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#代码
- <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
[c#] view
plaincopyprint?
- <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
后台代码:
C#代码
- string Text = Request.Form["TextBox1"].Trim();
[c#] view
plaincopyprint?
- string Text = Request.Form["TextBox1"].Trim();
string Text = Request.Form["TextBox1"].Trim();
3、在Page_Load()正设置文本框的只读属性,能正常读取,如下:
C#代码
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- TextBox1.Attributes.Add("readonly","true");
- }
- }
时间: 2024-10-11 13:30:13