. NET textbox control settings readonly=true Background No value three workarounds

Source: Internet
Author: User

. NET textbox control settings readonly=true Background No value three workarounds when the textbox is set readonly=true after the foreground for the control to add a value, the background is not taken, the value is empty, how depressing a problem after trying, The discovery can solve this problem in the following way. Interested friends can understand the nextwhen 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 "empty"The principle is not clear, it is unclear what Microsoft is considering, but sometimes we can fill the value through the foreground script, do not want the user to modify the contents of the control, it is more embarrassing.

At first it was replaced with HTML <input type= "text" readonly= "ReadOnly" >, but later found that the workload is very large, so the web search, did not find the textbox readonly= "true" The page fill value cannot be taken for the reason, but the problem is solved.
Body
For VS2005 's specification and security of page code, personal opinion is to use the second method
Know why the TextBox set the ReadOnly property, do not take the principle of value, give the following instructions, thank you!

Under. NET 2.0, when a textbox on a page sets the property readonly= "True", it is not available to access its Text property in background code after it has been assigned a value through client script. After trying, we found that we can solve this problem in the following way:

Method One: Do not set the ReadOnly property, which is simulated by Onfocus=this.blur (), as follows:
Copy CodeThe code is as follows:
<asp:textbox id= "TextBox1" runat= "Server" Onfocus=this.blur () ></asp:TextBox>
In this case, when the text box gets the focus immediately lost, so also can not manually modify its contents, can simulate ReadOnly, in the background code can also through the Text property, normal to get through the script in the client set value;

Method Two: After setting the ReadOnly property, the value is taken by request, as follows:
Front Code:
Copy CodeThe code is as follows:
<asp:textbox id= "TextBox1" runat= "Server" readonly= "True" ></asp:TextBox>
Background code:
Copy CodeThe code is as follows:
String Text = request.form["TextBox1"]. Trim ();
Method Three: The read-only property of the text box is set in Page_Load () and is not set in the foreground. Can be read normally, as follows:
Copy CodeThe code is as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
TEXTBOX1.ATTRIBUTES.ADD ("ReadOnly", "true");
}
}

. NET textbox control settings readonly=true Background No value three workarounds

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.