asp:textbox readonly

2019-03-25 17:03发布

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?

4条回答
Lonely孤独者°
2楼-- · 2019-03-25 17:13

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

查看更多
一夜七次
3楼-- · 2019-03-25 17:19
<asp:TextBox ID="textValue2" runat="server" Width="100px" ReadOnly="true"/>

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

查看更多
劫难
4楼-- · 2019-03-25 17:26

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");
查看更多
贪生不怕死
5楼-- · 2019-03-25 17:26

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

查看更多
登录 后发表回答