自定义验证后如何使用 JavaScript 提交 ASP.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3242365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:49:50  来源:igfitidea点击:

How to submit ASP.NET with JavaScript after custom validation

javascriptasp.netform-submit

提问by bill

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 提交表单?

回答by Kelsey

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 __doPostBackJavaScript code as a string, and you will need place it on your page attached to something or you can call the __doPostBackdirectly on your own with:

这将以__doPostBack字符串形式返回JavaScript 代码,您需要将它放在您的页面上附加到某些东西,或者您可以__doPostBack直接调用:

__doPostBack(yourControlId,'');

If you're doing it yourself and not using Page.GetPostBackEventReferencethen make sure to get the ClientIDfor 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();

回答by airmanx86

If you want to post back, you can use __doPostBack()that ASP.NET put into the <form>. Take a look at this link. If you want to submit another form just call .submit()on the form element.

如果要回发,可以使用__doPostBack()ASP.NET 放入<form>. 看看这个链接。如果您想提交另一个表单,只需调用.submit()表单元素。

回答by Danish Khan

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。