ASP.NET中TextBox控件ReadOnly属性设置为True,后台取值为空解决办法

来源:互联网 发布:公司出纳遭遇网络诈骗 编辑:程序博客网 时间:2024/05/07 12:43
ASP.NET中TextBox控件设置ReadOnly="true"H或Enabled=false后台取不到值
当TextBox设置了ReadOnly="true" 后,要是在前台为控件添加了值,后台是取不到的,值为“空” 。

方法一:不设置ReadOnly属性,通过onfocus=this.blur()来模拟,如下:
 
<asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>
 
方法二:设置了ReadOnly属性后,通过Request来取值,如下:
 
前台代码:
 
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
 
 后台代码:
 
string Text = Request.Form["TextBox1"].Trim();
 
  
 方法三:在Page_Load()正设置文本框的只读属性,在前台不设置。就能正常读取,如下:
 
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TextBox1.Attributes.Add("readonly","true");
        }
    }
0 0