asp.net中的服务器端控件 textbox 设为只读属性后无法获取 javascript给其赋的值

给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免: 

1、不设置ReadOnly,设置onfocus=this.blur() 

C#代码 
  1. <asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>  



文本框不变灰色,但也无法手动修改内容,可以在后台通过Text属性正常赋值取值 

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

前台代码: 

C#代码 
  1. <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>  



后台代码: 

C#代码 
  1. string Text = Request.Form["TextBox1"].Trim();  



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

C#代码 
    1. protected void Page_Load(object sender, EventArgs e)  
    2. {  
    3.     if (!Page.IsPostBack)  
    4.     {  
    5.         TextBox1.Attributes.Add("readonly","true");  
    6.     }  
    7. }  
    8.  
原文地址:https://www.cnblogs.com/jes_shaw/p/2610671.html