我有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;
}
上述功能解决了我的问题。
答案 0 :(得分:11)
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />
<script type="text/javascript">
function Validate()
{
var isValid = false;
isValid = Page_ClientValidate('VG1');
if (isValid)
{
isValid = Page_ClientValidate('VG2');
}
return isValid;
}
</script>
试试这个....希望它会有所帮助
答案 1 :(得分:4)
是的按钮可以验证多个验证组。
在button_click中,您可以将组验证为
Page.Validate("VG1");
Page.Validate("VG2");
if (Page.IsValid)
{
// Page is valid so proceed..!!
}
答案 2 :(得分:4)
我正在添加另一个答案,因为添加到我上一个现有的答案会让答案太大,任何人都无法阅读。
在这个答案中,我扩展了我的上一个答案,因此在客户端和服务器端都连接了多个验证组automatically
。这意味着您无需在JavaScript onclick按钮事件中调用Page_ClientValidate("group1,group2")
,因为它会自动发生。此外,多个组的服务器端验证将自动进行。
下面给出了标记和代码隐藏。您可以尝试我提供的aspx代码并在网站项目中测试它。要测试是否发生自动服务器端验证,您必须为三个验证器中的每一个设置EnableClientScript="false"
。
自动验证多个群组的方法说明
如果要实现多个验证组,则需要在aspx页面中完成以下步骤。如果您需要一次验证多个组,请确保在标记中为按钮控件的ValidationGroup
属性提及逗号分隔的验证组列表。
IsValidationGroupMatch
(此覆盖的代码在下面的标记代码末尾给出,您可以将其复制/粘贴到您的aspx页面中);这是ASP.Net验证框架提供的标准方法。HookupValidationForMultipleValidationGroups
。(您可以将第二代码片段中给出的此方法复制/粘贴到代码隐藏中)你的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";
}
}
}
答案 3 :(得分:1)
您可以使用Page_ClientValidate(validationgroup)
功能验证验证组。
喜欢
function Validate(vgroup) {
return Page_ClientValidate(vgroup);
}
你可以尝试
<asp:Button ID="B1" runat="server" Text="Button"
OnClientClick="return Validate('VG1') && Validate() " />
<asp:Button ID="B2" runat="server" Text="Button"
OnClientClick="return Validate('VG2') && Validate() " />
答案 4 :(得分:1)
这是一篇很老的帖子,但我最近遇到了同样的问题,我解决它的方式如下所述。
您可以将此帖中给出的代码复制到ASP.Net网站项目中并自行测试。第一个按钮,即左边的按钮,与其他两个按钮不同,同时验证三个验证组。
只需在下面的代码中包含aspx页面末尾给出的JavaScript,其中我已经覆盖了验证函数IsValidationGroupMatch
,因此可以验证多个组。然后,如果要在客户端验证多个组,则必须调用函数Page_ClientValidate("group1,group2,group6,group5")
,您只需传递逗号分隔的验证组列表。
(注意:但请记住,通过使用此方法,您可以仅在客户端验证多个验证组。这不会自动验证服务器端的多个组。您必须调用Page_ClientValidate
的API函数在客户端,因为ASP.Net框架不会自动连接多个组验证。)
示例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" onclientclick="if(!Page_ClientValidate('group1,group2,group3')) { return false;}" />
<asp:Button ID="btnGroup1" runat="server" Text="Validate only group1" ValidationGroup="group1" />
<asp:Button ID="btnGroup2" runat="server" Text="Validate only group2" ValidationGroup="group2" />
<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(",");
}
for (var i = 0; i < controlGroupArray.length; i++) {
if (controlGroupArray[i].trim() == controlGroup.trim()) {
isGroupContained = true;
}
}
}
return (controlGroup == validationGroup || isGroupContained);
}
</script>
</form>
</body>
</html>
答案 5 :(得分:0)
一种不涉及编写任何附加代码的简单方法是简单地在任何需要的地方复制验证器,如下面的示例所示:
<div>
<asp:TextBox ID="txt1" runat="server" placeholder="field1" />
<asp:RequiredFieldValidator runat="server" ID="req1a" ControlToValidate="txt1" ValidationGroup="group1" Display="Dynamic" />
<asp:RequiredFieldValidator runat="server" ID="req1b" ControlToValidate="txt1" Display="Dynamic" />
</div>
<div>
<asp:TextBox ID="txt2" runat="server" placeholder="field2" />
<asp:RequiredFieldValidator runat="server" ID="req2" ControlToValidate="txt2" />
</div>
<!-- Validate validators with empty ValidationGroup -->
<asp:Button runat="server" ID="btn1" Text="Validate All" />
<!-- Validate group1 validators -->
<asp:Button runat="server" ID="btb2" Text="Validate Group 1" ValidationGroup="group1"/>
第一个按钮将验证两个文本框,即使它们位于不同的组中,而第二个按钮仅验证第一个文本框。