When ASP. NET textbox is read-only, it cannot be assigned a value through the background.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.