js无法获取只读属性服务器端控件TextBox的值

解决办法:

不直接在页面中设置而改为在代码中设置。

1、把textbox控件中的readonly=true去掉;

2、在后台代码Page_Load中添加属性:

    this.txtfilename.Attributes.Add("readonly", "true");
    this.txtfilename.Attributes.Add("contenteditable", "false");

其中txtfilename为文本框控件的名。 

给页面的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. }                                                    
posted @ 2011-11-28 11:41  星月磊子  阅读(288)  评论(0编辑  收藏  举报