473,416 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Solution: Raising an Event Handler Postback on Parent Window From Popup Child Window Dialog Box

I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at

http://weblogs.asp.net/asmith/archiv.../15/27684.aspx

but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution.

Background
I have created a web application that uses configuation information held in a data store to alter the display charactoristics of a page. On the top of each page is a control panel that lets users change the configuation information in then save the data to the data store, and an event is fired that rebinds the page. This works fine but the Control Panel at the top of the page can use quite a bit on page real estate when it is open(visible). I thought if I could accomplish the same affect with a popup window, that would give the users a better experience.

Objectives
1. A very simple way of creating a button on the parent window that has a postback event associated with it.
2. When the parent window open button is clicked, a child window to be displayed and the event hander is not fired (at this time)
3. The child window would employ a button that would fire its event handler, (contitionally) fire the event handler of the parent opening button, and then (conditionally) close the itself (the child window)
4. Muliple parent "window open" buttons need to be able to be added to a page without conflicts.

Solution.
I created two custom buttons that inherit from the System.Web.UI. WebControls.Button control.

The WindowOpenButton goes on the parent file and sets the FileToOpen property to the name of the page to pop up into a window. When a user clicks on this button, it opens the file specified in the FileToOpen property, but does NOT fire the buttons event handler.

The ChildWindowButton goes in the child page file and captures the ID of the parent window button that opened its window. In the event handler of this button, the programmer must explicity call one of two methods to fire the event handler of the parent WindowOpenButton button. PostBackParentAndExitWindow() fires the event hander for the WindowOpenButton that opened this window but does NOT close the current child window. PostBackParent() fires the event hander for the WindowOpenButton that opened this window but does NOT close the current child window.

Here are the code files.

WindowOpenButton Control File

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for WindowOpenButton.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WindowOpenButton runat=server></{0}:WindowOpenButton>")]
public class WindowOpenButton : System.Web.UI.WebControls.Button
{
public string FileToOpen
{
get
{
object x = ViewState["WindowToOpen"];
return x==null?"":Convert.ToString(x);
}
set
{
ViewState["WindowToOpen"] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
this.Attributes.Add("onclick","javascript:window.o pen(\""+FileToOpen+"?clientid="+ this.ID +"\",\""+this.ID+"\",\"toolbar,width=500,height=50 0\");return false");
Page.RegisterStartupScript("PostBack_" + this.ID,"<Script Language=\"Javascript\">function PostBack_"+ this.ID +"() {"+Page.GetPostBackClientEvent(this,"")+"}</script>");
base.OnPreRender (e);
}

}
}
ChildWindowButton Control File

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for ChildWindowButton.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ChildWindowButton runat=server></{0}:ChildWindowButton>")]
public class ChildWindowButton : System.Web.UI.WebControls.Button
{

public string WindowOpenButtonClientId
{
get
{
object x = ViewState["WindowOpenButtonClientId"];
return x==null?null:Convert.ToString(x);
}
set
{
ViewState["WindowOpenButtonClientId"] = value;
}
}

public void PostBackParentAndExitWindow()
{
Page.RegisterStartupScript("ParentPostback","<Scri pt Language=\"Javascript\">window.opener.PostBack_" + this.WindowOpenButtonClientId + "();window.close();</script>");
}

public void PostBackParent()
{
Page.RegisterStartupScript("ParentPostback","<Scri pt Language=\"Javascript\">window.opener.PostBack_" + this.WindowOpenButtonClientId + "();</script>");
}

protected override void OnPreRender(EventArgs e)
{
WindowOpenButtonClientId=Page.Request["ClientId"];
base.OnPreRender (e);
}

}
}

In this sample a session variable Session["Data"] is used to represent a data store,. When the Child Window comes up, the user can enter some text into the text box and it will be stored in the session variable. The event hander of the parent will then run the DataBindPage method to display the value on its page.

ParentPage.aspx

<form id="Form1" method="post" runat="server">
<cc1:windowopenbutton id="WindowOpenButton1" runat="server" FileToOpen="popup.aspx" Text="Open Window 1 Button"></cc1:windowopenbutton>
<BR>
PAGE TEXT
<br>
<asp:literal id="Literal1" runat="server"></asp:literal></P>
</form>

ParentPage.aspx.cs CodeBehind


protected WindowOpenButton WindowOpenButton1;
protected System.Web.UI.WebControls.Literal Literal1;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
DataBindPage();
}
}

