ASP.NET postback with jQuery?

2019-01-22 03:38发布

问题:

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 

回答1:

While you can do Postbacks with JQuery, it might be better to call a web method (web service that is in your page). The call could also be faster because you are not posting the entire page ViewState.



回答2:

See if this helps: http://www.codeproject.com/KB/aspnet/sample.aspx.

Basically, you declare a dummy anchor tag:

<a id="anchorId" runat="server" onclick="return true" onserverclick="foo"></a> 

In your code behind, you need to declare a foo method:

protected void foo(object sender, EventArgs e)
{
    // Do something here
}

Then you can invoke this anchor's onclick function with this javascript:

document.getElementById('anchorId').click()


回答3:

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.



回答4:

Check out this article

http://www.deviantpoint.com/post/2009/03/12/Using-jQuery-UI-Dialogs-for-confirmation-windows.aspx

The basic idea is you place the call back function in a hidden field and run an eval on the $(this).dialog('close');

I used this to have it work in a Gridview, so if you want to know how to do that leave a comment.



回答5:

You should be able to stop the postback with whatever JS is attached to the button onclick event, regardless of whether it is a WebControl or a standar HTML button, by returning false.

E.g. <input type="image" id="test" onclick="doWhatever();return false;" />



回答6:

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.