472,802 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,802 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 11368
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...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.