private void DataBindPage()
{
object o = Session["Data"];
Literal1.Text = (o==null)?"":(string)o;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.WindowOpenButton1.Click += new System.EventHandler(this.WindowOpenButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void WindowOpenButton1_Click(object sender, EventArgs e)
{
DataBindPage();
}

Popup.aspx Popup Window Page

<form id="Form1" method="post" runat="server">
<P>POPUP WINDOW</P>
<P>Data :
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P><cc1:ChildWindowButton id="ChildWindowButton1" Text="Local Postback Only" runat="server" /></P>
<P><cc1:ChildWindowButton id="ChildWindowButton2" Text="PostBack Parent but Keep this Window Open" runat="server" /></P>
<P><cc1:ChildWindowButton id="ChildWindowButton3" Text="Postback Pareent and Close Window" runat="server" /></P>
<P>&nbsp;</P>
<P>
<asp:Literal id="Literal2" runat="server"></asp:Literal></P>
</form>

Popup.aspx.cs Code Behind

protected System.Web.UI.WebControls.Literal Literal2;
protected ChildWindowButton ChildWindowButton1;
protected ChildWindowButton ChildWindowButton2;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected ChildWindowButton ChildWindowButton3;

private void Page_Load(object sender, System.EventArgs e)
{
//ChildCloseWithPostbackButton1.WindowOpenButtonClie ntId=Request["ClientId"];
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ChildWindowButton1.Click += new System.EventHandler(this.ChildWindowButton1_Click) ;
this.ChildWindowButton2.Click += new System.EventHandler(this.ChildWindowButton2_Click) ;
this.ChildWindowButton3.Click += new System.EventHandler(this.ChildWindowButton3_Click) ;
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ChildWindowButton1_Click(object sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This is local postback only";
}
private void ChildWindowButton2_Click(object sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent (For Apply Button)";
ChildWindowButton2.PostBackParent();
}

private void ChildWindowButton3_Click(object sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent and Close Window";
ChildWindowButton3.PostBackParentAndExitWindow();
}
If you have any comments or suggestions, please let me know

Earl
Nov 18 '05 #1
1 11518
CORRECTION

THE LINE

PostBackParentAndExitWindow() fires the event hander for the WindowOpenButton that opened this window but does NOT close the current child window.

SHOULD READ

PostBackParentAndExitWindow() fires the event hander for the WindowOpenButton that opened this window and closes the current child window.

Earl

"Earl Teigrob" <ea******@hotmail.com> wrote in message news:OE**************@TK2MSFTNGP12.phx.gbl...
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at

http://weblogs.asp.net/asmith/archiv.../15/27684.aspx

but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution.

Background
I have created a web application that uses configuation information held in a data store to alter the display charactoristics of a page. On the top of each page is a control panel that lets users change the configuation information in then save the data to the data store, and an event is fired that rebinds the page. This works fine but the Control Panel at the top of the page can use quite a bit on page real estate when it is open(visible). I thought if I could accomplish the same affect with a popup window, that would give the users a better experience.

Objectives
1. A very simple way of creating a button on the parent window that has a postback event associated with it.
2. When the parent window open button is clicked, a child window to be displayed and the event hander is not fired (at this time)
3. The child window would employ a button that would fire its event handler, (contitionally) fire the event handler of the parent opening button, and then (conditionally) close the itself (the child window)
4. Muliple parent "window open" buttons need to be able to be added to a page without conflicts.

Solution.
I created two custom buttons that inherit from the System.Web.UI. WebControls.Button control.

The WindowOpenButton goes on the parent file and sets the FileToOpen property to the name of the page to pop up into a window. When a user clicks on this button, it opens the file specified in the FileToOpen property, but does NOT fire the buttons event handler.

The ChildWindowButton goes in the child page file and captures the ID of the parent window button that opened its window. In the event handler of this button, the programmer must explicity call one of two methods to fire the event handler of the parent WindowOpenButton button. PostBackParentAndExitWindow() fires the event hander for the WindowOpenButton that opened this window but does NOT close the current child window. PostBackParent() fires the event hander for the WindowOpenButton that opened this window but does NOT close the current child window.

Here are the code files.

WindowOpenButton Control File

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for WindowOpenButton.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WindowOpenButton runat=server></{0}:WindowOpenButton>")]
public class WindowOpenButton : System.Web.UI.WebControls.Button
{
public string FileToOpen
{
get
{
object x = ViewState["WindowToOpen"];
return x==null?"":Convert.ToString(x);
}
set
{
ViewState["WindowToOpen"] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
this.Attributes.Add("onclick","javascript:window.o pen(\""+FileToOpen+"?clientid="+ this.ID +"\",\""+this.ID+"\",\"toolbar,width=500,height=50 0\");return false");
Page.RegisterStartupScript("PostBack_" + this.ID,"<Script Language=\"Javascript\">function PostBack_"+ this.ID +"() {"+Page.GetPostBackClientEvent(this,"")+"}</script>");
base.OnPreRender (e);
}

}
}
ChildWindowButton Control File

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for ChildWindowButton.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ChildWindowButton runat=server></{0}:ChildWindowButton>")]
public class ChildWindowButton : System.Web.UI.WebControls.Button
{

public string WindowOpenButtonClientId
{
get
{
object x = ViewState["WindowOpenButtonClientId"];
return x==null?null:Convert.ToString(x);
}
set
{
ViewState["WindowOpenButtonClientId"] = value;
}
}

public void PostBackParentAndExitWindow()
{
Page.RegisterStartupScript("ParentPostback","<Scri pt Language=\"Javascript\">window.opener.PostBack_" + this.WindowOpenButtonClientId + "();window.close();</script>");
}

public void PostBackParent()
{
Page.RegisterStartupScript("ParentPostback","<Scri pt Language=\"Javascript\">window.opener.PostBack_" + this.WindowOpenButtonClientId + "();</script>");
}

protected override void OnPreRender(EventArgs e)
{
WindowOpenButtonClientId=Page.Request["ClientId"];
base.OnPreRender (e);
}

}
}

In this sample a session variable Session["Data"] is used to represent a data store,. When the Child Window comes up, the user can enter some text into the text box and it will be stored in the session variable. The event hander of the parent will then run the DataBindPage method to display the value on its page.

ParentPage.aspx

<form id="Form1" method="post" runat="server">
<cc1:windowopenbutton id="WindowOpenButton1" runat="server" FileToOpen="popup.aspx" Text="Open Window 1 Button"></cc1:windowopenbutton>
<BR>
PAGE TEXT
<br>
<asp:literal id="Literal1" runat="server"></asp:literal></P>
</form>

ParentPage.aspx.cs CodeBehind


protected WindowOpenButton WindowOpenButton1;
protected System.Web.UI.WebControls.Literal Literal1;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
DataBindPage();
}
}

