We help IT Professionals succeed at work.

How to respond to PageRequestManager beginRequest

hemminggrp
hemminggrp asked
on
I have an UpdatePanel in an ASP.NET web forms page, and I need to execute some JavaScript when the PageRequestManager beginRequest event fires. How do I do this?

So far, I have a separate js file referenced in my web page, and this js file contains the code below.

The first time a partial page load happens inside the UpdatePanel, the alert is shown. The next time a partial page load happens inside the UpdatePanel, the alert is shown twice. The next time, 3 times -- and so on. How do I make the alert show only once on each partial page load?

If I put pageMgr.remove_beginRequest *after* pageMgr.add_beginRequest, beginRequestHandler is never executed and my alert is never shown, but if I put pageMgr.remove_beginRequest *before* pageMgr.add_beginRequest, it does nothing and I get the duplicate alerts.

The problem seems to be that pageMgr.add_beginRequest adds an event listener each time it is called, thereby stacking up multiple event listeners, which cause my alert to be shown multiple times -- but I can't find this discussed anywhere in my Google searches and how to deal with it.

Very grateful for any help.
function pageLoad(sender, args) {
    var pageMgr = Sys.WebForms.PageRequestManager.getInstance();

    pageMgr.remove_beginRequest(beginRequestHandler);
    pageMgr.add_beginRequest(beginRequestHandler);

    function beginRequestHandler() {
        alert("fired");
    };
};

Open in new window

Comment
Watch Question

GlobaLevelProgrammer

Commented:
see half way down on this link:
http://msdn.microsoft.com/en-us/library/bb311028.aspx


The PageRequestManager class defines events that you can use to customize your page's partial-page rendering. The client infrastructure supports automatic event binding similar to the way that server page events are automatically bound to methods such as Page_Load. The following table lists client events that you can use, and scenarios in which you might handle them. The events are listed in the table in the order in which they are called by the PageRequestManager class.

Thanks. Neither of those answer my question.

I'm trying to figure out how to create just one event listener for the beginRequest event, but "add_beginRequest" seems to create a new event listener each time it is called -- contrary to the MSDN documentation.
I finally figured it out after stumbling across this page:
http://weblogs.asp.net/bleroy/archive/2007/05/16/microsoft-ajax-events-part-1-subscribing.aspx

What the MSDN documentation does not make clear is that code similar to my code below is needed to add and remove an event listener for the event "beginRequest". Otherwise the event handler will be fired an increasing number of times.

There are two important things here that are missing in the MSDN documentation::
(1) Check for async postback in the JavaScript pageLoad event, since this event fires for full and partial and postbacks.
(2) Include "remove_beginRequest" in the JavaScript pageUnload event to stop the event handler being fired an increasing number of times.

Hopefully this will be useful to someone.
function pageLoad(sender, args) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (!prm.get_isInAsyncPostBack()) {
        prm.add_beginRequest(onBeginRequest);
    };
};

function pageUnload(sender, args) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.remove_beginRequest(onBeginRequest);
};

function onBeginRequest() {
    alert("fired");
};

Open in new window

Author

Commented:
Answered my own question.