14

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?

2
  • 1
    how can u set value to read only textbox Commented May 3, 2011 at 13:30
  • 1
    With javascript. A tipical example is a Calendar + Input text. Commented May 3, 2011 at 13:49

4 Answers 4

17

Remove the server side attribute - ReadOnly - from the TextBox and set the HTML attribute from the code. You will be able to access the value then in post back:

textValue2.Attributes.Add("readonly","readonly"); 
Sign up to request clarification or add additional context in comments.

Comments

16

"bbb" is being posted back, but .NET will not populate a read-only textbox from postback data. You can manually populate the text box by grabbing the form data yourself from the Page_Load() method as follows:

textValue2.Text = Request.Form[textValue2.UniqueID];

Comments

8
<asp:TextBox ID="textValue2" runat="server" Width="100px" ReadOnly="true"/> 

You are missing a quotation mark before 'true', it might cause issues.

Comments

0

I think instead of readonly, set your value to the textbox and then disable it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.