You are not Logged in
Would you like to Login or Register

Today is: Friday, 21 November, 2008
Check this months hot topics

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();
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : Tuesday, 10 July, 2007

Paul is the COO of kwiboo ltd consultant and has more than a decade of 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.

July 15, 2007 - 3: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

July 16, 2007 - 11:03 AM

Sandaruwan said:

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

July 17, 2007 - 12:17 PM

Adam said:

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

August 17, 2007 - 2:49 PM

Sander said:

Thanks a lot for this!

October 25, 2007 - 7: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.

November 21, 2007 - 1: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?

March 25, 2008 - 6: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.

April 10, 2008 - 7:50 PM

Greg said:

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

April 18, 2008 - 7:02 PM

Bala said:

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

MPE.show();

June 03, 2008 - 5:38 PM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views