Tags

, , , , , ,

Here is a simple mechanism to make a aspx page to be opened as popup window. I used JavaScript method window.open to open a page as popup in C# and ASP.NET.

The window.open() function requires three parameters:

  • The link for the new page.
  • The frame name of the window.
  • A comma-separated string of attributes that will configure the style and size of the pop-up window. These can include the height and width attributes (with pixel values); the toolbar, menuBar, and scrollbar attributes (set to yes or no, depending on whether you want to display these elements); and the resizable attribute (set to yes or no, depending on whether you want a fixed or resizable window border).

Here i created tow pages: Parent.aspx and Child.aspx. I placed a button in the parent  page, which on click on that button the child.aspx page popup.

Parent.aspx page:

   1: <%@ Page Language="C#" AutoEventWireup="true" 

CodeFile="Parent.aspx.cs" Inherits="Test_Parent" %>
   2: 
   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4: <html xmlns="http://www.w3.org/1999/xhtml">
   5: <head runat="server">
   6:     <title>Untitled Page</title>
   7: </head>
   8: <body>
   9:     <form id="form1" runat="server">
  10:     <div>
  11:         Hello,
  12:         <br />
  13:         This is a parent page click on poop up button to 

open new popup window.
  14:         <br />
  15:         <br />
  16:         <asp:Button ID="btnPopup" runat="server" 

Text="Pop Up" />
  17:     </div>
  18:     </form>
  19: </body>
  20: </html>

 

Parent.aspx.cs page:

   1: using System;
   2: using System.Collections;
   3: using System.Configuration;
   4: using System.Data;
   5: using System.Linq;
   6: using System.Web;
   7: using System.Web.Security;
   8: using System.Web.UI;
   9: using System.Web.UI.HtmlControls;
  10: using System.Web.UI.WebControls;
  11: using System.Web.UI.WebControls.WebParts;
  12: using System.Xml.Linq;
  13: 
  14: public partial class Test_Parent : System.Web.UI.Page
  15: {
  16:     protected void Page_Load(object sender, EventArgs e)
  17:     {
  18:         btnPopup.Attributes.Add("onclick", 

"window.open('Child.aspx',null,'left=200, top=30, height=550,

 width= 500, status=no, resizable= yes, scrollbars= yes, 

toolbar= no,location= no, menubar= no');");
  19:     }
  20: }

 

Child.aspx page:

   1: <%@ Page Language="C#" AutoEventWireup="true"

 CodeFile="Child.aspx.cs" Inherits="Test_Popup_Child" %>
   2: 
   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4: <html xmlns="http://www.w3.org/1999/xhtml">
   5: <head runat="server">
   6:     <title>Untitled Page</title>
   7: </head>
   8: <body>
   9:     <form id="form1" runat="server">
  10:     <div>
  11:         Hi, This is a Child window<br />
  12:         <br />
  13:         <asp:Button ID="btnClose" runat="server" 

Text="Close" />
  14:     </div>
  15:     </form>
  16: </body>
  17: </html>

 

Child.aspx.cs page:

   1: using System;
   2: using System.Collections;
   3: using System.Configuration;
   4: using System.Data;
   5: using System.Linq;
   6: using System.Web;
   7: using System.Web.Security;
   8: using System.Web.UI;
   9: using System.Web.UI.HtmlControls;
  10: using System.Web.UI.WebControls;
  11: using System.Web.UI.WebControls.WebParts;
  12: using System.Xml.Linq;
  13: 
  14: public partial class Test_Popup_Child : System.Web.UI.Page
  15: {
  16:     protected void Page_Load(object sender, EventArgs e)
  17:     {
  18:         btnClose.Attributes.Add("onclick", 

"window.opener.location.reload();window.close()");
  19:     }
  20: }