Readonly is read-only, that is, the input value is not accepted by textbox. Of course, it is null.
Alternatively, you can assign a value to textbox in other ways, which is the same as the normal value.
[Source: appdev-sysk 118] Sometimes, we do not want users to edit textbox directly, but want to set content through client scripts, the general practice is to set the textbox attribute readonly to true. But in ASP. NET 2.0 has changed and readonly is set to true textbox. on the server side, the new content set on the client cannot be obtained through the text attribute, and the implementation of loadpostdata is compared in reflector.
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 ";
Syskey 118: readonly or contenteditable?
Consider this: You want a text box on a web page to be not editable by the user, but you want to be able to change the text box's contents in client side script and see the updated text on the server.
Did you know that if you set textbox1.readonly = true, the value set by the client side script will not be visible on the server? Try it for yourself... Here is the code:
initial text
Public partial class myform: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Textbox1.readonly = true;
}
Protected void button#click (Object sender, eventargs E)
{
Response. Write (textbox1.text + "<br> ");
}
}
However, if instead of setting textbox1.readonly property you set contenteditable attribute to false, you'll get the behavior you're looking:
Textbox1.attributes ["contenteditable"] = "false ";