GeekZilla
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();
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.
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
Sandaruwan
said:
In order for this to work we need to add BehaviourID="MyMPE" also into the ModalPopupExtender.
Adam
said:
Where is a good place of reference to see all available calls for this control?
Sander
said:
Thanks a lot for this!
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.
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?
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.
Greg
said:
Remove the div tag hiding it ... works fine then
Bala
said:
if you want to call the MPE directly from your codebehind, you could call
MPE.show();
Rama charan
said:
that helped me a lot .Thanks a lot for your help!!!
Jerry Nixon
said:
Perfect. Thanks.
Jerry Nixon
said:
Oops. BehaviourID="MyMPE" is a typo.
What is that, English?
Try BehaviorID="MyMPE" and presto.
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
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.
jabits
said:
Thanks much for the tip. Works great!
- jabits (jabits@gmail.com)
Bruce Talcott
said:
Thanks! It´s working now
Shaun
said:
Thanks, this has helped me quite alot.
Brian W.
said:
Nice and easy. Works like a charm. thanks.
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..................
ritu
said:
this helps me alot...thanks to all........
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