我正在添加另一个答案,因为添加到我最后一个现有答案会使答案太大,任何人都无法阅读。
在这个答案中,我已经扩展了我的最后一个答案,因此automatically
在客户端和服务器端都连接了多个验证组。这意味着您不需要Page_ClientValidate("group1,group2")
在 JavaScript 中调用按钮的 onclick 事件,因为它会自动发生。此外,多个组的服务器端验证将自动发生。
下面给出了标记和代码隐藏。您可以尝试我提供的 aspx 代码并在网站项目中对其进行测试。要测试是否发生自动服务器端验证,您必须EnableClientScript="false"
为三个验证器中的每一个进行设置。
多组自动验证方法说明
如果要实现多个验证组,则需要在您的 aspx 页面中完成以下步骤。ValidationGroup
如果您需要一次验证多个组,请确保在您的标记中提及按钮控件属性的逗号分隔的验证组列表。
- 您需要
IsValidationGroupMatch
通过将 JavaScript 添加到您的 aspx 页面末尾来覆盖 JavaScript 方法(此覆盖的代码在下面的标记代码末尾给出,您可以将其复制/粘贴到您的 aspx 页面中);这是 ASP.Net 验证框架提供的标准方法。
- 您需要将按钮与多个验证组连接以进行客户端验证,因为这不是由 ASP.Net 自动完成的;为此,您必须为
HookupValidationForMultipleValidationGroups
具有多个验证组的每个按钮在 Page_Load 事件的代码隐藏中调用该方法。(您可以将第二个代码片段中给出的此方法复制/粘贴到您的 aspx 页面的代码隐藏中)
- 您需要覆盖服务器端方法
Validate
以添加多个验证组的功能,因为 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";
}
}
}