How to keep the Text of a Read only TextBox after PostBack()?

前端 未结 7 1111
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 07:05

I have an ASP.NET TextBox and I want it to be ReadOnly. (The user modify it using another control)

But when there is a PostBack()

相关标签:
7条回答
  • 2020-12-08 08:08

    I've had this same issue but using Knockout binding 'enable' and ASP.Net Server Control Text.

    This way:

    <asp:TextBox ID="txtCity" runat="server" required="required" class="form-control" placeholder="City" data-bind="value: city, enable: !hasZipCode()"></asp:TextBox>
    

    However, when the form was submitted this field value was always empty. This occurred, I presume, because if the control is disabled, it is not persist on the ViewState chain.

    I solved replacing bindig 'enable' by 'attr{ readonly: hasZipCode}'

        <asp:TextBox ID="txtCity" runat="server" required="required" class="form-control" placeholder="City" data-bind="attr{ value: city, readonly: hasZipCode }">/asp:TextBox>
    
    0 讨论(0)
提交回复
热议问题