Solution:
Instead of directly setting in the page, you can set it in the code.
1. Remove readonly = true from the Textbox Control;
2. Add attributes in the background code page_load:
This.txt filename. Attributes. Add ("readonly", "true ");
This.txt filename. Attributes. Add ("contenteditable", "false ");
Txtfilename is the name of the text box control.
When readonly = "true" is set for textbox on the page, values cannot be assigned to the background code. The following methods can be avoided:
1. If readonly is not set, set onfocus = This. Blur ()
C # code
- <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:
C # code
- <Asp: textbox id = "textbox1" runat = "server" readonly = "true"> </ASP: textbox>
Background code:
C # 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:
C # code
- Protected void page_load (Object sender, eventargs E)
- {
- If (! Page. ispostback)
- {
- Textbox1.attributes. Add ("readonly", "true ");
- }
- }