一个按钮可以验证更多的验证组? [英] Can a button validate more validation groups?

查看:178
本文介绍了一个按钮可以验证更多的验证组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种类型的验证器:

I have 3 types of validators:


  1. 有VG1验证组的一部分

  1. It is part of "VG1" validation group

有VG2验证组的一部分

It is part of "VG2" validation group

这不是任何验证组的一部分

It is not part of any validation groups

我有两个按钮,B1和B2。

I have two buttons, B1 and B2.

我想验证B1.Click当且仅当该第一和第三类型的所有验证成功验证关联给它们的控制。

I would like to validate B1.Click if and only if all validators of the first and third type successfully validated the controls associated to them.

我想验证B2.Click当且仅当所述第二和第三类型的所有验证成功验证关联给它们的控制。

I would like to validate B2.Click if and only if all validators of the second and third type successfully validated the controls associated to them.

这是可能的ASP.NET?如果是这样,你可以告诉我,我怎么能做到这一点还是我在哪里可以读的东西这会开导我这个问题?

Is this possible in ASP.NET? If so, can you tell me how can I do this or where could I read something which would enlighten me in this question?

编辑:

    function isValidButton1()
    {
        var VG1 = Page_ClientValidate("VG1");
        var empty = Page_ClientValidate("");
        return VG1 && empty;
    }

这工作,因为空组的验证好了,但是,如果VG1是无效的,那么信息就会消失。是否有显示所有验证错误消息的解决方案?谢谢你。

This works well, however, if VG1 is invalid, then the messages will disappear, because of the validation of the empty group. Is there a solution to show all validation error messages? Thank you.

EDIT2:

    function isValidSaveAsClosed()
    {
        Page_ClientValidate("");
        Page_ClientValidate("VG1");
        var groups = [];
        groups[0] = undefined;
        groups[1] = "VG1";
        var valid = true;
        for (var f in Page_Validators)
        {
            if (jQuery.inArray(Page_Validators[f].validationGroup, groups) >= 0)
            {
                ValidatorValidate(Page_Validators[f]);
                valid = valid && Page_Validators[f].isvalid;
            }
        }

        return valid;
    }

上面的功能,解决了我的问题。

The function above solves my problem.

推荐答案

我加入另一个答案,因为添加到我的最后一个现有的回答会令答案太大,任何人都可以阅读。

I am adding another answer since adding to my last existing answer would make the answer too big for anyone to read.

在这个答案,我已经在我的最后答案扩大,使多个验证组是自动迷上了无论在客户端和服务器端。这意味着你不需要调用 Page_ClientValidate(组1,组2)按钮,在JavaScript中 onclick事件,因为它会自动发生。另外,对于多个组的服务器端验证会自动发生。

In this answer, I have expanded on my last answer so multiple validation groups are automatically hooked up both on client-side as well as server-side. This means you do not need to call Page_ClientValidate("group1,group2") in JavaScript onclick event of button, since it will occur automatically. Also, the server-side validation for multiple groups will happen automatically.

的标记和code-落后于这个如下。你可以试试,我所提供的ASPX code,并在网站项目进行测试。为了测试时,自动服务器端验证,则必须设置 EnableClientScript =假为三个验证器。

The markup and code-behind for this is given below. You can try the aspx code that I have provided and test it in a website project. To test if automatic server-side validation occurs, you must set EnableClientScript="false" for each of the three validators.

的多组自动验证方法说明

如果要实现多个验证组,下面的步骤需要在你的aspx页面来完成。请确保你的标记你提到一个逗号分隔的验证组的列表的ValidationGroup 按钮控件的属性,如果你需要同时验证多个组。

If you want to implement multiple validation groups, the following steps need to done in your aspx page. Make sure that in your markup you mention a comma-delimited list of validation groups for ValidationGroup property of button control if you need to validate multiple groups at a time.


  1. 您需要通过添加JavaScript来你的aspx页面的端覆盖JavaScriptmethod IsValidationGroupMatch (此覆盖code在标记$ C $的结尾给出下面C,你可以复制/粘贴到您的aspx页面);这是ASP.Net验证框架提供了一个标准的方法。

  2. 您需要联播与多个验证组对于客户端验证,因为这不是由ASP.Net自动完成按钮;对于这一点,你要隐藏调用C在$ C $方法 HookupValidationForMultipleValidationGroups 在Page_Load事件对于具有多个验证组每个按钮。(你可以复制/粘贴此方法在二线code段给到你的aspx页面的code-后面)

  3. 您需要重写服务器端的方法验证来的,因为这在ASP.Net缺少多个验证组添加功能。(你可以复制/粘贴此方法在二线code段给到你的aspx页面的code-后面)

  1. you need to override the JavaScriptmethod IsValidationGroupMatch by adding JavaScript to end of your aspx page (The code for this override is given at end of markup code below and you can copy/paste it into your aspx page); this is a standard method provided by ASP.Net validation framework.
  2. you need to hookup the button with multiple validation groups for client-side validation since this is NOT done automatically by ASP.Net; for this, you have to call the method HookupValidationForMultipleValidationGroups in code-behind in Page_Load event for each button that has multiple validation groups.(you can copy/paste this method given in second-code snippet into the code-behind of your aspx page)
  3. you need to override the server-side method Validate to add functionality for multiple validation groups since this is missing in ASP.Net.(you can copy/paste this method given in second-code snippet into the code-behind of your aspx page)

