Skip to content Skip to sidebar Skip to footer

In ASP.NET, How To Hook Postback Event On Clientside

I want to run a code on every postback on client-side. Is there an event or something to do that?

Solution 1:

If you just handle a form's submit event, you won't necessarily catch every __doPostBack(). A __doPostBack() that triggers an UpdatePanel refresh doesn't submit the form, for example.

One way that you can make sure you know about every call to __doPostBack() is to redefine it:

var oldDoPostBack = __doPostBack;

function __doPostBack(eventTarget, eventArgs) {
  // Your code here, which will execute before __doPostBack,
  //  and could conditionally cancel it by returning false.

  oldDoPostBack(eventTarget, eventArgs);
}

Just be sure to do that near the end of the page or in a document.ready type event, so you can be sure __doPostBack is defined when you attempt this.


Solution 2:

The way asp.net works is that the the whole page is a form. So to do something client side before the form is submitted (which is what the postback event really is), I would attach to the form submit event, return false to prevent the submission of the form, perform your client side code and then submit the form using javascript.

You can do this with jQuery easily: http://api.jquery.com/submit/

Let me know if you need more help.


Solution 3:

Dave Ward's answer almost worked for me, but for whatever reason (Telerik?), it created an infinite loop of calls to __doPostBack. So instead, I used the following code:

function __doPostBack_Custom(eventTarget, eventArgs) {
    // Your code here, which will execute before __doPostBack,
    //  and could conditionally cancel it by returning false.

    oldDoPostBack(eventTarget, eventArgs);
}
var oldDoPostBack = window.__doPostBack;
window.__doPostBack = __doPostBack_Custom;

Solution 4:

There is a javascript __doPostBack() method on every aspx page, that is generated by ASP.NET. So you can use it


Post a Comment for "In ASP.NET, How To Hook Postback Event On Clientside"