Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 512
bill Avatar asked Jul 13 '10 23:07

bill


People also ask

Where do I put JavaScript code in asp net?

You can write the JavaScript code in the ASP.Net Page in the following sections. You can write the JavaScript code in the head section, it is the recommended way to write the code, the code must be enclosed in the following syntax: <head id="Head1" runat="server"> <script type="text/javascript">

How import JavaScript file in asp net?

Simply open the file in the browser, press "view source" and use the JS path in the URL to see if it opens. Your syntax is (more or less) correct.

Can you use .NET with JavaScript?

NET code can do virtually anything when combined with HTML and CSS but JavaScript is still very important and the reason lies in the different domains in which each language operates.

How can use client side validation in asp net?

Some ASP.NET server controls use client side scripting to provide response to the users without posting back to the server. For example, the validation controls. Apart from these scripts, the Button control has a property OnClientClick, which allows executing client-side script, when the button is clicked.


2 Answers

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();
like image 124
Kelsey Avatar answered Oct 19 '22 06:10

Kelsey


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.

like image 22
airmanx86 Avatar answered Oct 19 '22 08:10

airmanx86