add_shown & add_hiding ModalPopupExtender Events

 

 

In this topic, I’ll discuss the Client events we usually need while using ModalPopupExtender. The add_shown fires when the ModalPopupExtender had shown and add_hiding fires when the user cancels it by CancelControlID,note that it fires before hiding the modal.

They are useful in many cases, for example may you need to set focus to specific Textbox when the user display the modal, or if you need to reset the controls values inside the Modal after it has been hidden.

To declare Client event either in pageLoad javascript function or you can attach the function by Sys.Application.add_load like this:

Sys.Application.add_load(modalInit);   
  
  
    function modalInit()  
    {  
        var modalPopup = $find('mpeID');   
        modalPopup.add_hiding(onHiding);  
    }  
      
    function onHiding(sender, args)  
    {  
    }  

 

I’ll use the first way in the current example. So lets start with the illustration:

 

1- In this example am using simple panel which contain UserName and Password Textboxes besides submit and cancel buttons, this Panel will be used as PopupControlID in the ModalPopupExtender :

 <asp:Panel ID="panModal" runat="server" Height="180px" Width="300px" style="display:none" CssClass="ModalWindow">
        <table width="100%" >
            <tr>
                <td>
                   User Name
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Password
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            
        </table>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
    </asp:Panel>

 

You can use this simple style for the Panel :

   <style type="text/css">
   .ModalWindow
{
   border: solid;
  border-width:3px;
  background:#f0f0f0; 
}
   </style>

 

2- Create the view button (TargetControlID) as you know this contain the ID of the element that activates the modal popup:

 <asp:Button ID="btnView" runat="server" Text="View" />

 

3-Add the ModalPopupExtender ,moreover don’t forget to add the ScriptManager:

 <asp:ScriptManager ID="ScriptManager1" runat="server"/>
  
    <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnView"
        PopupControlID="panModal" CancelControlID="btnCancel"/>

 

 

4-In the pageLoad javascript function inside add_shown event set the focus on the txtName , and inside add_hiding reset the two Textboxes.

<script language="javascript" type="text/javascript">
       function pageLoad() {
           $find('ModalPopupExtender1').add_shown(function() {
               alert('add_shown event fires');
               $get('<%=txtName.ClientID%>').focus();
 
           });
 
           $find('ModalPopupExtender1').add_hiding(function() {
               alert('add_hiding event fires');
               $get('<%=txtName.ClientID%>').value = "";
               $get('<%=txtPassword.ClientID%>').value = "";
 
           });
       } 
 
</script>  

 

I’ve added the two alerts just to let you show when the event fires.

 

Hope this simple example show you the benefit and how to use these events.

Published Thursday, April 15, 2010 1:14 AM by Yousef_Jadallah

Comments

# Twitter Trackbacks for add_shown &amp; add_hiding ModalPopupExtender Events - Yousef Jadallah's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 add_shown &amp; add_hiding ModalPopupExtender Events - Yousef Jadallah's Blog         [asp.net]        on Topsy.com

# re: add_shown & add_hiding ModalPopupExtender Events

Wednesday, December 15, 2010 3:03 PM by DAStelow

This has been *very* useful.  Thanks for posting.

# links for 2010-12-15 &laquo; dstelow notes&#8230;

Wednesday, December 15, 2010 6:22 PM by links for 2010-12-15 « dstelow notes…

Pingback from  links for 2010-12-15 &laquo;  dstelow notes&#8230;

# re: add_shown & add_hiding ModalPopupExtender Events

Wednesday, March 02, 2011 1:59 AM by asp.net beginner

Very useful. Thanks a lot.

# re: add_shown & add_hiding ModalPopupExtender Events

Friday, December 16, 2011 4:52 AM by Mark

If I click on the modalpopup extender anywhere....the page_load and add_shown gets fired again...? anyway to stop it form firing on mouse clicks?

# re: add_shown & add_hiding ModalPopupExtender Events

Wednesday, January 25, 2012 7:26 AM by Dora743

This didn't work for me in VS2010 but work well in VS2008 what might be the issue

# re: add_shown & add_hiding ModalPopupExtender Events

Wednesday, January 25, 2012 9:46 AM by Yousef_Jadallah

Dear Dora743,

Try to use ToolkitScriptManager instead of ScriptManager.

<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>

 

 

Leave a Comment

(required) 
(required) 
(optional)
(required)