Solution for textbox of readonly in ASP. NET 2.0

Source: Internet
Author: User

[Source: appdev-sysk 118] Sometimes, we do not want users to edit textbox directly, but want to set it through client scripts.

The general practice is to set the textbox attribute readonly to true. But there is a change in ASP. NET 2.0, and readonly is set to true.

The textbox on the server side cannot obtain the new content set on the client through the text attribute. In reflector, compare the content of loadpostdata

Implementation

In. NET 1.1,

Bool ipostbackdatahandler. loadpostdata (string postdatakey, namevaluecollection postcollection)
{
String text1 = This. text;
String text2 = postcollection [postdatakey];
If (! Text1.equals (text2 ))
{
This. Text = text2;
Return true;
}
Return false;
}

In. NET 2.0,

Protected virtual bool loadpostdata (string postdatakey, namevaluecollection postcollection)
{
Base. validateevent (postdatakey );
String text1 = This. text;
String text2 = postcollection [postdatakey];
If (! This. readonly &&! Text1.equals (text2, stringcomparison. ordinal ))
{
This. Text = text2;
Return true;
}
Return false;
}

We can see that if readonly is set to true, the new value returned from the client is not set to the text attribute.

To maintain the behavior in. Net 1. *, we recommend that you set the client property contenteditable = false. For more information, see

Syskey 118: readonly or contenteditable?
Http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx

In fact, if you set the client properties, you can also set the readonly attribute of the client:

Textbox1.attributes ["readonly"] = "true ";

Example:

Txtcol_rr_rt_area.attributes [ " Contenteditable " ] =   " False " ;
Txtcol_rr_rt_quality.attributes [ " Contenteditable " ] =   " False " ;

  Foreach (Gridviewrow gvr In Gridview1.rows)
{
(Textbox) gvr. findcontrol ( " Txtquality " ). Attributes [ " Readonly " ] =   " True " ;
(Textbox) gvr. findcontrol ( " Txtarea " ). Attributes [ " Readonly " ] =   " True " ;
}

 

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.