Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET postback with jQuery?

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 
like image 734
mark smith Avatar asked Apr 15 '09 12:04

mark smith


People also ask

What is postback in jQuery?

Learning jQuery "PostBack is the name given to the process of submitting an ASP.NET page to the server for processing .". Once there is a postback (on click of button) that particular code should never ever get called again.

How do you create a postback in JavaScript?

How to Raise a Postback from JavaScript? To do this, we need to just call the __doPostBack() function from our javascript code. When the above function is called, it will raise a postback to server.

What postback means?

A postback is the exchange of information between servers to report a user's action on a website, network, or app.


2 Answers

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()
like image 129
David Avatar answered Oct 17 '22 03:10

David


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.

like image 27
Chris Brandsma Avatar answered Oct 17 '22 03:10

Chris Brandsma