How to specify javascript to run when ModalPopupExtender is shown

By : slolife
Source: Stackoverflow.com
Question!

The ASP.NET AJAX ModalPopupExtender has OnCancelScript and OnOkScript properties, but it doesn't seem to have an OnShowScript property. I'd like to specify a javascript function to run each time the popup is shown.

In past situations, I set the TargetControlID to a dummy control and provide my own control that first does some JS code and then uses the JS methods to show the popup. But in this case, I am showing the popup from both client and server side code.

Anyone know of a way to do this?

BTW, I needed this because I have a textbox in the modal that I want to make a TinyMCE editor. But the TinyMCE init script doesn't work on invisible textboxes, so I had to find a way to run it at the time the modal was shown

By : slolife


Answers
var launch = false;

function launchModal() {
    launch = true;
}

function pageLoad() {
    if (launch) {
          var ModalPedimento = $find('ModalPopupExtender_Pedimento');
          ModalPedimento.show();
          ModalPedimento.add_shown(SetFocus);
    }
}

function SetFocus() {
    $get('TriggerClientId').focus();
}


For two modal forms:

var launch = false;
var NameObject = '';

function launchModal(ModalPopupExtender) {
    launch = true;
    NameObject = ModalPopupExtender;
}

function pageLoad() {
    if (launch) {
        var ModalObject = $find(NameObject);
        ModalObject.show();
        ModalObject.add_shown(SetFocus);
                }
} 

function SetFocus() {
    $get('TriggerClientId').focus();
}

Server side: behand

protected void btnNuevo_Click(object sender, EventArgs e)
{
    //Para recuperar el formulario modal desde el lado del sercidor
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "<script>launchModal('" + ModalPopupExtender_Factura.ID.ToString() + "');</script>", false);
}


Here's a simple way to do it in markup:

<ajaxToolkit:ModalPopupExtender 
                ID="ModalPopupExtender2" runat="server" 
                TargetControlID="lnk_OpenGame" 
                PopupControlID="Panel1" 
                BehaviorID="SilverPracticeBehaviorID"  >
            <Animations>
                <OnShown>
                     <ScriptAction Script="InitializeGame();" />  
                </OnShown>
            </Animations>                
</ajaxToolkit:ModalPopupExtender>
By : John Ward


This video can help you solving your question :)
By: admin