javascript 在asp.net中验证表单数据后如何获取javascript确认框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8641395/
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
How to get javascript confirm box after validating the form data in asp.net?
提问by Peeyush
I am facing a problem in my asp.net web application that i need to display a java script confirmation box on click of Updatebutton and this as per requirements given to me.
我在我的 asp.net Web 应用程序中遇到了一个问题,我需要在点击更新按钮时显示一个 java 脚本确认框,这符合给我的要求。
Currently I am using onclient click
目前我正在使用客户端点击
<asp:Button ID="btnCustomerUpdate" runat="server" CssClass="applybuttonstyle"
Text="Update" onclick="btnCustomerUpdate_Click"
OnClientClick="return confirm('Do you really want to update?');"/>
It's working fine but the problem is i also applied some validation controls on my form fields so if a user leave any field blank and click update button then it shows javascript confirm box and the validation message below that form field.
它工作正常,但问题是我还在我的表单字段上应用了一些验证控件,因此如果用户将任何字段留空并单击更新按钮,那么它会显示 javascript 确认框和该表单字段下方的验证消息。
And it is not desired behavior it should not show the confirmation box until unless all validation are passed.
除非所有验证都通过,否则它不应该显示确认框,这不是预期的行为。
I hope this is due to because i am using this on client click so please tell me a better solution for that.
我希望这是因为我在客户端点击时使用它,所以请告诉我一个更好的解决方案。
Thanks in advance.
提前致谢。
回答by Pencho Ilchev
you can use Page_ClientValidate
in your own confirm function to decide whether to display the confirmation dialog.
Something like:
您可以Page_ClientValidate
在自己的确认函数中使用来决定是否显示确认对话框。就像是:
function validateAndConfirm(message){
var validated = Page_ClientValidate('group1');
if (validated){
return confirm(message);
}
}
And on the server you will have something like:
在服务器上,您将有类似的东西:
<asp:textbox id="TextBox1" runat="server"/>
<asp:requiredfieldvalidator ValidationGroup="group1"
ErrorText="Need to Fill in Value!"
ControlToValidate="TextBox1"
runat="server"/>
<asp:textbox id="TextBox2" runat="server"/>
<asp:requiredfieldvalidator ValidationGroup="group1"
ErrorText="Need to Fill in Value!"
ControlToValidate="TextBox2"
runat="server"/>
And your button code will change to:
您的按钮代码将更改为:
<asp:Button ID="btnCustomerUpdate" runat="server" CssClass="applybuttonstyle"
Text="Update" onclick="btnCustomerUpdate_Click" ValidationGroup="group1"
OnClientClick="return validateAndConfirm('Do you really want to update?');"/>
Obviously, I cannot test this, but you get the idea.
显然,我无法对此进行测试,但是您明白了。
If you will be using Page_ClientValidate
you might find this SO questionuseful.
如果您将使用,Page_ClientValidate
您可能会发现这个 SO 问题很有用。
回答by competent_tech
You can call the Page_ClientValidate method to ensure the page is valid before asking the user:
您可以在询问用户之前调用 Page_ClientValidate 方法以确保页面有效:
if (typeof(Page_ClientValidate) == 'function' && Page_ClientValidate() == false) {
return false;
} else {
return confirm('Do you really want to update?');
}
回答by Adeel
You should atleast apply validation on client side first and than open a confirmation dialog, while on server, you have to use server side validation first too in case if user has disabled javascript, and than update the record.
您至少应该首先在客户端应用验证,然后打开确认对话框,而在服务器上,如果用户禁用了 javascript,您也必须首先使用服务器端验证,然后更新记录。
You can also use AJAX to update record, so if a validation message occurs, shows error message and if the validation passed, than you can alert the user but the only disadvantage of using that is you have to re-post the data again. To mitigate this problem you either have to save in temporary table, until user gave confirmation and than delete it from temporary table but obviously it takes a lot of work.
您还可以使用 AJAX 来更新记录,因此如果出现验证消息,则显示错误消息,如果验证通过,您可以提醒用户,但使用它的唯一缺点是您必须再次重新发布数据。为了缓解这个问题,你要么必须保存在临时表中,直到用户确认,然后从临时表中删除它,但显然这需要很多工作。