您的位置:首页 > 产品设计 > UI/UE

关于TextBox属性ReadOnly=“true”时,后台无法获取js重新对TextBox的赋值问题

2018-03-16 13:54 846 查看
在我调试项目的过程中发现,TextBox的属性设为ReadOnly时,后台无法获取其回传的值:

<asp:TextBox ID="TextBox1" ReadOnly="true"  runat="server"></asp:TextBox>
$('#TextBox1').val('Test');
//最后取值时,发现无法获取
string T = this.TextBox1.Text;


查阅相关资料发现,在ASP.NET2.0中,为了提高应用程序的安全性, 对ReadOnly做了限制,以下为TextBox控件获取数据的内部方法:

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的限制。解决办法有以下三种:

1、寻找ReadOnly的替换方法,eg通过onfocus=”this.flur();”来模拟

2、前台继续设置ReadOnly属性,不过后台通过Request来取值:

string Test = Request.Form["TextBox1"];


3、不在前台设置ReadOnly属性,在Page_Load()中进行设置:

this.TextBox1.Attributes.Add("readonly", "true");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TextBox ReadOnly  .NET