Launching an Ajax ModalPopupExtender from JavaScript

This is an updated version of the earlier (and very popular) Atlas article

It is by far the simplest way to launch an Ajax ModalPopupExtender from javascript.

The groundworks

Add a hidden link for ModalPopup Extender to attach to(which will NEVER be used)

<a href="#" style="display:none;visibility:hidden;" 
   onclick="return false" ID="dummyLink" runat="server">dummy</a>

Add the ID of the hidden link to the ModalPopupExtender

<ajaxToolkit:ModalPopupExtender ID="MyMPE" runat="server"
    TargetControlID="dummyLink"
    PopupControlID="EditorPanel"
    BackgroundCssClass="modalBackground" 
    DropShadow="true" 
    BehaviourID="MyMPE"
    OkControlID="OkButton"
    CancelControlID="CancelButton"> 
</ajaxToolkit:ModalPopupExtender>

Showing the modal popup

Now you've done this, in Javascript, you can display the modal quite easily... here's how.

$find('MyMPE').show();

Hiding the modal popup

Just as easy...

$find('MyMPE').hide();
Author Paul Hayman

Paul is the COO of kwiboo ltd and has more than 20 years IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.

Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Swati Shah said:

I have extender within user controls and this js doesnt work for me. I expected the entender to be renamed prefixed with usercontrol id etc but surprisingly in view source i cant see the extender.

Have you tried this wth user controls?

first user control hosts the extender and target is in usercontrol that is in first user control. Dummy link is sibling as you explained. I just need to show it on client side without any server trips.

15/Jul/2007 03:34 AM

phayman said:

Swati,

Can you get it to work normally inside a User Control? I'm supprised that you can't see the extender when you View Source.

Paul

16/Jul/2007 11:03 AM

Sandaruwan said:

In order for this to work we need to add BehaviourID="MyMPE" also into the ModalPopupExtender.

17/Jul/2007 12:17 PM

Adam said:

Where is a good place of reference to see all available calls for this control?

17/Aug/2007 14:49 PM

Sander said:

Thanks a lot for this!

25/Oct/2007 19:20 PM

sasha.kuperman said:

When I try the javascript programmatic approach the problem I get is that the background isn't disabled when the modal popup is displayed, and I need this.

I use the call to $find('popupbehavior').show() and I also tried calling the hidden button's click() method/event; same problem.

I'd appreciate any help.

-Sorry - I just figured it out. It will disable interaction with the background if (and only if) you use the BackgroundCssClass attribute.

21/Nov/2007 01:25 AM

Mark said:

I was able to get it to work without adding the BehaviorID. Instead I just passed the ID of the ModalPopupExtender to $find, and called show on that. Fluke?

25/Mar/2008 18:01 PM

IrishManInUSA said:

I have tried to do this and have not been successfull in making it work. Here is my code.

    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">

        </asp:ScriptManager>







        <h1>Demo</h1>

        <h2>Launching a modal popup extender from javascript</h2>



      <a href="#" onclick="return showModal();">Show</a>

        <msAjax:ModalPopupExtender ID="ModalPopupExtender1" runat="server"

         TargetControlID="dummylink"

         popupcontrolid ="Panel1"

         backgroundcssclass="modalBackground"

         dropshadow="True"

         okcontrolid="okbutton"

         onokscript="onOk()"

         cancelControlId="CancelButton"

         BehaviorId = "mpe"

        >



        </msAjax:ModalPopupExtender>





        <div style="display:none; visibility:hidden;">

        <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup">

            <h1>Here it is!</h1>

            <p>Here is the model popup!!</p>

            <center>

                <asp:Button ID="OkButton" runat="server" Text="OK"></asp:Button>

                <asp:Button ID="CancelButton" runat="server" Text="Cancel"></asp:Button>

            </center>

        </asp:Panel>

        </div>        

<div style="display:none; visibility:hidden;">

    <a href="#" style="display:none;visibility:hidden;" 

   onclick="return false" ID="dummyLink" runat="server">dummy</a>

        </div>



        <script type="text/javascript">

            function onOk() { return true; }



            function showModal() { 

var myBehavior = $find('mpe');

myBehavior.show();

$find('mpe').show;

}

I don't see any popup, so not exactly sure why this is not working for me.

10/Apr/2008 19:50 PM

Greg said:

Remove the div tag hiding it ... works fine then

18/Apr/2008 19:02 PM

Bala said:

if you want to call the MPE directly from your codebehind, you could call

MPE.show();

03/Jun/2008 17:38 PM

Rama charan said:

that helped me a lot .Thanks a lot for your help!!!

21/Jan/2009 14:27 PM

Jerry Nixon said:

Perfect. Thanks.

20/Mar/2009 03:51 AM

Jerry Nixon said:

Oops. BehaviourID="MyMPE" is a typo.

What is that, English?

Try BehaviorID="MyMPE" and presto.

20/Mar/2009 04:23 AM

abhijit.mith@gmail.com said:

I need to open a modal popup on click of a hyperlink and also a certain validation which i perform in the onclick event of the hyperlink.

However, even if the validation results in false, the modal pop up is displayed which is incorrect. Can you suggest a way around without using an additional hidden control

17/Aug/2009 13:36 PM

abhijit said:

i need to open a modal popup on click of a hyperlink. However, I would also like to perform a validation before displaying the modal popup. The modal popup should be displayed only when validation function returns true.

<a id="aBookMachines" runat="server" enableviewstate="false" href="#" onclick="javascript:return BookMachines();">book machines</a>

function BookMachines()

{

        if (i == 0)

                return true;

        else

                return false;

}

If false then modal popup should not be displayed.

17/Aug/2009 13:41 PM

jabits said:

Thanks much for the tip. Works great!

- jabits (jabits@gmail.com)

19/May/2010 17:55 PM

Bruce Talcott said:

Thanks! It´s working now

16/Dec/2010 03:11 AM

Shaun said:

Thanks, this has helped me quite alot.

20/Jan/2011 11:23 AM

Brian W. said:

Nice and easy. Works like a charm. thanks.

11/Mar/2011 21:43 PM

Prabhu Kumar.T said:

Hi Paul ,

i am using a ajax modalpopup control in gridview, when i click delete link button in my grid , i am showing a popup that r u sure todelete?

if i click ok button record will be deleted, if i click cancel button record will not be delete.up to this i achieved but, when i click "ok"

button one more modal popup must come asking "Reason to delete?" and i have dropdownlist control in it,i have to select any one from dropdown and click ok then only record must be deleted. i faied in achieving this function can u pls help me ..........

it is important for me.

Thanks in advance..................

09/Aug/2011 07:57 AM

ritu said:

this helps me alot...thanks to all........

28/Jun/2012 13:41 PM

Reg Modal popup said:

Hi, i have a grid in a modal popup,this grid helps add a new record and edit a record. while editing a record, need to clear the values before it is closed. I have achieved this on server side, but the performance is very slow as it has got so many fields. I need to clear the values of text boxes and labels when i say cancel and the popup should hide or disappear. It would be great to know if there is a simple way to do it.

Thanks

Pavan

10/Sep/2012 07:45 AM

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.