ReadOnly and Disabled properties for TextBox server controls in asp.net 2.0

Source: Internet
Author: User
Ado|asp.net| Server | control

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= "Modify 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:

<input readonly = "ReadOnly" >

The textbox for the Enabled property is displayed as such in the client:

<input disabled= "Disabled" >

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
is skipped when you use 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 could 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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.