Dbl
Dbl

Reputation: 5894

How to figure out which updatepanel was updated on clientside

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:

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)

Upvotes: 1

Views: 371

Answers (2)

Dbl
Dbl

Reputation: 5894

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.

Upvotes: 0

Alexander Manekovskiy
Alexander Manekovskiy

Reputation: 3203

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.

Upvotes: 1

Related Questions