Image Popup In ASP.NET Using AJAXControlToolkit ModalPopupExtender

AJAXControlToolkit’s ModalPopupExtender is an important tool for showing popups in ASP.NET WebForms. Today, we will discuss how to show image popups on clicking the images in your Web Form.

So, let’s start. First, install the latest AJAXControlToolkit for Visual Studio. My article here will help you with that. This installs the AJAXControlToolkit for all versions of Visual Studio.

Now, let’s create an empty website (or website project) in Visual Studio targeting the .NET framework 4.0 or above (I am using Visual Studio 2013). Let’s name the website. I named it “ImagePopUp”. In the project, let’s add a folder “Images”. In that folder, let’s put a few images which we will show as popups. Now, add a WebForm to the project. In the head section of the WebForm, let’s add some CSS.

  1. <style type="text/css">  
  2.         body  
  3.         {  
  4.             font-familyArial;  
  5.             font-size10pt;  
  6.         }  
  7.         .modalBackground  
  8.         {  
  9.             background-color: Black;  
  10.             filter: alpha(opacity=90);  
  11.             opacity: 0.8;  
  12.         }  
  13.         .modalPopup  
  14.         {  
  15.             background-color#fff;  
  16.             border3px solid #ccc;  
  17.             padding10px;  
  18.             width1000px;  
  19.         }  
  20.     </style>  

This stylesheet will be used to show the background and foreground of the popup. In the body of the WebForm, let’s add an Image button (this will show the images which will popup when we click on it). The code for image button is given below.

  1. <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/nature1.jpg" Width="300px" Height="100px" />  

I have set the ImageUrl to one of the images in my Images folder and set the width and height.

Now, let’s add a panel to your WebForm and in the panel, let’s add and HTML image and ASP.NET image button. The code for panel looks like this.

  1. <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" Style="display: none">  
  2.             <img src="Images/nature1.jpg" width="800" height="400" />  
  3.             <div style="text-align:center;">  
  4.             <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/x.png" Width="25px" />  
  5.                 </div>  
  6.         </asp:Panel>  

The panel will be displayed as popup. Let’s set the CSS Class as “modalPopup”(the CSS we declared earlier in the head section). The HTML image will be shown in the popup panel and the Image Button in the panel will be used to close the popup. Now, let’s drag and drop a ModalPopUpExtender from AjaxControlToolkit tab in the tool box on your WebForm.

Image Popup In ASP.NET Using AJAXControlToolkit ModalPopupExtender

You will see that AjaxControlToolit.dll is added to your project.

Image Popup In ASP.NET Using AJAXControlToolkit ModalPopupExtender

Now, set the PopupControlID as “panel1”, TargetControlID as “ImageButton1”, CancelControlID as “ImageButton2” and BackgroundCssClass as “modalBackground”(the CSS we declared earlier in the head section). Let’s add some animation for popup. I have added fadein-fadeout animation. My code for ModalPopupExtender looks like below.

  1. <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="Panel1" TargetControlID="ImageButton1"  
  2.              CancelControlID="ImageButton2" BackgroundCssClass="modalBackground" >  
  3.              <Animations>  
  4.                 <OnShowing>  
  5.                 <FadeIn Duration=".5" Fps="30" />  
  6.             </OnShowing>  
  7.             <OnShown>  
  8.                 <FadeIn Duration=".5" Fps="30" />  
  9.             </OnShown>  
  10.               
  11.             <OnHiding>  
  12.                 <FadeOut Duration=".5" Fps="30" />  
  13.             </OnHiding>  
  14.             <OnHidden>  
  15.                 <FadeOut Duration=".5" Fps="30" />  
  16.             </OnHidden>  
  17.   
  18.             </Animations>  
  19.         </ajaxToolkit:ModalPopupExtender>  

Now, repeat the above steps for any other images you want to show. My code for the second image is as below.

  1. <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender2" runat="server" PopupControlID="Panel2" TargetControlID="ImageButton3"  
  2.              CancelControlID="ImageButton4" BackgroundCssClass="modalBackground">  
  3.             <Animations>  
  4.                 <OnShowing>  
  5.                 <FadeIn Duration=".5" Fps="30" />  
  6.             </OnShowing>  
  7.             <OnShown>  
  8.                 <FadeIn Duration=".5" Fps="30" />  
  9.             </OnShown>  
  10.               
  11.             <OnHiding>  
  12.                 <FadeOut Duration=".5" Fps="30" />  
  13.             </OnHiding>  
  14.             <OnHidden>  
  15.                 <FadeOut Duration=".5" Fps="30" />  
  16.             </OnHidden>  
  17.   
  18.             </Animations>  
  19.         </ajaxToolkit:ModalPopupExtender>  
  20.         <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/Images/mature2.jpg" Width="300px" Height="100px" />  
  21.         <asp:Panel ID="Panel2" runat="server" CssClass="modalPopup" align="center" Style="display: none">  
  22.             <img src="Images/nature2.jpg" width="800" height="400" />  
  23.             <div style="text-align:center;">  
  24.             <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/Images/x.png" Width="25px" />  
  25.                 </div>  
  26.         </asp:Panel>  

Now, on the top of the form, let’s add script manager from Ajax Extender Tab in the toolbox. Now, run the program. You will see something like this.

Image Popup In ASP.NET Using AJAXControlToolkit ModalPopupExtender

Now, click on any of the images and you will get a popup with the image.

Image Popup In ASP.NET Using AJAXControlToolkit ModalPopupExtender

Click on the black “X” button at the bottom of the popup and it will close the popup.

Conclusion

So, we see that ModalPopupExtender is an important tool for showing the popup. You can use it to show popups of literally anything. Here, I have used it to show the popups of images.

That’s all for this article. Hope this article is useful.