Setting readonly = "true" for textbox in ASP. NET cannot obtain the value.

Source: Internet
Author: User

I have recently completed a project and found a small problem.

When readonly = "true" is set for Textbox, if a value is added to the control on the foreground, the background cannot be obtained and the value is "null"

I have no idea about the principle, but I cannot tell you why Microsoft considers it,

However, sometimes we want to fill in the value through the foreground script and do not want the user to modify its control content. This is embarrassing.

In the beginning, I switched to <input type = "text" readonly = "readonly"> in HTML, but later I found that the workload was huge,

So I searched the internet and did not find out the reason why the textbox readonly = "true" Page filling value could not be obtained, but the problem was solved.

Too many gossip...: d

Body:

Out of vs2005's specification and security for page code, my personal opinion is to use the second method.

If you know why the readonly attribute is set in textbox and the value principle cannot be obtained, Let's explain it. Thank you!

Received online,

In. NET 2.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 it, as shown below:

<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 as follows:

Front-end code:

<Asp: textbox id = "textbox1" runat = "server" readonly = "true"> </ASP: textbox>

Background 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:

Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Textbox1.attributes. Add ("readonly", "true ");
}
}

The above text is transferred from http://www.cnblogs.com/wangdaye/archive/2011/03/03/1969758.html

Original Author not found

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.