与多个验证组的aspx的标记的按钮

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleValidationGroupsByOneButton.aspx.cs" Inherits="MultipleValidationGroupsByOneButton" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            TextBox1 : 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="TextBox1 needs input"  ControlToValidate="TextBox1" ForeColor="Red" ValidationGroup="group1"></asp:RequiredFieldValidator>

            <br />
            <br />
            TextBox2 : 
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="TextBox2 needs input"  ControlToValidate="TextBox2" ForeColor="Red" ValidationGroup="group2"></asp:RequiredFieldValidator>
            <br />
            <br />
            TextBox3 :
             <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="TextBox3 needs input"  ControlToValidate="TextBox3" ForeColor="Red" ValidationGroup="group3"></asp:RequiredFieldValidator>

            <br />
            <br />
        </div>
        <asp:Button ID="btnMultipleValidationGroups" runat="server" Text="Validate group1 and group2" ValidationGroup="group1,group2" OnClick="btnMultipleValidationGroups_Click" />
        <asp:Button ID="btnGroup1" runat="server" Text="Validate only group1" ValidationGroup="group1" OnClick="btnGroup1_Click" />
        <asp:Button ID="btnGroup2" runat="server" Text="Validate only group2" ValidationGroup="group2" OnClick="btnGroup2_Click" />
        <asp:Label ID="lblMessage" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
        <script type="text/javascript">
            window["IsValidationGroupMatch"] = function (control, validationGroup) {
                if ((typeof (validationGroup) == "undefined") || (validationGroup == null)) {
                    return true;
                }
                var controlGroup = "";
                var isGroupContained = false;
                if (typeof (control.validationGroup) == "string") {
                    controlGroup = control.validationGroup;
                    var controlGroupArray = [];
                    if (validationGroup.indexOf(",") > -1) {
                        controlGroupArray = validationGroup.split(",");// validationGroup.split(",");
                    }
                    for (var i = 0; i < controlGroupArray.length; i++) {
                        if (controlGroupArray[i].trim() == controlGroup.trim()) {
                            isGroupContained = true;
                        }
                    }
                }
                return (controlGroup == validationGroup || isGroupContained);
            }
        </script>
    </form>
</body>
</html>

$ C $上面aspx页面的C-背后

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MultipleValidationGroupsByOneButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //always call this method in Page Load event for each button with multiple validation groups
        HookupValidationForMultipleValidationGroups(btnMultipleValidationGroups);
    }
    //the method below will automatically hook up a button with multiple validation groups for client-side validation
    private void HookupValidationForMultipleValidationGroups(IButtonControl button)
    {
     if (!Page.IsPostBack && button.ValidationGroup.Contains(","))
     {
        //hook up validation on client-side by emitting the appropriate javascript for onclick  event of the button with multiple validation groups
        PostBackOptions myPostBackOptions = new PostBackOptions((WebControl)button);
        myPostBackOptions.ActionUrl = string.Empty;
        myPostBackOptions.AutoPostBack = false;
        myPostBackOptions.RequiresJavaScriptProtocol = true;
        myPostBackOptions.PerformValidation = true;//THIS true value hooks up the client-side validation
        myPostBackOptions.ClientSubmit = true;
        myPostBackOptions.ValidationGroup = button.ValidationGroup;

        // Add postback script so cleint-side validation is automatically hooked up for control with multiple validation groups
        ((WebControl)button).Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(myPostBackOptions));
     }
    }
    //Override default Validate method so server-side validation of buttons with multiple validation groups occurs automatically 
    public override void Validate(string validationGroup)
    {
        if (validationGroup.Contains(","))
        {
            string[] validationGroups = validationGroup.Split(",".ToCharArray());
            foreach (string group in validationGroups)
            {
                Page.Validate(group);
            }
        }
        base.Validate(validationGroup);
    }
    protected void btnMultipleValidationGroups_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblMessage.Text = "Button with multiple validation groups was clicked";
        }
    }
    protected void btnGroup1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblMessage.Text = "Button with Group1 validation group was clicked";
        }
    }
    protected void btnGroup2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblMessage.Text = "Button with Group2 validation group was clicked";
        }
    }

}

这篇关于一个按钮可以验证更多的验证组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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