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.

How do I submit the form using JavaScript?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To do a postback via JavaScript you can call the following server side to create the JavaScript code for you:

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(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:

__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:

document.forms[0].submit();

Method 2

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.

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x