I have recently completed a project and found a small problem.
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 have no idea about the principle, but I cannot tell you 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 huge,
So I searched the internet and did not find out the reason why the textbox readonly = "true" Page filling value could not be obtained, but the problem was solved.
Too many gossip...: d
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!
Received online,
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:
1. Do not set the readonly attribute. Use onfocus = This. Blur () to simulate it, as shown below:
<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;
2. After the readonly attribute is set, use the request parameter as follows:
Front-end code:
<Asp: textbox id = "textbox1" runat = "server" readonly = "true"> </ASP: textbox>
Background code:
String text = request. Form ["textbox1"]. Trim ();
3. Set the read-only attribute of the text box in page_load () to read normally, as shown below:
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Textbox1.attributes. Add ("readonly", "true ");
}
}
The above text is transferred from http://www.cnblogs.com/wangdaye/archive/2011/03/03/1969758.html
Original Author not found