private void DataBindPage()
{
object o = Session["Data"];
Literal1.Text = (o==null)?"":(string)o;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.WindowOpenButton1.Click += new System.EventHandler(this.WindowOpenButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void WindowOpenButton1_Click(object sender, EventArgs e)
{
DataBindPage();
}

Popup.aspx Popup Window Page

<form id="Form1" method="post" runat="server">
<P>POPUP WINDOW</P>
<P>Data :
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P><cc1:ChildWindowButton id="ChildWindowButton1" Text="Local Postback Only" runat="server" /></P>
<P><cc1:ChildWindowButton id="ChildWindowButton2" Text="PostBack Parent but Keep this Window Open" runat="server" /></P>
<P><cc1:ChildWindowButton id="ChildWindowButton3" Text="Postback Pareent and Close Window" runat="server" /></P>
<P>&nbsp;</P>
<P>
<asp:Literal id="Literal2" runat="server"></asp:Literal></P>
</form>

Popup.aspx.cs Code Behind

protected System.Web.UI.WebControls.Literal Literal2;
protected ChildWindowButton ChildWindowButton1;
protected ChildWindowButton ChildWindowButton2;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected ChildWindowButton ChildWindowButton3;

private void Page_Load(object sender, System.EventArgs e)
{
//ChildCloseWithPostbackButton1.WindowOpenButtonClie ntId=Request["ClientId"];
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ChildWindowButton1.Click += new System.EventHandler(this.ChildWindowButton1_Click) ;
this.ChildWindowButton2.Click += new System.EventHandler(this.ChildWindowButton2_Click) ;
this.ChildWindowButton3.Click += new System.EventHandler(this.ChildWindowButton3_Click) ;
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ChildWindowButton1_Click(object sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This is local postback only";
}
private void ChildWindowButton2_Click(object sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent (For Apply Button)";
ChildWindowButton2.PostBackParent();
}

private void ChildWindowButton3_Click(object sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent and Close Window";
ChildWindowButton3.PostBackParentAndExitWindow();
}
If you have any comments or suggestions, please let me know

Earl
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: LuB | last post by:
I am programming in an event model - and I need to delete dynamically allocated memory. I apologize if this question is to Windows specific - but I think it has more to do with alloc and...
10
by: Tom Szabo | last post by:
Is there an event when that triggers when the window is closing.... I am talking about when the user clicks on the cross on the right top corner of the window!!!
3
by: SJ | last post by:
Here's the scenario Website has a button that pops up a confirmation window when clicked. When the user clicks the "confirm" button in this popup window, I want all the following to happen :...
3
by: Stevie_mac | last post by:
It might be me but... I dont seem to get a Page_Load event when a opening an ASPX in an iFrame. I do geta Page_Load event when an item on the ASPX (inside the iFrame) is clicked but then...
4
by: Earl Teigrob | last post by:
I am thinking about using a popup window to edit settings that will affect parent asp.net page. The data that is changed in the popup window will be saved to the datastore that is loaded and...
2
by: chuckdfoster | last post by:
I have 2 windows (MainPage.aspx and PopUp.aspx) and both have a textbox. I need the user to hit a button on the MainPage and the PopUp will show. Then I need the user to enter information in a...
5
by: Stan Sainte-Rose | last post by:
Hi, Which event is called when the user click on the close window icon (X) ? I want, when he clicks on this icon, to display a message before closing the form. If he replys by No, I don't want to...
5
by: Andy Fish | last post by:
Hi, I have an asp.net web application which uses a pop-up form that works a bit like a dialog box. when the user clicks "OK" it does a postback (basically a form post if you don't know .net) to...
6
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.