user1263981
user1263981

Reputation: 3157

Asp.net required validator resets on postback

I am trying to disable required field validator if checkbox is ticked through jquery.

It works fine but on page post back , validator is not disabled and i see required field message.

 <asp:CheckBox ID="chkAssociate" Text="Associate / Duplicate Email"
  runat="server" TabIndex="-1" />

    <asp:TextBox ID="txtAddress" TabIndex="4" Width="220px" TextMode="MultiLine" Rows="3" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="rfvAddress" runat="server" ControlToValidate="txtAddress" Display="Dynamic" CssClass="required" ErrorMessage="* Required" ValidationGroup="Save" SetFocusOnError="True"></asp:RequiredFieldValidator>

JQUERY

var valAddress = $("[id*=rfvAddress]");

if ($(this).is(':checked')) 
{
    $("[id*=spAddress]").css('display', 'none');
    ValidatorEnable(valAddress[0], false);

}
else 
{
    $("[id*=spAddress]").css('display', 'inline');
    ValidatorEnable(valAddress[0], true);
}     

Upvotes: 3

Views: 748

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

Your issue is in the link you mention; anything changed on the client will not be persisted on the server; you would have to interpret whether the validator is enabled when the page posts back, and set the Enabled property on the server control itself:

if (chkAssociate.Checked) {
   rfvAddress.Enabled = False;
}

This should then persist back to the client. Changing enabled status on the client and server may behave differently.

Upvotes: 2

Related Questions