"Guide" This article describes the readonly limitations of the TextBox control's internal methods of obtaining data, illustrates this with an example, and provides a way to use both ReadOnly and the Text property to obtain a value.
In the previous ASP.net 1.x version, the TextBox control set to ReadOnly could still get the modified value on the server side after the client changed the value, but in asp.net 2.0, this practice was limited. This is considered to improve application security. The following is an internal method for the TextBox control to get the data, which can be seen in ReadOnly limitations:
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;
}
This restricts only the Text property, but does not limit the NameValueCollection of the name/value of the submitted data, so the method of request["form name" can still get the value. This is illustrated in the following example, and provides a way to obtain a value using both ReadOnly and the Text property:
<%@ 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 = "TextBox2 new value";
document.getElementById (' <%=textbox3.clientid%> '). Value = "TextBox3 new value";
}
]]>
</script>
<title> asp.net 2.0 textbox controls with readonly and enabled Properties </title>
<body>
<form id= "Form1" runat= "Server"
<span> TextBox1 ReadOnly: </span>
<asp:textbox id= "TextBox1" runat= "Server" readonly= "True" text= "TextBox1 old Value" > </asp:TextBox> <BR/>
<span> TextBox2 Enabled: </span>
<asp:textbox id= "TextBox2" runat= "Server" enabled= "False" text= "TextBox2 old Value" > </asp:TextBox> <BR/>
<span> TextBox3 ReadOnly: </span>
<asp:textbox id= "TextBox3" runat= "Server" text= "TextBox3 old Value" > </asp:TextBox> <BR/>
<BR/>
<asp:button id= "Button2" runat= "Server" text= "modifies the new value" onclientclick= "Setnewvalue (); return false;"/"
<asp:button id= "Button1" runat= "Server" text= "Submit" onclick= "Button1_Click"/>
</form>
</body>