Auto Submit Form in ASP.NET With Javascript

Not sure if this is the best way to do this, but this is what I do. Say you want to auto submit a form on an aspx page. You can call document.formname.submit(); from the body onLoad event and it will submit, but ASP.net will automatically post it back to itself.

I tried added buttons with different postbackUrl’s, clicking the submit button in javascript, etc to no avail.

First, what I had to do was put on my ASP Classic hat. Look at your <form> and remove the runat=”server”

Then, you can say document.formname.submit(); and it will submit your form.

How do you pass data though?

Well, you have to created input fields, probably hidden like

<input type=”hidden” name=”blah” id=”blah” value=”<%= Request.QueryString[“myvalue”] %>” />

Then you can pass data from another page or whatever and auto submit your form.

It would be nice if you could say in your Page_Load() something like

 

btnSubmit.Click(); and it would automatically click it and submit your form, but that doesn’t seem to be available at all.

Like I said, there is probably a better way to do this, and it shouldn’t be this complicated, but a few minutes googling for answers left me up in the air. Funny how something that is so easy in ASP Classic turns out to be harder in ASP.NET. ASP.NET wants you to really post back to the same page by default. It hijacks the “action” attribute on the form no matter what with runat=”server on there.

Advertisements

4 thoughts on “Auto Submit Form in ASP.NET With Javascript”

  1. This solution will tough to me because my application is already completed. I can not make this change at this moment.

    do you have any simple solution to avoid the IE refresh alert which comes after the page submit while saving the data to database. ?

    I want to bypass only this given alert. nothing more that.
    Alert:
    —————————
    Windows Internet Explorer
    —————————
    To display the webpage again, Internet Explorer needs to
    resend the information you’ve previously submitted.

    If you were making a purchase, you should click Cancel to
    avoid a duplicate transaction. Otherwise, click Retry to display
    the webpage again.
    —————————
    Retry Cancel
    —————————

    I am not getting that how it is getting resubmitted.

    There can be small trick or something for this which I am trying to find to bypass this message.

    Thanks

    Kamleshkumar Gujarathi
    kamlesh.gujarathi@indiatimes.com

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s