验证摘要消息弹出两次
我的页面中有验证控件,并且我正在使用验证摘要消息框来显示验证消息,下面显示的 javascript 函数对我有用,但问题是我在按钮的 OnClientClick 事件上使用此 javascript 函数,当我单击带有表单控件的按钮未填充,它按预期显示验证摘要消息框,但是当我关闭摘要框时,它会再次显示,并且我需要关闭消息框才能消失,这意味着我每次都必须单击两次。有人可以纠正我我做错了什么吗?
这是我正在做的:-
<asp:Button ID="SubmitButtonOne" runat="server" Text="Submit" OnClick="SubmitButtonOne_Click"
ValidationGroup="Cases" ClientIDMode="Static" OnClientClick="PleaseWaitShow()" />
这是 javascript 函数:
function PleaseWaitShow() {
var isPageValid = true;
// Do nothing if client validation is not active
if (typeof (Page_Validators) != "undefined") {
if (typeof (Page_ClientValidate) == 'function') {
isPageValid = Page_ClientValidate();
}
}
if (isPageValid) {
document.getElementById('SubmitButtonOne').value = "Processing...";
}
}
后面的代码:
protected void SubmitButtonOne_Click(object sender, EventArgs e)
{
try
{
// some functionality
}
catch (Exception)
{
//show errror message
}
}
I have validation controls in my page and I am using Validation summary message box to display the validation messages,the javascript function shown below worked for me but the problem is I am using this javascript function on OnClientClick event of the button and when I click the button with form controls not been filled its displaying the validation summary message box as expected but when i close the summary box it's getting displayed again and i need to close the message box to disappear, means i have to do two clicks everytime. Can somebody correct me what am I doing wrong.
Here is what I am doing:-
<asp:Button ID="SubmitButtonOne" runat="server" Text="Submit" OnClick="SubmitButtonOne_Click"
ValidationGroup="Cases" ClientIDMode="Static" OnClientClick="PleaseWaitShow()" />
Here is the javascript function:
function PleaseWaitShow() {
var isPageValid = true;
// Do nothing if client validation is not active
if (typeof (Page_Validators) != "undefined") {
if (typeof (Page_ClientValidate) == 'function') {
isPageValid = Page_ClientValidate();
}
}
if (isPageValid) {
document.getElementById('SubmitButtonOne').value = "Processing...";
}
}
code behind:
protected void SubmitButtonOne_Click(object sender, EventArgs e)
{
try
{
// some functionality
}
catch (Exception)
{
//show errror message
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果使用 Page_ClientValidate 方法,则应将 CauseValidation 设置为 false。
CausesValidation="false"
因为 Page_ClientValidate 已经对您验证的按钮或控件执行此操作
if you use Page_ClientValidate method, you should set causevalidation to false.
CausesValidation="false"
because Page_ClientValidate is already doing this for button or control you validate
请删除按钮的ValidationGroup。 ValidationSummary 的唯一用户
喜欢:
谢谢
Please remove ValidationGroup of Button. Only user for ValidationSummary
Like:
Thanks
不,这不是预期的行为。相反,这是 ASP.NET 验证 JS 代码中的错误。
ValidationSummaryOnSubmit
函数循环遍历所有摘要,并且对于每个摘要,循环遍历所有验证器。如果未指定组,则附加每个验证器的消息,导致相同消息重复 2(3、4、5?)次。我通过修复函数解决了这个问题(盯着第 53 行左右):
No, it is not the expected behavior. Rather, it's a bug in ASP.NET validation JS code.
The
ValidationSummaryOnSubmit
function loops over all summaries, and for each summary, loops over all validators. If not group is specified, then each validator's message is appended, resulting in 2 (3, 4, 5?) repetitions of the same messages.I solved the problem by fixing the function (staring at line 53 or so):
这是预期的行为。
一旦 Page_ClientValidate 由于您在 PleaseWaitShow 方法内的显式调用而被触发,第二个是通过单击 PostBack 按钮进行隐式调用。
因此您会看到两次 ValidationSummary 消息框。
我没有解决方案来规避这个问题,但如果出现问题我会更新。
注意:我想指出的一件事是,由于您的提交按钮上有ValidationGroup="Cases",因此您应该将其传递给 Page_ClientValidate 方法。
更新1:我能想到的一种方法是尝试这样的事情:
1:
OnClientClick =“return PleaseWaitShow();”
2:从PleaseWaitShow()返回isPageValid:
It is expected behavior.
Once Page_ClientValidate is fired due to your explicit call inside PleaseWaitShow method and second is an implicit call made by the PostBack button click.
And so you are seeing two times the ValidationSummary message box.
I don't have a solution to circumvent this but will update if something strikes.
Note: One thing I would like to point out is since you have ValidationGroup="Cases" on your submit button you should pass that to your Page_ClientValidate method.
Update 1: One way I can think of is trying something like this:
1:
OnClientClick="return PleaseWaitShow();"
2: return isPageValid from PleaseWaitShow():