When ReadOnly = "true" is set for TextBox, if a value is added to the control on the foreground, the background cannot be obtained and the value is "null"
I can't figure out the principle, but I don't know why Microsoft considers it. However, sometimes we want to fill in the value through the foreground script and do not want the user to modify its control content, this is embarrassing.
In the beginning, I switched to <input type = "text" readonly = "readonly"> in Html, but later I found that the workload was heavy, so I searched the internet, the reason why the TextBox ReadOnly = "true" Page fill value cannot be obtained is not found, but the problem is still solved.
Body:
Out of vs2005's specification and security for page code, my personal opinion is to use the second method.
If you know why the ReadOnly attribute is set in TextBox and the value principle cannot be obtained, Let's explain it. Thank you!
In. NET 2.0, when the ReadOnly = "True" attribute is set for a TextBox on the page, assign a value to it through the client script, this value cannot be obtained when you access the Text attribute in the background code. After trying, we found that the problem can be solved in the following ways:
Method 1: Do not set the ReadOnly attribute. Use onfocus = this. blur () to simulate it, as shown below:Copy codeThe Code is as follows: <asp: TextBox ID = "TextBox1" runat = "server" onfocus = this. blur ()> </asp: TextBox>
In this case, the Text box is immediately lost when it gets the focus, so it cannot be manually modified, it can simulate ReadOnly, and the Text attribute can also be used in the background code, normally obtain the value set by the script on the client;
Method 2: After the ReadOnly attribute is set, use the Request parameter as follows:
Front-end 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 3: The read-only attribute of the text box is set in Page_Load () and is not set in the foreground. You can read the data as follows:Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
TextBox1.Attributes. Add ("readonly", "true ");
}
}