asp.net
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"/>
</form>
</body>
For disabled textbox, you cannot get the modified value on the server side, if you really want to use this attribute, then use the method of hiding the form field to implement it.
The textbox for the ReadOnly property is displayed as such in the client:
The textbox for the Enabled property is displayed as such in the client:
According to the rules of the consortium: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.12
Input that is set to disabled will have the following restrictions:
• Cannot receive focus
• Will be skipped when using the TAB key
• Probably not successful.
Input that is set to ReadOnly will have the following restrictions:
• Can receive focus but cannot be modified
• You can use the TAB key to navigate
• It may be successful.
Only successful form elements are valid data and can be committed. Text entry boxes for disabled and readonly can only be modified by scripting the Value property.