asp:textbox readonly

2019-03-25 16:48发布

问题:

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?

回答1:

"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];



回答2:

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");


回答3:

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

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



回答4:

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