12

I want to intercept any postbacks in the current page BEFORE it occurs . I want to do some custom manipulation before a postback is served. Any ideas how to do that?

NLV
  • 21,141
  • 40
  • 118
  • 183
  • Please be more specific: what do you mean by "before the postback"? – John Saunders Feb 10 '10 at 05:39
  • You say in your requirements that you want to intercept a postback *when* it occurs, yet in your comments to the answerers you say you want to intercept it *before*. Which do you want? Are you wanting to do it client side or server side? – slugster Feb 10 '10 at 05:40
  • Sorry for the confusion. Edited the question to make it clear. I want to intercept it before it occurs. I want to do it in the server side. Though, i may be interested to know how to do it in the client side too. – NLV Feb 10 '10 at 05:46

6 Answers6

22

There's a couple of things you can do to intercept a postback on the client.

The __doPostBack function looks like this:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

Notice that it calls "theForm.onsubmit()" before actually doing the postback. This means that if you assign your form an onsubmit javascript function, it will always be called before every postback.

<form id="form1" runat="server" onsubmit="return myFunction()">

Alternately, you can actually override the __doPostBack function and replace it with your own. This is an old trick that was used back in ASP.Net 1.0 days.

var __original= __doPostBack;
__doPostBack = myFunction();

This replaces the __doPostBack function with your own, and you can call the original from your new one.

womp
  • 115,835
  • 26
  • 236
  • 269
7

Use the following options

All options works with ajax-enabled-forms and simple forms.
return false to cancel submit within any submit-handler.

Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "submit-handler", "alert(\"On PostBack\");");

Equivalent javascript --don't use this code with previous code simultaneously

// Modify your form tag like this
<form onsubmit="javascript:return submit_handler();" ...>

// Add this script tag within head tag
<script type="text/javascript">
    function submit_handler() {
        // your javascript codes
        // return false to cancel
        return true; // it's really important to return true if you don't want to cancel
    }
</script>

And if you want complete control over __doPostBack put this script next to your form tag

<script type="text/javascript">
    var default__doPostBack;
    default__doPostBack = __doPostBack;
    __doPostBack = function (eventTarget, eventArgument) {
        // your javascript codes
        alert('Bye __doPostBack');
        default__doPostBack.call(this, eventTarget, eventArgument);
    }
</script>

Tested with ASP.NET 4.0

Beygi
  • 1,918
  • 1
  • 16
  • 22
2

To get the postback before a page does, you can create an HttpHandler and implement the ProcessRequest function.

Check this Scott Hanselman link for a good blog post on how to do it (including sample code).

slugster
  • 49,403
  • 14
  • 95
  • 145
1

Page.IsPostBack is your friend.

Saar
  • 8,286
  • 5
  • 30
  • 32
  • 1
    I want to do some manipulations before a postback is served. Not after the postback has happened. Hope you get it. – NLV Feb 10 '10 at 05:34
1

You can check for a postback in one of the page events for your form.

If you want to take some action on postback that involves creating controls or manipulating viewstate then you may want to come in on an earlier event like Page_Init.

Try this:

protected void Page_Init(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            //Check for your conditions here, 

            if (Page.IsAsync)
            {
                //also you may want to handle Async callbacks too:
            }
        }
    }
Tj Kellie
  • 6,336
  • 2
  • 31
  • 40
  • I want to do some manipulations before a postback is served. Not after the postback has happened. Hope you get it. – NLV Feb 10 '10 at 05:35
0

not sure, but I think you are looking for..

if (Page.IsPostBack)
    { 
    }
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • I want to do some manipulations before a postback is served. Not after the postback has happened. Hope you get it. – NLV Feb 10 '10 at 05:33