天天看点

ASP.Net TextBox 只读(ReadOnly)时后台不能赋值取值

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">	</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">当把ASP.Net的自带控件TextBox设置为只读时,后台无法正常取值(取到的结果为“”)。解决办法如下:</span>
           

1. 不设置ReadOnly属性,添加事件οnfοcus=this.blur()

<asp:TextBox ID="TextBox1" runat="server" οnfοcus=this.blur()></asp:TextBox> 
           

2、设置了ReadOnly属性后,通过Request来取值,如下:

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

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

protected void Page_Load(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
    {  
        TextBox1.Attributes.Add("readonly","true");  
    }  
}