被指定的ValidationGroup时的ValidationSummary不工作 [英] ValidationSummary not working when ValidationGroup is specified

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

问题描述

我有一个ASP.NET页面的几个部分,需要分别验证它们。



每个部分都有它自己的验证摘要部分,所以我想到了使用的的ValidationSummary 标记与的ValidationGroup 属性,但它不能正常工作。



下面的代码工作,但验证所有控件的页面:

 < ASP:文本框ID =字段1=服务器的TabIndex =1MAXLENGTH =20/> 
< ASP:的RequiredFieldValidator ID =field1RequiredValidator的ControlToValidate =字段1=服务器
显示=无的ErrorMessage =强制1/>

< ASP:文本框ID =字段2=服务器的TabIndex =2MAXLENGTH =20/>
< ASP:的RequiredFieldValidator ID =field2RequiredValidator的ControlToValidate =字段2=服务器
显示=无的ErrorMessage =强制性2/>

....

< ASP:的ValidationSummary ID =的ValidationSummary的HeaderText =Sumary=服务器/>



而下面不工作(没有验证任何上提交我只是去到下一个页面精灵):

 < ASP:文本框ID =字段1=服务器的TabIndex =1MAXLENGTH = 20/> 
< ASP:的RequiredFieldValidator ID =field1RequiredValidator的ControlToValidate =字段1=服务器
显示=无的ErrorMessage =强制1的ValidationGroup =XXXX/>

< ASP:文本框ID =字段2=服务器的TabIndex =2MAXLENGTH =20/>
< ASP:的RequiredFieldValidator ID =field2RequiredValidator的ControlToValidate =字段2=服务器
显示=无的ErrorMessage =强制性2的ValidationGroup =XXXX/>

....

< ASP:的ValidationSummary ID =的ValidationSummary的HeaderText =Sumary=服务器的ValidationGroup =XXXX/>



我缺少的是在这里吗?有没有需要额外的设置还是其他什么东西?


解决方案

在ASP.NET的默认行为是,当用户点击一个按钮,没有的ValidationGroup 规定(并已的CausesValidation 设置为真正 ),不属于验证组的所有验证控件进行验证。



因此,为了验证一个特定的群体,你需要设置的ValidationGroup 按钮的属性,应当引起验证(也可能是的CausesValidation 属性)。



见Button.ValidationGroup的 MSDN文档的信息和实例



编辑:如果您需要验证页面上的所有群体,最简单的方法当然是在所有不使用验证组。



<$ P:不过,若你想验证只有一些(但不止一个)组,您可以通过调用做到这一点的服务器(在按钮的点击处理程序)上$ p> 验证(groupOne);
验证(groupTwo);
// ...

请注意,这将不会触发客户端验证。例如,见这个帖子的讨论关于上的一个按钮点击触发多个验证组



编辑:我找到的一篇博客文章中描述如何建立一个可重复使用的多重验证组按钮为ASP.NET,完整的代码。还没有尝试过,但它看起来很有希望。


I have a few section in an ASP.NET page and need to validate them separately.

Each section has it's own validation summary section so I thought of using the ValidationSummary tag with the ValidationGroup attribute but it does not work.

The following code works but validates all controls in the page:

<asp:TextBox ID="field1" runat="server" TabIndex="1" MaxLength="20" />
<asp:RequiredFieldValidator ID="field1RequiredValidator" ControlToValidate="field1" runat="server"
Display="None" ErrorMessage="mandatory 1" />

<asp:TextBox ID="field2" runat="server" TabIndex="2" MaxLength="20" />
<asp:RequiredFieldValidator ID="field2RequiredValidator" ControlToValidate="field2" runat="server"
Display="None" ErrorMessage="mandatory 2" />

....

<asp:ValidationSummary ID="validationSummary" HeaderText="Sumary" runat="server" />

While the following does not work (no validation whatsoever, on submit I just go to the next page in the wizard):

<asp:TextBox ID="field1" runat="server" TabIndex="1" MaxLength="20" />
<asp:RequiredFieldValidator ID="field1RequiredValidator" ControlToValidate="field1" runat="server"
Display="None" ErrorMessage="mandatory 1" ValidationGroup="xxxx" />

<asp:TextBox ID="field2" runat="server" TabIndex="2" MaxLength="20" />
<asp:RequiredFieldValidator ID="field2RequiredValidator" ControlToValidate="field2" runat="server"
Display="None" ErrorMessage="mandatory 2" ValidationGroup="xxxx" />

....

<asp:ValidationSummary ID="validationSummary" HeaderText="Sumary" runat="server" ValidationGroup="xxxx" />

What am I missing here? Is there extra setup needed or something else?

解决方案

The default behaviour in ASP.NET is that when the user clicks a button that has no ValidationGroup specified (and has CausesValidation set to true), all validation controls that do not belong to a validation group are validated.

Therefore, to validate a specific group, you need to set the ValidationGroup property of the button that should cause the validation (and also possibly the CausesValidation property).

See the MSDN documentation for Button.ValidationGroup for details and an example.

EDIT: If you need to validate ALL groups on the page, the easiest way is of course to not use validation groups at all. If you however want to validate only some (but more than one) groups, you can do it on the server (in the click handler of the button) by calling:

Validate("groupOne");
Validate("groupTwo");
// ...

Note that this won't trigger client-side validation. See for example this post for a discussion about triggering multiple validation groups on a single button click.

EDIT: I found a blog post describing how to build a reusable "multiple validation group button" for ASP.NET, complete with code. Haven't tried it, but it looks promising.

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

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