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");
}
}