|
在以前的ASP.NET 1.x版本中,设置为ReadOnly的TextBox控件在客户端更改了值后,在服务器端仍然可以得到修改后的值,但在ASP.NET 2.0中,这种做法已经限制。这是为了提高应用程序安全性所考虑的。下面就是TextBox控件获得数据的内部方法,由此可以看出ReadOnly的限制:
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; }
这里限制的只是Text属性,而没有限制提交数据的名称/值的NameValueCollection,因此,通过Request["表单名称"]的方法仍然可以得到值的。下面的例子充分说明了这一点,并且提供了既使用ReadOnly,又可以通过Text属性获得值的方法:
<%@ Page Language="C#" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { Response.Write("<li>TextBox1 = " + TextBox1.Text); Response.Write("<li>TextBox2 = " + TextBox2.Text); Response.Write("<li>TextBox3 = " + TextBox3.Text); Response.Write("<li>Request.Form[TextBox1] = " + Request.Form[TextBox1.UniqueID]); Response.Write("<li>Request.Form[TextBox2] = " + Request.Form[TextBox2.UniqueID]); Response.Write("<li>Request.Form[TextBox3] = " + Request.Form[TextBox3.UniqueID]); }
protected void Page_Load(object sender, EventArgs e) { TextBox3.Attributes.Add("readonly", "readonly"); } </script>
<script type="text/javascript"> //<![CDATA[ function SetNewValue() { document.getElementById('<%=TextBox1.ClientID %>').value = "TextBox1 new Value"; document.getElementById('<%=TextBox2.ClientID %>').value [1] [2] 下一页 |