Passing postBackElement to endRequest handler

It would be nice if an endRequest() handler for the PageRequestManager had a means for getting the element that triggered the post back, like the get_postBackElement() of the BeginRequestEventArgs class. This usually isn’t necessary when you have only one UpdatePanel, but if you have more than one, you may want to take different actions depending on which panel posted.

An easy way to overcome this, is to create a proxy function which can forward the original arguments and some custom ones, in this case the element that triggered the post back. In the beginRequest() handler, we get the element that triggered the post back, and create an inline function that calls the appropriate endHandler() like so..

function _OnBeginRequest( sender, args ) {
  var elm = args.get_postBackElement();
  // Add the correct endRequest handler for this post back
  if ( -1 !=‘CmdSave’) ) {
  } else if ( -1 !=‘CmdEdit’) ) {

Notice that we’re passing the add_endRequest() method of the PageRequestManager an inline function that acts as the handler. This handler then calls another function with the original handler arguments passed by the PageRequestManager, and an additional one which is the element.

A problem with this, is that we’re registering a new handler every time the beginRequest() handler is called, and they’re going to add up if we don’t unregister them. We used an inline function though, which doesn’t have a name so there’s no way to get at it by name. We can however use the “caller” property of the Javascript Function object, which will be the inline function we installed as the handler. So the first thing we do, when our custom handler is called, is unregister the caller from the PageRequestManager.

function _OnCmdSaveEndRequestEx( sender, args, elm ) {
   // Unhook this handler from the PageRequestManager..

function _OnCmdEditEndRequestEx( sender, args, elm ) {

So now we can propagate anything that’s available in the beginRequest() handler to the endRequest() handler.

This entry was posted in .NET, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s