Open a New Web Form in the Modal Popup of ASP.Net Application

Introduction

In my previous article you learned about how to create a Model Popup in ASP.NET Application by using Ajax.

But my previous article was opening a Panel in Model Popup, it might be possible that you want to use too many items or classes in the Model Popup in that case using a Panel can become hectic, in those cases you need to use a New WebForm as a Popup so my this article will show you how you can open a New Web Form in the Model Popup.

Step 1

If you are working on the previous application that was created in my previous article then you don't need to Add New add a new Ajax Toolkit for this article, and if you are Creating a New Web Application then first of all add an Ajax Toolkit that can be either downloaded from the Ajax Website or can be taken from the Zip File that I had provided at the beginning of this Article.

model2.jpg

After attaching the Toolkit with the application you need to register this in your application that can be done by writing this code:

  1. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
Step 2

Now you need to add a new Web Page to your application that you want to open as a Model Popup.

After adding the Web Page create a Form in the Web Page that will be filled by the End User in the Popup Window.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication26.WebForm2" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <table>  
  10.     <tr>  
  11.     <td>  
  12.     <asp:Label ID="Label1" runat="server" CssClass="lbl" Text="First Name"></asp:Label>  
  13.     </td>  
  14.     <td>  
  15.     <asp:TextBox ID="TextBox1" runat="server" Font-Size="14px" ></asp:TextBox>  
  16.     </td>  
  17.     </tr>  
  18.     <tr>  
  19.     <td>  
  20.     <asp:Label ID="Label2" runat="server" CssClass="lbl" Text="Middle Name"></asp:Label>  
  21.     </td>  
  22.     <td>  
  23.     <asp:TextBox ID="TextBox2" runat="server" Font-Size="14px" ></asp:TextBox>  
  24.     </td>  
  25.     </tr>  
  26.     <tr>  
  27.     <td>  
  28.     <asp:Label ID="Label3" runat="server" CssClass="lbl" Text="Last Name"></asp:Label>  
  29.     </td>  
  30.     <td>  
  31.     <asp:TextBox ID="TextBox3" runat="server" Font-Size="14px" ></asp:TextBox>  
  32.     </td>  
  33.     </tr>  
  34.     <tr>  
  35.     <td>  
  36.     <asp:Label ID="Label4" runat="server" CssClass="lbl" Text="Gender"></asp:Label>  
  37.     </td>  
  38.     <td>  
  39.     <asp:TextBox ID="TextBox4" runat="server" Font-Size="14px" ></asp:TextBox>  
  40.     </td>  
  41.     </tr>  
  42.     <tr>  
  43.     <td>  
  44.     <asp:Label ID="Label5" runat="server" CssClass="lbl" Text="Age"></asp:Label>  
  45.     </td>  
  46.     <td>  
  47.     <asp:TextBox ID="TextBox5" runat="server" Font-Size="14px" ></asp:TextBox>  
  48.     </td>  
  49.     </tr>  
  50.     <tr>  
  51.     <td>  
  52.     <asp:Label ID="Label6" runat="server" CssClass="lbl" Text="City"></asp:Label>  
  53.     </td>  
  54.     <td>  
  55.     <asp:TextBox ID="TextBox6" runat="server" Font-Size="14px" ></asp:TextBox>  
  56.     </td>  
  57.     </tr>  
  58.     <tr>  
  59.     <td>  
  60.     <asp:Label ID="Label7" runat="server" CssClass="lbl" Text="State"></asp:Label>  
  61.     </td>  
  62.     <td>  
  63.     <asp:TextBox ID="TextBox7" runat="server" Font-Size="14px" ></asp:TextBox>  
  64.     </td>  
  65.     </tr>  
  66.     </table>  
  67.     </form>  
  68. </body>  
  69. </html>  
Now our work on the second Web Form is completed, now we just need to show it in the Model Popup of first Web Page.

Step 3

Now next step is to add the Script Manager to your application so that you can use the Tools of Ajax.

  1. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  2. </asp:ScriptManager>  
Now we can call the ModelPopupExtender in our application.
  1. <cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="Button1"  
  2.     CancelControlID="Button2" BackgroundCssClass="Background">  
  3. </cc1:ModalPopupExtender>  
In ModelPopupExtender we pass few ID's like which ID will be used to open the Popup, which ID will be used to close the Popup, on which ID Popup is to be open.

Step 4

Here I had used Panel as Popup and two buttons for opening and closing the application.

Under the Panel you need to add the IFrame that will be used to open the Web Form in the Panel.

  1. <asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">  
  2.     <iframe style=" width: 350px; height: 300px;" id="irm1" src="WebForm2.aspx" runat="server"></iframe>  
  3.    <br/>  
  4.     <asp:Button ID="Button2" runat="server" Text="Close" />  
  5. </asp:Panel>  
As you can see I had shown the IFrame inside the Panel, that's because we can't use an IFrame directly in the popup so for that we need a Panel and inside the Panel our Web Form will be opened, but don't worry; it will not at all disturb the working of your second Web Form, you can think a Panel as a container of IFrame and IFrame as container of WebForm. the second WebForm name would be provided in the "src" of IFrame.

But still now the Closing Button for the Popup will be provided in the Panel only otherwise "Your Popup will not be opened in the output window", that's because you need to call the closing button on the same WebForm on which Popup is to be shown.

Step 5

Your complete code would be as follows:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication26.WebForm1" %>  
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
  3. <!DOCTYPE html>  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7.      <style type="text/css">  
  8.         .Background  
  9.         {  
  10.             background-color: Black;  
  11.             filter: alpha(opacity=90);  
  12.             opacity: 0.8;  
  13.         }  
  14.         .Popup  
  15.         {  
  16.             background-color#FFFFFF;  
  17.             border-width3px;  
  18.             border-stylesolid;  
  19.             border-colorblack;  
  20.             padding-top10px;  
  21.             padding-left10px;  
  22.             width400px;  
  23.             height350px;  
  24.         }  
  25.         .lbl  
  26.         {  
  27.             font-size:16px;  
  28.             font-style:italic;  
  29.             font-weight:bold;  
  30.         }  
  31.     </style>  
  32. </head>  
  33. <body>  
  34.     <form id="form1" runat="server">  
  35. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  36. </asp:ScriptManager>  
  37. <asp:Button ID="Button1" runat="server" Text="Fill Form in Popup" />  
  38. <cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="Button1"  
  39.     CancelControlID="Button2" BackgroundCssClass="Background">  
  40. </cc1:ModalPopupExtender>  
  41. <asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">  
  42.     <iframe style=" width: 350px; height: 300px;" id="irm1" src="WebForm2.aspx" runat="server"></iframe>  
  43.    <br/>  
  44.     <asp:Button ID="Button2" runat="server" Text="Close" />  
  45. </asp:Panel>  
  46.     </form>  
  47. </body>  
  48. </html>  
Output

Now you can run your program and will see the output.

model3.jpg

Now a Popup Window will be opened where the data will be shown that you had provided in the second Web Form.

model5.jpg


Similar Articles