. NET textbox control settings readonly=true Background No value three solutions _ Practical Tips

Source: Internet
Author: User
When the textbox is set to Readonly= "true", if a value is added to the control in the foreground, the background is not available and the value is "null"
The principle is not figured out, it is not clear what Microsoft is considering, but sometimes we can through the foreground to fill the value of the script, do not want users to modify the content of their controls, this is more embarrassing.

In the beginning is changed into HTML <input type= "text" readonly= "ReadOnly" >, but later found that the workload is very large, so the internet search, did not find the textbox readonly= "true" Page fill value is not taken, but the problem is solved.
Body
For VS2005 code and security of the page, the personal opinion is to use the second method
Know why the TextBox set the ReadOnly property, not to the value of the principle, give instructions, thank you!

Under. NET 2.0, when a textbox on a page sets the property readonly= "True", the value is not obtained by accessing its Text property in the background code after the client script assigns it a value. After a trial, it was found that the problem could be solved in the following ways:

Method One: The ReadOnly property is not set and is simulated by Onfocus=this.blur () as follows:
Copy Code code as follows:

<asp:textbox id= "TextBox1" runat= "Server" Onfocus=this.blur () ></asp:TextBox>

In this case, the text box when the focus is lost immediately, so can not manually modify its contents, can simulate ReadOnly, in the background code can also pass the Text property, the normal access to the script in the client set the value;

Method Two: After setting the ReadOnly property, the value is taken by request, as follows:
Foreground code:
Copy Code code as follows:

<asp:textbox id= "TextBox1" runat= "Server" readonly= "True" ></asp:TextBox>

Background code:
Copy Code code as follows:

String Text = request.form["TextBox1"]. Trim ();

Method Three: The read-only property of the text box is being set at Page_Load () and is not set in the foreground. will be able to read normally, as follows:
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
TEXTBOX1.ATTRIBUTES.ADD ("ReadOnly", "true");
}
}

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.