C# 用一个按钮触发多个验证组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/984191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Triggering multiple validation groups with a single button?
提问by Craig
Let's say the page TestPage.aspx has two controls. The first control is an address control that has a validation group called "AddressGroup". This group contains several validation controls which are colated in the validation summary on that control. The second control is a credit card control and has a validation group called "CreditCardGroup". It also has several validators and a summary to display the results. To add to the problem, there are some random controls on the page that also have validators which are tied to a third ValidatorSummary control.
假设页面 TestPage.aspx 有两个控件。第一个控件是一个地址控件,它有一个名为“AddressGroup”的验证组。该组包含多个验证控件,这些控件在该控件的验证摘要中进行了整理。第二个控件是信用卡控件,它有一个名为“CreditCardGroup”的验证组。它还有几个验证器和一个用于显示结果的摘要。更糟糕的是,页面上有一些随机控件也有与第三个 ValidatorSummary 控件相关联的验证器。
When the user presses the "Do it all" button, I would like the page to trigger all three validation groups. The button itself can be tied to a single group or an unlabeled group. It can not be tied to multiple groups as far as I can tell.
当用户按下“全部完成”按钮时,我希望页面触发所有三个验证组。按钮本身可以绑定到单个组或未标记的组。据我所知,它不能绑定到多个组。
The solution is not to extract the validation from the controls as that would deminish the value of having them in seperate controls. Thanks for your thoughts.
解决方案不是从控件中提取验证,因为这会削弱将它们置于单独控件中的价值。谢谢你的想法。
采纳答案by Peter Lange
Are you talking client side or server side validation? Jamie's answer is spot on for server side, but for client side validation you will probably need to write your own JS function that will trigger validation on all three groups in concert.
你说的是客户端验证还是服务器端验证?Jamie 的答案是针对服务器端的,但对于客户端验证,您可能需要编写自己的 JS 函数,该函数将同时触发对所有三个组的验证。
回答by Jamie Ide
Call the Validate method for each validation group individually inside the button's click handler:
在按钮的单击处理程序中为每个验证组分别调用 Validate 方法:
bool isValidTest = false;
Validate("AddressGroup");
isValidTest = IsValid;
Validate("CreditCardGroup");
isValidTest &= IsValid;
// etc.
if (!isValidTest) return;
The next problem you may encounter is that the ValidationSummary control is linked to a single validation group. The only way that I've found to display all the error messages for multiple groups (without walking the control tree) is use multiple ValidationSummary controls.
您可能会遇到的下一个问题是 ValidationSummary 控件链接到单个验证组。我发现显示多个组的所有错误消息(无需遍历控件树)的唯一方法是使用多个 ValidationSummary 控件。
With user controls, you may want to have its Validate method perform validation for all the controls it contains and display its own summary.
对于用户控件,您可能希望其 Validate 方法对其包含的所有控件执行验证并显示其自己的摘要。
Edited to add: The isValidTest variable is not needed. According to the docs:
编辑添加:不需要 isValidTest 变量。根据文档:
Note that when you call the Validate method, the IsValid property reflects the validity of all groups validated so far.
请注意,当您调用 Validate 方法时,IsValid 属性反映了迄今为止已验证的所有组的有效性。
回答by Basit Tanveer
Call Page.Validate()
on server side it will validate all the validators..
Page.Validate()
在服务器端调用它将验证所有验证器..