Page_ClientValidate() 与多个 ValidationGroups - 如何同时显示多个摘要? [英] Page_ClientValidate() with multiple ValidationGroups - how to show multiple summaries simultaneously?
问题描述
ASP.NET 2.0.假设我有两个验证组 valGrpOne 和 valGrpTwo;和两个验证摘要 valSummOne 和 valSummTwo;拆分部分的原因纯粹是审美.一个提交按钮,触发两个组的验证.
ASP.NET 2.0. Lets say i have two Validation Groups valGrpOne and valGrpTwo; and two Validation Summaries valSummOne and valSummTwo; Reason for breaking up sections is purely aesthetic. One submit button which triggers validation on both groups.
现在我想触发客户端验证,并希望同时显示两个验证摘要;
Now i want to trigger Client-Side validation, AND want BOTH validation summaries to display at the same time;
所以我设置了一个在 btnSubmit 上调用的 Javascript 函数,在这个函数中我连续调用 Page_ClientValidate("valGrpOne")
和 Page_ClientValidate("valGrpTwo")
;问题是一次只显示一个摘要(但我真的想同时显示!)
So i setup a Javascript function which gets called upon btnSubmit, and inside this function i call Page_ClientValidate("valGrpOne")
and Page_ClientValidate("valGrpTwo")
in succession; Problem is only one summary shows at a time (But i really want both to show!)
关于如何从客户端代码同时显示两个验证摘要的任何想法?
Any ideas on how to get both validation summaries to display simultaneously, from client-side code?
与以下问题非常相似,该问题为服务器端提供答案.一个按钮触发多个验证组?
Very similar to the following question, which answers for server-side. Triggering multiple validation groups with a single button?
推荐答案
好的,所以答案并不简单.客户端验证的默认行为似乎是仅显示刚刚验证的最新组/摘要.但是一些 Javascript tweeking 给了我一个可以接受的答案.
Ok, so the answer was not simple. It seems the default behaviour of client-side validation is to show only the lastest group / summary that has just been validated. But a bit of Javascript tweeking gave me an acceptable answer.
随时提出改进意见.
<script type="text/javascript" language="javascript">
/* Manual client-side validation of Validator Groups */
function fnJSOnFormSubmit() {
var isGrpOneValid = Page_ClientValidate("valGrpOne");
var isGrpTwoValid = Page_ClientValidate("valGrpTwo");
var i;
for (i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators[i]); //this forces validation in all groups
}
//display all summaries.
for (i = 0; i < Page_ValidationSummaries.length; i++) {
summary = Page_ValidationSummaries[i];
//does this summary need to be displayed?
if (fnJSDisplaySummary(summary.validationGroup)) {
summary.style.display = ""; //"none"; "inline";
}
}
if (isGrpOneValid && isGrpTwoValid)
return true; //postback only when BOTH validations pass.
else
return false;
}
/* determines if a Validation Summary for a given group needs to display */
function fnJSDisplaySummary(valGrp) {
var rtnVal = false;
for (i = 0; i < Page_Validators.length; i++) {
if (Page_Validators[i].validationGroup == valGrp) {
if (!Page_Validators[i].isvalid) { //at least one is not valid.
rtnVal = true;
break; //exit for-loop, we are done.
}
}
}
return rtnVal;
}
</script>
这篇关于Page_ClientValidate() 与多个 ValidationGroups - 如何同时显示多个摘要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!