The value of TextBox in asp.net after being read-only

Source: Internet
Author: User

In. in NET2.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

<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 to set the value.

Front-end code:

<Asp: TextBox ID = "TextBox1" runat = "server" ReadOnly = "True"> </asp: TextBox>

Background code:

String Text = Request. Form ["TextBox1"]. Trim ();

 

It is worth noting that:

We know that html is based on the control name, and the value in J2EE is like this. If nested, such as the master page and the Gridview. Then we must first obtain the uniqueID.

String uid = this.txt Birth. UniqueID;
String value = Request. Form [uid]. Trim ();

See ID, ClientID, UniqueID difference, read this article: http://www.bkjia.com/kf/201202/119612.html

 

 

3. the read-only attribute of the text box is set in Page_Load (), and can be read normally.

Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
TextBox1.Attributes. Add ("readonly", "true ");
}

}

 

From Xu Yue's column

Related Article

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.