简体   繁体   中英

CustomValidations won't show up in web form

This is my control for the form:

<asp:CustomValidator ID="txtZipCode" runat="server" 
            ErrorMessage="Enter your zip code." ControlToValidate="txtZip" EnableClientServer="false" ValidateEmptyText="true"
            ForeColor="Red" ></asp:CustomValidator>

This is my method:

protected void btnContinue_Click(object sender, EventArgs e)
    {
        if (ddlState.SelectedValue == "International (No U.S. State)" && ddlCountry.SelectedValue == "United States")
        {
            CustomValidator1.IsValid = true;
        }
}

You aren't using the validator correctly.

When a form is posted back to the server, the page's Validate() function polls all validators to find out whether they are valid. To get the CustomValidator to respond to the polling, you have to intercept the ServerValidate event. That is where your logic should go to determine whether that particular validator is valid. Then, on the Click event for the button, you check to make sure the form is valid before proceeding.

Here's an example, written using your code:

protected void CustomValidator1_ServerValidate( object source, 
    ServerValidateEventArgs args )
{
    if ( ddlState.SelectedValue == "International (No U.S. State)" 
         && ddlCountry.SelectedValue == "United States" )
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

protected void btnContinue_Click( object sender, EventArgs e )
{
    if ( !Page.IsValid )
        return;

    // do whatever the continue button is supposed to do
}

Maybe you're missing an else statement. What is supposed to happen if the country is International and the Country is United States? I can't really tell what the behavior should be.

    if (ddlState.SelectedValue == "International (No U.S. State)" && ddlCountry.SelectedValue == "United States")
    {
        CustomValidator1.IsValid = true;
    }
    else
    {
        CustomValidator1.IsValid = false; 
    }

You must explicitly set them to false, the validators don't default to false.

ControlToValidate property is pointless in CustomValidator, you should specify server validation function in OnServerValidate property:

<asp:CustomValidator runat="server" 
            ErrorMessage="Enter your zip code."
            EnableClientScript="false"
            OnServerValidate="OnZipCodeValidate">
</asp:CustomValidator>

<script language="c#" runat="server">
    protected void OnZipCodeValidate(object sender, ServerValidateEventArgs args) {
      // not intending to write correct validation function, just an example
      args.IsValid = 
                     ddlState.SelectedValue == "International (No U.S. State)" || 
                     (ddlCountry.SelectedValue == "United States" &&
                     !String.IsNullOrEmpty(txtZip.Text));
    }

</script>

Also, make sure that you form's submit button has CausesValidation="true" and custom validator is in the same validation group (if there's any).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM