views:

33

answers:

2

Hi all,

I need to fire some custom javascript validation and then submit my ASP.Net using javascript.

How do I submit the form using javascript? Thanks!

+1  A: 

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.

airmanx86
A: 

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

string postBackJavascript = Page.GetPostBackEventReference(yourControl);

This will return the __doPostBack javascript as a string and you will need place on your page attached to something or you can call the __doPostBack directly on your own with:

__doPostBack(yourControlId,'');

If your 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();
Kelsey
well since you asked.. i am trying to submit using an ASP.Net button.right now i cannot get the button to NOT submit. I have the OnClientClick="validate(); return false;" but doesn't seem to work. Any ideas??
bill
@bill that should work... try removing the `validate()` and see if just `return false;` works. My hunch is your doing something in the `validate()` that is causing that behavious because it looks correct. Maybe start another question and include your `validate()` code.
Kelsey
You may have problems getting the button behaviour to cancel as the events are stacked. So the do postback that was originally there gets fired before your javascript. So you need to clear the original event first before putting yours on top. Otherwise the button will always submit.
Simon