asp.net – 按钮可以验证更多验证组吗?

前端之家收集整理的这篇文章主要介绍了asp.net – 按钮可以验证更多验证组吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有3种类型的验证器:

>它是“VG1”验证组的一部分
>它是“VG2”验证组的一部分
>它不是任何验证组的一部分

我有两个按钮,B1和B2.

我想验证B1.Click当且仅当第一和第三类型的所有验证器成功验证了与它们相关的控件时.

我想验证B2.点击当且仅当第二和第三类的所有验证器成功验证了与它们相关的控件时.

这在ASP.NET中可行吗?如果是这样,你能告诉我我该怎么做?或者在哪里可以阅读一些可以启发我的问题?

编辑:

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

这很有效,但是,如果VG1无效,则由于空组的验证,消息将消失.是否有解决方显示所有验证错误消息?谢谢.

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;
    }

上面的功能解决了我的问题.

解决方法

添加了另一个答案,因为添加到我上一个现有的答案将使得答案太大,任何人都无法阅读.

在这个答案中,我扩展了我的最后一个答案,因此在客户端和服务器端都会自动连接多个验证组.这意味着你不需要在JavaScript onclick按钮事件中调用Page_ClientValidate(“group1,group2”),因为它会自动发生.此外,多个组的服务器端验证将自动进行.

下面给出了标记代码隐藏.您可以尝试我提供的aspx代码并在网站项目中测试它.要测试是否发生自动服务器端验证,必须为三个验证器中的每一个设置EnableClientScript =“false”.

多个组的自动验证方法的说明

如果要实现多个验证组,则需要在aspx页面中完成以下步骤.如果需要一次验证多个组,请确保在标记中为按钮控件的ValidationGroup属性提及逗号分隔的验证组列表.

>您需要通过在aspx页面的末尾添加JavaScript来覆盖JavaScript方法IsValidationGroupMatch(此覆盖的代码在下面的标记代码末尾给出,您可以将其复制/粘贴到您的aspx页面中);这是ASP.Net验证框架提供的标准方法.
>您需要将具有多个验证组的按钮连接起来进行客户端验证,因为这不是由ASP.Net自动完成的.为此,您必须在具有多个验证组的每个按钮的Page_Load事件中调用代码隐藏中的方法HookupValidationForMultipleValidationGroups.(您可以将第二代码片段中给出的此方法复制/粘贴到您的aspx页面代码隐藏中)
>您需要覆盖服务器端方法验证以添加多个验证组的功能,因为ASP.Net中缺少此功能.(您可以将第二代码片段中给出的此方法复制/粘贴到您的aspx页面代码隐藏中) )

使用多个验证组为一个按钮标记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>

以上aspx页面代码隐藏

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";
        }
    }

}
原文链接:https://www.f2er.com/aspnet/249244.html

猜你在找的asp.Net相关文章