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.

6 Comments

Comments have been disabled for this content.