7

I am refreshing an UpdatePanel with Javscript by calling a method like so:

reloadDropDown = function (newValue)
{
  __doPostBack("DropDown1", "");
  selectNewValueInDropDown(newValue);
}

Inside my UpdatePanel is a <select> box that I need to select an <option> with a newValue. My problem is that my selectNewValueInDropDown method is being called prior to the __doPostBack from completing. Is there a way I can "wait" for the postback before calling my selectNewValueInDropDown method?

rick schott
  • 21,012
  • 5
  • 52
  • 81
Brian David Berman
  • 7,514
  • 26
  • 77
  • 144

2 Answers2

11

To make my comment more concrete, here's the idea:

reloadDropDown = function (newValue)
{
    var requestManager = Sys.WebForms.PageRequestManager.getInstance();

    function EndRequestHandler(sender, args) {
        // Here's where you get to run your code!
        selectNewValueInDropDown(newValue);

        requestManager.remove_endRequest(EndRequestHandler);
    }
    requestManager.add_endRequest(EndRequestHandler);

    __doPostBack("DropDown1", "");
}

Of course, you probably want to handle race conditions where two requests overlap. To handle that, you would need to keep track of which handler is for which request. You could use something like ScriptManager.RegisterDataItem on the server side, or call args.get_panelsUpdated() and check to see if the panel you're interested it was updated.

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
6
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args)
{
    //
}
function EndRequestHandler(sender, args)
{
    //request is done
}
rick schott
  • 21,012
  • 5
  • 52
  • 81
  • But where would my parameter coming into play? I also would need a single entry point method. – Brian David Berman Sep 30 '11 at 20:35
  • You will have to get crafty with JavaScript and set some state variables to know when to fire your method. When you hijack the framework/process it's on you. – rick schott Sep 30 '11 at 20:37
  • @BrianDavidBerman: You could write a function that calls `selectNewValueInDropDown(newValue)` then calls `Sys.WebForms...remove_endRequest(theFunction)` to remove itself as an event handler. – Matthew Crumley Sep 30 '11 at 20:47
  • any idea why MS chose not to include a callback parameter in __doPostBack? is there a technical reason? or just oversight? – greg Jan 10 '15 at 16:30