In asp file I have two asp:textbox
<asp:TextBox ID="textValue" runat="server" Width="100px"/>
<asp:TextBox ID="textValue2" runat="server" Width="100px" ReadOnly="true"/>
then I set the value via javascript getting
<asp:TextBox ID="textValue" runat="server" Width="100px" value="aaa"/>
<asp:TextBox ID="textValue2" runat="server" Width="100px" ReadOnly="true" value="bbb"/>
but when refresh the webpage finally get
<asp:TextBox ID="textValue" runat="server" Width="100px" value="aaa"/>
<asp:TextBox ID="textValue2" runat="server" Width="100px" ReadOnly="true"/>
Why the value bbb is "lost"? How can I avoid this?
Enabled TextBox and ReadOnly TextBox ASP.Net TextBox control has similar two properties “Enabled” and “ReadOnly”. Both properties used to prevent a user from entering a value in textbox. The Enabled and ReadOnly make textbox field non-editable mode.
Add("readonly", "readonly"); From MSDN, The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box.
Definition and UsageThe readonly attribute is a boolean attribute. When present, it specifies that an input field is read-only. A read-only input field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).
Use the ReadOnly property to specify whether the contents of the TextBox control can be changed. Setting this property to true will prevent users from entering a value or changing the existing value. Note that the user of the TextBox control cannot change this property; only the developer can.
<asp:TextBox ID="textValue2" runat="server" Width="100px" ReadOnly="true"/>
You are missing a quotation mark before 'true', it might cause issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With