ASP.NET Ajax UpdatePanelNotifier: get notified of partial updates
UPDATE: The control has been added new features, check it out here.
Sometime ago I wrote a post about how to get the ID of the UpdatePanel which triggered an asynchronous postback using the ATLAS July CTP and how to execute a custom Javascript function whenever the postback initiated and when it sent back its results. With the release of ASP.NET Ajax Extensions Beta the API has changed a lot and that code is no longer working, while he new API exposes now a whole new set of facilities which make it really easier to achieve the same functionality.
I wrote the control I once called UpdateProgressSignup from scratch and changed its name to a more meaningful one: UpdatePanelNotifier. What this control does is very similar to the UpdateProgress standard control, in that it executes some client-side code whenever a partial update is triggered and when the results are returned from the server. The difference is that instead of executing a determined function (showing and hiding a <div> element by manipulating the DOM) you can bind your custom javascript functions to those events, which in turn receive as input parameter the concerned UpdatePanel DOM element so that you can directly act on it, making it blink when the beginRequest event is fired and then shake when the endRequestEvent is fired, for example.
The control exposes the same properties as the standard UpdateProgress control beyond two new properties which let you enter the name of the functions you want to execute:
- OnBeginRequest
- OnEndRequest
Each function executed in response to those events will be passed the DOM element of the UpdatePanel concerned in the partial update, so that if you set the OnBeginRequest property to "alertPanelId", you'll have to write a javascript function called "alertPanelId" in you page which will have the following signature and will eventually do something like this:
function alertPanelId(panelElement)
{
alert(panelElement.id);
}
Note that the bound functions will be called only when the partial refresh is triggered by a control inside the UpdatePanel and not when it's an external trigger doing it, since in this case it's not yet possible to retrieve the Id(s) of the UpdatePanel(s) which will be refreshed in response to that action. Anyhow it seems that the ASP.NET Ajax Team is working on it and will eventually provide this facility in the final release.
You can watch a short screencast to get started fast. The source code is available on my main project's svn repository.