asp.net - GridView的验证 - 重复验证信息的问题 [英] asp.net - gridview validation - repeating validation messages issue

查看:159
本文介绍了asp.net - GridView的验证 - 重复验证信息的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一对多行(最喜欢做)的GridView - 每一个输入文本框。每一行都有针对文本框一个RequiredFieldValidator。当表单提交GridView的验证,完全可以超过一列有一个空的文本框。这将导致重复验证消息例如

I have a gridview that contains one to many rows (like most do) - each with an input textbox. Each row has a requiredfieldvalidator against that textbox. When the form is submitted the gridview is validated it is entirely possible that more than one row has an empty textbox. This results in repeating validation messages e.g.


  • 请提供名称字段中的文本

  • 请提供名称字段中的文本

  • 请提供名称字段中的文本

是否有可能将这些信息整合到一个消息?

Is it possible to consolidate these messages into one message?

我知道它可以创建通过设置一个验证器类以及可用于验证GridView控件作为一个整体BaseValidator类继承的验证。但是我把图像对每一行,当它是无效的,所以我要想像我需要在每一行独立验证。

I know it possible to create a validator by setting up a validator class and inheriting from BaseValidator class which can be used to validate the gridview as a whole. But I put an image against each row when it is invalid so I should imagine I require separate validators on each row.

推荐答案

这是一个使用的CustomValidator并要求几个组织变化的解决方案。这需要一个回传,因为的CustomValidator验证是在服务器端执行。

This is a solution that uses a CustomValidator and requires a few organizational changes. This requires a postback since CustomValidator validation is performed on the server-side.

这里的设置:


  1. 对于每个显示的名称字段请提供文字您现有的RequiredFieldValidators的消息,您将需要设置:

  2.   

          
    • EnableClientScript =假

    •     
    • 的ValidationGroup =vgTxtName(提供自己的姓名)

    •     
    • 的ErrorMessage =(或者干脆删除它,而现在的CustomValidator将负责此)

    •   
  • 显示=无

      
  • 显示=动态

  •   
  • 设置的文本,以验证标签之间:*


        
  • 添加事件处理程序为的CustomValidator的事件的ServerValidate(你可以加倍从设计单击它有它产生的)

  •     
  • 实施EventHandler的逻辑(见code以下)

  •   

我们的想法是不是直接让页面来处理这些RequiredFieldValidators了,而是我们让的CustomValidator做到这一点。

The idea is not to directly allow the page to handle those RequiredFieldValidators anymore and instead we'll let the CustomValidator do it.

文本框的RequiredFieldValidator例如(你应该有一些看起来像这样与它对应上面的步骤1相关的ID名):

TextBox RequiredFieldValidator example (you should have something that looks like this with relevant ID names which corresponds to step 1 above):

选项1:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server" 
EnableClientScript="false" Display="None" ValidationGroup="vgTxtName" />

选项2:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server" 
EnableClientScript="false" Display="Dynamic" ValidationGroup="vgTxtName">*
</asp:RequiredFieldValidator>

的CustomValidator标记(你可以把这个懂事的任何地方,比如旁边ValidationSummary控件):

CustomValidator Markup (you can place this anywhere sensible, such as next to the ValidationSummary control):

<asp:CustomValidator ID="cvName" runat="server" Display="None"
ErrorMessage="Please provide text for 'Name' field" 
OnServerValidate="cvName_ServerValidate" />

错误消息在这里取代了个人验证的人。还要注意有没有的ControlToValidate集,它的有效期为这种类型的验证,并且是应用验证涵盖多个控件非常有用。

The error message here replaces the ones from the individual validators. Also notice there's no ControlToValidate set, which is valid for this type of validator and is useful for applying validation covering multiple controls.

的CustomValidator事件处理程序(cvName_ServerValidate):

CustomValidator EventHandler (cvName_ServerValidate):

protected void cvName_ServerValidate(object source, ServerValidateEventArgs args)
{
    // Validate vgTxtName group
    Page.Validate("vgTxtName");

    // .NET 3.5 - add using System.Linq;
    args.IsValid = Page.GetValidators("vgTxtName")
                        .OfType<RequiredFieldValidator>()
                        .All(v => v.IsValid);

    // .NET 2.0 (use either this or the above, not both)
    bool isValid = true;
    foreach (RequiredFieldValidator validator in Page.GetValidators("vgTxtName"))
    {
        isValid &= validator.IsValid;
    }
    args.IsValid = isValid;
}

这就是它!只要记住,这是严格RequiredFieldValidators。你不应该把不同类型的校验器的vgTxtName组中,因为cvName逻辑与的RequiredFieldValidator类型严格的交易。你需要设置不同分组或调整的code,如果你打算使用其他验证类型。

That's it! Just bear in mind that this is strictly for RequiredFieldValidators. You shouldn't place different types of validators in the "vgTxtName" group since the cvName logic deals strictly with the RequiredFieldValidator type. You'll need to setup different groupings or tweak the code if you intend to use other validator types.

这篇关于asp.net - GridView的验证 - 重复验证信息的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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