We help IT Professionals succeed at work.
Get Started

How to respond to PageRequestManager beginRequest

hemminggrp
hemminggrp asked
on
2,296 Views
Last Modified: 2012-05-12
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
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE