Run JavaScript when Ajax modal popup opens

Let’s say if you want to run some JavaScript method right after Ajax modal popup opens, we probably think injecting JavaScript method into page like this.

if (!Page.IsStartupScriptRegistered(“PickerInit”))
Page.RegisterStartupScript(“pickerInit”,javascriptString);

The JavaScript we inject using above method will be called on every page loads. But our above example doesn’t work for Ajax modal popup because it does asynchronous post back so that JavaScript method injected using above example won’t be called.

But there is a delegate(add_shown) for Ajax modal popup control where you can wrap your method in and it will be called once MPE opens. Here it is how you can use that

<script type=”textJavaScript”>

function pageLoad(){
$find(‘MyMPEBID’).add_shown(onMPEShown);

// note: MYMPEID is behaviorID of MPE.

}
function onMPEShown(sender,args){
//some javascript

}

</script>

Those who are not aware of pageLoad method defined in above example, It’s load event that gets triggered when page loads. It’s entirely different topic but if you are interested to read more about ASP.NET AJAX client life cycle here is the link. ASP.NET AJAX Client Life-Cycle Events.

Please leave a comment or question you have.

Posted in Client Side Scripting, Microsoft Technology Tagged with: ,
One comment on “Run JavaScript when Ajax modal popup opens
  1. John Ward says:

    Here’s another way to do it:

Ads