1

For my particular problem i need to find a way on the client side which UpdatePanel has been updated. To figure that out on the ServerSide is not quite hard (there's a plentitude of SO Questions about this), but i'd prefer a more generic solution for the client side.

I tried the following things:

  • I inspected the PageRequestManager client object without success
  • I checked the value of $("#__EVENTTARGET").val() in hope for any ID to work with (always empty)

My scenario is that i've written a script for a control which is executed via Sys.Application.add_init, which is why EVENTTARGET hidden field may not be filled yet.

My idea would be mixing techniques of ASP.MVC (IHttpModule) and Webforms (like here) to get a clean and generic solution.

Thus my plan is to access params for the request and inject a JS into the response which declares the eventtarget for me.

What i am wondering about now is: Is this a good solution or am i unaware of some existing functionality to figure out which updatepanel was updated? (Or is this an ugly solution and you know a better way to do this)

Community
  • 1
  • 1
Dbl
  • 5,634
  • 3
  • 41
  • 66
  • Has been updated means which panel causes the postback? – Tim Schmelter Jul 25 '14 at 13:42
  • @TimSchmelter Yes. Basically i have a nested structure of UpdatePanels and some Telerik Controls and need to figure out, which one has been updated to limit my script calls depending on that information – Dbl Jul 25 '14 at 13:49

2 Answers2

1

One way I know to get the names of update panels that are going to be updated after postback is to use Sys.WebForms.PageRequestManager pageLoading Event.

From MSDN:

Raised after the response from the server to an asynchronous postback is received but before any content on the page is updated.

To get a list of update panels you need to call get_panelsUpdating function:

<script>
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);

    function pageLoadingHandler(sender, args) {

        console.log("Following panels are going to be updated:");
        var panelsToBeUpdated = args.get_panelsUpdating();
        for (var index in panelsToBeUpdated) {
            console.log(panelsToBeUpdated[index].id);
        }
    }
</script>

Alternatively you can intercept asynchronous postback before anything would be sent to server using Sys.WebForms.PageRequestManager initializeRequest Event. I think it would be possible to save the list of update panels to some variable/field/whatever which is available for your script.

<script>
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initializeRequestHandler);

    function initializeRequestHandler(sender, args) {

        var panelsToBeUpdated = args.get_updatePanelsToUpdate(); // This function returns client ids of update panels
        for (var index in panelsToBeUpdated) {
            console.log(panelsToBeUpdated);
        }
    }
</script>

Anyways, almost all page event handlers sender parameters are having _postBackSettings field which could be used to get the list of panel ids affected by postback:

sender._postBackSettings.panelsToUpdate

The only downside of this field is _ in its name which means that it is "private" and should be avoided to be accessed directly.

Community
  • 1
  • 1
Alexander Manekovskiy
  • 3,185
  • 1
  • 25
  • 34
  • helpful but not viable for my scenario. i've used this method before in another scenario. unfortunately this time i am bound to getting it to work within add_init and there's no similar method to call on the args of the handler. – Dbl Jul 27 '14 at 12:24
  • @AndreasMüller maybe I'm mistaken but isn't `add_init` handler called only once, i.e. the first time page loaded and not after each async postback? – Alexander Manekovskiy Jul 27 '14 at 17:43
  • You are correct about that. However in my scenario i bind it to init, as well as firing it at render time, making my script run at both times. Unfortunately this is part of the reason why i need to find a way to figure out the updatepanel in question. (Also i can't use add_load, because some of the controls inside my control use that event and i need my script to be executed before that) – Dbl Jul 28 '14 at 08:01
  • @AndreasMüller please see updated answer. New idea is to save list of update panels right before postback and then retrieve them in your script. – Alexander Manekovskiy Jul 29 '14 at 07:04
0

In the meantime i modified my production solution but for this particular issue you can indeed implement IHttpModule and inject code to update information in javascript, in case someone stumbles into a similar scenario.

Dbl
  • 5,634
  • 3
  • 41
  • 66