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

SP.NET中TextBox控件设置ReadOnly="true"H或Enabled=false后台取不到值
 
当TextBox设置了ReadOnly="true" 后,要是在前台为控件添加了值,后台是取不到的,值为“空”  。
 
  
 
方法一:不设置ReadOnly属性,通过οnfοcus=this.blur()来模拟,如下:
 
<asp:TextBox ID="TextBox1" runat="server" οnfοcus=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");
        }
    }

猜你喜欢

转载自www.cnblogs.com/Jeremy2001/p/11294296.html