After the textbox of the server control in asp.net is set as a read-only property, the value assigned to it by javascript cannot be obtained.

Source: Internet
Author: User

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
  1. <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
  1. <Asp: TextBox ID = "TextBox1" runat = "server" ReadOnly = "True"> </asp: TextBox>

Background code:

C # code
  1. 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
    1. Protected void Page_Load (object sender, EventArgs e)
    2. {
    3. If (! Page. IsPostBack)
    4. {
    5. TextBox1.Attributes. Add ("readonly", "true ");
    6. }
    7. }
    8.  

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.