简体   繁体   English

CustomValidations将不会以网络形式显示

[英]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. 将表单发布回服务器后,页面的Validate()函数将轮询所有验证器,以查明它们是否有效。 To get the CustomValidator to respond to the polling, you have to intercept the ServerValidate event. 若要使CustomValidator响应轮询,您必须截获ServerValidate事件。 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. 然后,在按钮的Click事件上,在继续操作之前检查以确保表单有效。

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. 也许您错过了else语句。 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. 您必须将它们显式设置为false,验证器不会默认为false。

ControlToValidate property is pointless in CustomValidator, you should specify server validation function in OnServerValidate property: ControlToValidate属性在CustomValidator中没有意义,您应该在OnServerValidate属性中指定服务器验证功能:

<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). 另外,请确保表单的“提交”按钮具有CausesValidation="true"并且自定义验证器位于同一验证组中(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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