当我们不想让页面上TextBox编辑时,一般会设置TextBox的ReadOnly属性,可是有时候会出现一种情况,当我们在客户端中使用Javascript给TextBox赋值时,在服务器端获取不到TextBox的值,
造成这一问题的原因是Net Framework2.0对TextBox的改变,分析一下Net Framew2.0的源代码就可以得知:
.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属性,我们可以在客户端设置它的属性。
TextBox1.Attributes["contentEditable"] = "false";
TextBox1.Attributes["readonly"] = "false";