Today I want to talk about a strange issue and my workaround.

Scenario: you have an UpdatePanel and a button outside it. You want upon an asyncronous postback to execute some custom javascript on the button outside the UpdatePanel. Normally this would be done by inserting a javascript from codebehind, either by using ClientScriptManager.RegisterScript or by adding a literal control which is actually a simple script. Well, this does not work during asyncronous postbacks, or at least not as it normally should.

The document.GetElementById is returning always null when calling from an updatepanel with an ID of an element outside the updatepanel. So how to run that javascript?

Subscribe a javascript function to the EndRequest event of the PageRequestManager:

<script language=”javascript” type=”text/javascript”>
   function EndRequestHandler()
   {
      
document.getElementById(‘myButton’).style.visibility = “visible”;
   }
   Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
</script>

What this does is to define a function and then set it to be executed after successfully asyncronous postback.

Cheers!

Leave a Reply