的CustomValidator工作不正常 [英] CustomValidator not working well

查看:132
本文介绍了的CustomValidator工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段ASP的:

I have the following piece of asp:

<asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" 
        ValidationGroup="RegisterUserValidationGroup"/>

...

<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserNameTB">Username:</asp:Label>
<asp:TextBox ID="UserNameTB" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="UserNameTB" 
      ValidationExpression="[a-zA-Z]{3,8}" ErrorMessage="Username must be between 3 to 8 chars" runat="server"
      CssClass="failureNotification" ToolTip="Username must be between 3 to 8 chars" ValidationGroup="RegisterUserValidationGroup">
    *</asp:RegularExpressionValidator>
<asp:CustomValidator ID="NoUserValidator" ControlToValidate="UsernameTB" runat="server" ErrorMessage="User Taken!" CssClass="failureNotification" 
      ValidationGroup="RegisterUserValidationGroup"  OnServerValidate="UserValidate">*</asp:CustomValidator>

和则函数:

protected void UserValidate(object source, ServerValidateEventArgs args)
    {
        SqlDataSource1.SelectCommand = "SELECT ClientID FROM [Clients] WHERE Username= '" + UserNameTB.Text + "'";
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        if (dv.Table.Rows.Count != 0)
            args.IsValid = false;
        else
            args.IsValid = true;
    }

按钮:

<asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" 
       ValidationGroup="RegisterUserValidationGroup" 
       onclick="CreateUserButton_Click"/>

问题是,即使自定义验证函数被调用,并设置 .IsValid 为false,
按钮逻辑仍然运行!

Problem is that even though the custom validator function is called and sets .IsValid to false, the button logic still runs!

推荐答案

在该按钮的onclick的功能,增加一个检查,以查看该页面是否有效

In your onclick function for the button, add a check to see if the page is valid

protected void CreateUserButton_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    { 
        // Create the user
    }
}

这应该这样做。这是因为自定义验证设置为验证服务器上,回发期间。什么情况是,code首先运行验证code UserValidate ,在这里你将的IsValid 旗。在回发栈接下来是该按钮的onclick功能。此功能,无论结果的验证功能将运行,所以这就是你需要检查的IsValid 标志的值。这是行为,当您验证在服务器端自定义验证控件。

That should do it. This is because your custom validator is set up to validate on the server, during the postback. What happens is that the code first runs the validator code UserValidate, where you set the IsValid flag. Next in the postback stack is the button's onclick function. This function will run regardless of the result in the validator function, so this is where you need to check the value of the IsValid flag. This is the behavior when you validate the custom validation control on the server side.

另一种方法是,以验证在客户端。如果你看一下您的浏览器产生的页面源代码code,你会看到JavaScript是增加了对 RegularEx pressionValidator 。它的行为是已知的,并且在客户端的处理,所以没有回发需要评估前pression和验证页(它的所有由JavaScript处理)。自定义验证功能尚不清楚,所以除非你自己定义的客户端验证脚本回发是必需的。

An alternative is to validate on the client side. If you look at the page source code generated by your browser, you'll see that Javascript is added for the RegularExpressionValidator. It's behavior is known, and handled on the client side, so no post back is required to evaluate the expression and validate the page (it's all handled by javascript). The custom validator function is not known, so a postback is required unless you define a client-side validation script yourself.

下面是详细信息的链接<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator%28v=vs.80%29.aspx\">MSDN.

Here's a link to more information on MSDN.

这篇关于的CustomValidator工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