textBox的readonly=true

  有时候,我们不希望用户直接编辑TextBox,而是希望通过客户端脚本的方式来设置内容,一般的做法是设置TextBox的属性ReadOnly为true。但在ASP.NET   2.0里有了变化,设置了ReadOnly为true的TextBox,在服务器端不能通过Text属性获取在客户端设置的新内容,在Reflector里比较一下LoadPostData的实现  
   
  .NET   1.1中,  
   
  bool   IPostBackDataHandler.LoadPostData(string   postDataKey,   NameValueCollection   postCollection)  
  {  
              string   text1   =   this.Text;  
              string   text2   =   postCollection[postDataKey];  
              if   (!text1.Equals(text2))  
              {  
                          this.Text   =   text2;  
                          return   true;  
              }  
              return   false;  
  }  
   
  .NET   2.0中,  
   
  protected   virtual   bool   LoadPostData(string   postDataKey,   NameValueCollection   postCollection)  
  {  
              base.ValidateEvent(postDataKey);  
              string   text1   =   this.Text;  
              string   text2   =   postCollection[postDataKey];  
              if   (!this.ReadOnly   &&   !text1.Equals(text2,   StringComparison.Ordinal))  
              {  
                          this.Text   =   text2;  
                          return   true;  
              }  
              return   false;  
  }  
   
  就可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。  
   
  想要保持.NET   1.*中的行为,建议的做法是设置客户端属性ContentEditable=false,参考  
   
  SYSK   118:   ReadOnly   or   ContentEditable?  
  http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx  
   
   
  其实如果是设置客户端属性的话,设置客户端的readonly属性应该也是可以的:  
   
  TextBox1.Attributes["readonly"]   =   "true";  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值