[英]How to submit ASP.NET with JavaScript after custom validation
I need to fire some custom JavaScript validation and then submit my ASP.NET using JavaScript. 我需要激活一些自定义JavaScript验证,然后使用JavaScript提交我的ASP.NET。
How do I submit the form using JavaScript? 如何使用JavaScript提交表单?
To do a postback via JavaScript you can call the following server side to create the JavaScript code for you: 要通过JavaScript进行回发,您可以调用以下服务器端为您创建JavaScript代码:
string postBackJavascript = Page.GetPostBackEventReference(yourControl);
This will return the __doPostBack
JavaScript code as a string, and you will need place it on your page attached to something or you can call the __doPostBack
directly on your own with: 这会将
__doPostBack
JavaScript代码作为字符串返回,您需要将其放在附加到某些内容的页面上,或者您可以直接使用以下命令调用__doPostBack
:
__doPostBack(yourControlId,'');
If you're doing it yourself and not using Page.GetPostBackEventReference
then make sure to get the ClientID
for the control that triggered the validation, like: 如果您自己进行并且不使用
Page.GetPostBackEventReference
那么请确保获取触发验证的控件的ClientID
,例如:
__doPostBack('<%= yourControl.ClientID %>','');
EDIT: After re-reading your question you didn't say you wanted to trigger the postback based on an ASP.NET control, you might not even be using any ASP.NET controls so in that case if you want to just do a vanilla postback you can do: 编辑:重新阅读你的问题,你没有说你想要基于ASP.NET控件触发回发,你可能甚至没有使用任何ASP.NET控件,所以在这种情况下,如果你想做一个香草回发你可以这样做:
document.forms[0].submit();
It should be as simple as 它应该如此简单
document.forms[0].submit();
Provided, you only have one form on the page, or else you need to use the form name instead of the index 0. 如果您在页面上只有一个表单,或者您需要使用表单名称而不是索引0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.