How to intercept any postback in a page? – ASP.NET

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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).

Method 4

Page.IsPostBack is your friend.

Method 5

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:
            }
        }
    }

Method 6

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

if (Page.IsPostBack)
    { 
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x