ASP.NET 2.0。假设我有两个验证组valGrpOne和valGrpTwo;和两个验证摘要valSummOne和valSummTwo;分裂部分的原因纯粹是美学的。一个提交按钮,可触发两组的验证。
所以我设置一个调用btnSubmit的Javascript函数,在这个函数里面,我连续调用了Page_ClientValidate(“valGrpOne”)和Page_ClientValidate(“valGrpTwo”);问题只是一次一个总结显示(但我真的想要两个显示!)
非常类似于以下问题,哪些服务器端的答案。
Triggering multiple validation groups with a single button?
解决方法
好的,所以答案并不简单。似乎客户端验证的默认行为是仅显示刚刚验证的最新组/摘要。但是一点点的Javascript调整给了我一个可以接受的答案。
随意提供改进。
<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>