Alex Rivera | Logout

ASP.NET postback with jQuery?

Asked 2009-04-15T12:06:52.870
16

I have a ASP.NET button but recently, I replaced it with a standard HTML button ... What I need to do is a postback to an ASP.NET page and ensure a method is called.

The previous button was an ASP.NET button, so I had this event:

Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    UtilTMP.DisposeObjects()
    Server.Transfer("~\Forms\test.aspx", True)
End 

But I was using a button with a JavaScript ALERT and I recently changed to a jQuery UI Modal dialog but it doesn't wait for me to answer the question.. the postback happenes immediatly ... so I decided to change to a standard HTML button ... but I need to postback to the ASP.NET page and call a method like.

If I just postback it won't call the cleanup

Protected Sub Cleanup()
    UtilTMP.DisposeObjects()
    Server.Transfer("~\Forms\test.aspx", True)
End 
Edit
Report

2 Answers

4

You need to set the __EVENTTARGET hidden field to an appropriate value if you want to trigger the event handler on postback. I would do it a different way, however. Put ASP buttons in your modal dialog that have the event handler associated with them. Have the click handler that pops up the dialog return false (so that the postback doesn't happen from that button click). This way your form is posted back from an ASP button and the handler, including the client-side hidden field setting, is invoked automatically.

answered 2009-04-15T12:16:58.790
1

I came across this post through google so I suspect others will too. I tried the methods on here which didn't work for me. I will explain my solution, but first an overview of what I'm trying to do.

I have a form that uses jQuery ajax to post data to my webservice which in turn updates my database. I have an image upload form, for security reason you can't get the full file path to use in the ajax call to pass to the webservice... so it has to be done with a regular asp.net postback. So my dilemma is how to do both. What I did was use my ajax function to update the database, once I got a success back from the webservice I tell the form to do a postback to a certain method in the codebehind and then redirect to wherever. $("#Button1").click(function () { javascript: __doPostBack('ctl00$ContentPlaceHolder1$atest', '') });

This is essentially exactly the same as assigning a click function to an asp.net control, except instead of clicking the button I am telling javascript to execute it. atest would be the ID i assigned to the asp.net control.
Note: the <%=myclientid.ClientID %> will not work here.

answered 2010-12-20T04:45:24.503

Your Answer