[转载]ASP.NET中TextBox控件设立ReadOnly="true"后台取不到值

原文地址:http://www.cnblogs.com/yxyht/archive/2013/03/02/2939883.html

ASP.NET中TextBox控件设置ReadOnly="true"H或Enabled=false后台取不到值

当TextBox设置了ReadOnly="true"后,要是在前台为控件添加了值,后台是取不到的,值为“空” 。

方法一:不设置ReadOnly属性,通过onfocus=this.blur()来模拟,如下:

<asp:TextBox csharp string">"TextBox1"runat="server"onfocus=this.blur()></asp:TextBox>

方法二:设置了ReadOnly属性后,通过Request来取值,如下:

前台代码:

<asp:TextBox csharp string">"TextBox1"runat="server"ReadOnly="True"></asp:TextBox>

后台代码:

stringText = Request.Form["TextBox1"].Trim();

方法三:在Page_Load()正设置文本框的只读属性,在前台不设置。就能正常读取,如下:

protectedvoidPage_Load(objectsender, EventArgs e)

{

if(!Page.IsPostBack)

{

TextBox1.Attributes.Add("readonly","true");

}

}