Today, on the development of an Asp.net page, I encountered a situation where the textbox sets the readonly attribute. After a value is assigned in JS, the background Code cannot get the value. After searching online, I found several solutions.
Add to favorites.
1. If readonly is not set, set onfocus = This. Blur ()
C # code
<Asp: textbox id = "textbox1" runat = "server" onfocus = This. Blur ()> </ASP: textbox>
<Asp: textbox id = "textbox1" runat = "server" onfocus = This. Blur ()> </ASP: textbox>
The text box remains gray, but the content cannot be manually modified. You can assign values through the text attribute in the background.
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>
<Asp: textbox id = "textbox1" runat = "server" readonly = "true"> </ASP: textbox>
Background code:
String text = request. Form ["textbox1"]. Trim ();
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:
C # code
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Textbox1.attributes. Add ("readonly", "true ");
}
}