Custom Validator firing but it does not update the ValidationSummary

19,173

You seem to be missing ControlToValidate in your declaration of CustomValidator.

EDIT

If your CustomValidator aggregates multiple controls, then try this:

ASPX

<asp:TextBox ID="txtMyTextBox" runat="server" />
<asp:CustomValidator ID="AddressVerification" runat="server"
    Display="Dynamic"
    ErrorMessage="Please enter a valid address."
    OnServerValidate="AddressVerification_ServerValidate"
    Text="*"
    ValidationGroup="Step2" />
<asp:RequiredFieldValidator ID="rfvAddress" runat="server"
    ControlToValidate="txtMyTextBox"
    Display="Dynamic"
    ErrorMessage="Please enter an address"
    Text="*"
    ValidationGroup="Step2" />
...
<asp:ValidationSummary ID="ValidatorSummary" runat="server"
    ValidationGroup="Step2" />
...
<asp:Button ID="btnCheckAddresses" runat="server"
    CausesValidation="true"
    Text="Check Addresses"
    ValidationGroup="Step2" />

CS

protected void AddressVerification_ServerValidate(object source, ServerValidateEventArgs args) {
    args.IsValid = !string.IsNullOrEmpty(txtMyTextBox.Text) && !txtMyTextBox.Text.Contains(' ');
}

Note that the validation group of the control invoking the post back has CausesValidation="true" and has the same ValidationGroup as the validators.

EDIT 2

If your postback control was in the UpdatePanel but the ValidationSummary was not, then the partial postback would not have refreshed the ValidationSummary. Once you removed the postback control from the UpdatePanel, I imagine it would then generate a full postback, which would refresh your ValidationSummary.

I don't know what else is in your UpdatePanel, but many people report having issues with their validators being in UpdatePanel's.

Check out MSDN,

When you use the ValidationSummary control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

as well as this MSDN blog.

Share:
19,173
David
Author by

David

Updated on June 27, 2022

Comments

  • David
    David almost 2 years

    Hi I am working on a custom form field validator, it seems like the custom validator is working by not allowing it to continue to the next page, but it doesn't update the Validation Summary nor does it display the asterisk and the labels that i've made visable. I also have other validators like RequiredFieldValidator on the same field. My ValidationGroup is set, as is the Text and IsValid. I even wrote and set a dummy client side validation method in javascript as some workarounds suggests.

    here is the validation summary code in asp.net

    <asp:ValidationSummary ID="ValidatorSummary" runat="server" ValidationGroup="Step2" />
    

    here is the custom validator and the required field one

    <asp:CustomValidator ID="AddressVerification" runat="server" ErrorMessage="Please enter a valid address." Display="Dynamic" ValidationGroup="Step2" OnServerValidate="AddressVerification_ServerValidate" ClientValidationFunction="CustomValidatorDummy" Text="*" Enabled="true" EnableClientScript="true"></asp:CustomValidator>
    <asp:RequiredFieldValidator ID="RFValidatorHomeAddress" runat="server" ErrorMessage="Please enter home address." Text="*" Display="Dynamic" ValidationGroup="Step2" ControlToValidate="txtHomeAddress"></asp:RequiredFieldValidator>
    

    here is the custom validation method in the code behind

    protected void AddressVerification_ServerValidate(object sender, ServerValidateEventArgs e)
    {
    //lets just say it doesn't validate and sets the IsValid to false
    lblUspsValidatorResHomeCity.Visible = true;
    lblUspsValidatorResHomeState.Visible = true;
    lblUspsValidatorResHomeZip.Visible = true;
    e.IsValid = false;
    }
    

    please advise, thanks.

    EDIT: Answered - as bitxwise mentioned. the validation summary should be placed inside an update panel as well. Thanks!

    Like so:

    <asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="false" UpdateMode="Conditional"
    runat="server">
    <ContentTemplate>
        <asp:ValidationSummary ID="AddressHomeValidationSummary" runat="server" ValidationGroup="AddressHomeValidationGroup"
            CssClass="errors" /> 
    </ContentTemplate>
    

    and then calling the update:

    UpdatePanelValidationSummaryHome.Update();