具有ValidationGroup的RequiredFieldValidator无法验证 [英] RequiredFieldValidator with ValidationGroup doesn't validate

查看:113
本文介绍了具有ValidationGroup的RequiredFieldValidator无法验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的标记:

Name: 
<asp:TextBox ID="txtNewName" runat="server" ValidationGroup="NewDepartmentValidationGroup" />
<asp:RequiredFieldValidator ID="vldtxtNewName" runat="server" ControlToValidate="txtNewName"
    ErrorMessage="Required Field" /><br />
Description: 
<asp:TextBox ID="txtNewDescription" runat="server"          
    ValidationGroup="NewDepartmentValidationGroup"/>
<asp:RequiredFieldValidator ID="vldtxtNewDescription" runat="server" 
    ControlToValidate="txtNewDescription" ErrorMessage="Required Field" /><br />
<asp:Button ID="cmdCreate" runat="server" Text="Create"
     ValidationGroup="NewDepartmentValidationGroup" OnClick="cmdCreate_Click" />

当我删除ValidationGroup属性时,其行为符合预期,并且客户端代码警告该字段为必填字段.

When I remove ValidationGroup attribute, the behavior is as expected and the client side code warns that the field is required.

但是当我指定ValidationGroup(如上面的示例中所述)并单击文本框为空的按钮时,客户端代码将不执行任何操作,按钮单击事件将触发,并且Page.IsValid等于true,并且我的代码继续进行,与预期相反.

But when I specify ValidationGroup (as noted in the example above) and I click the button with the text boxes empty, the client side code does nothing, the button click event fires and the Page.IsValid equals true and my code proceeds, contrary to what is expected.

有什么办法解决这个问题吗?

Any ideas how to fix this?

推荐答案

您在验证器上缺少验证组.

You are missing Validation Group on the Validators.

无需在控件(文本框)上指定验证组,而在验证器上以及要在其上发布有效数据的按钮上指定验证组!

There's no need to specify validation group on the controls(textboxes) instead specify the validation group on the validators and the button on which you want the valid data to be posted!

尝试一下:

    Name: 
<asp:TextBox ID="txtNewName" runat="server" />
<asp:RequiredFieldValidator ID="vldtxtNewName" runat="server"
     ControlToValidate="txtNewName" ValidationGroup="NewDepartmentValidationGroup" 
     ErrorMessage="Required Field" /><br />
Description: 
<asp:TextBox ID="txtNewDescription" runat="server" />
<asp:RequiredFieldValidator ID="vldtxtNewDescription" runat="server" 
    ControlToValidate="txtNewDescription" ErrorMessage="Required Field"
    ValidationGroup="NewDepartmentValidationGroup" /><br />
<asp:Button ID="cmdCreate" runat="server" Text="Create"
     ValidationGroup="NewDepartmentValidationGroup" OnClick="cmdCreate_Click" />

这篇关于具有ValidationGroup的RequiredFieldValidator无法验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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