473,748 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.But ton control.

The WindowOpenButto n 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 ChildWindowButt on 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 WindowOpenButto n button. PostBackParentA ndExitWindow() fires the event hander for the WindowOpenButto n that opened this window but does NOT close the current child window. PostBackParent( ) fires the event hander for the WindowOpenButto n that opened this window but does NOT close the current child window.

Here are the code files.

WindowOpenButto n Control File

using System;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for WindowOpenButto n.
/// </summary>
[DefaultProperty ("Text"),
ToolboxData("<{ 0}:WindowOpenBu tton runat=server></{0}:WindowOpenB utton>")]
public class WindowOpenButto n : System.Web.UI.W ebControls.Butt on
{
public string FileToOpen
{
get
{
object x = ViewState["WindowToOp en"];
return x==null?"":Conv ert.ToString(x) ;
}
set
{
ViewState["WindowToOp en"] = value;
}
}
protected override void OnPreRender(Eve ntArgs e)
{
this.Attributes .Add("onclick", "javascript:win dow.open(\""+Fi leToOpen+"?clie ntid="+ this.ID +"\",\""+this.I D+"\",\"toolbar ,width=500,heig ht=500\");retur n false");
Page.RegisterSt artupScript("Po stBack_" + this.ID,"<Scrip t Language=\"Java script\">functi on PostBack_"+ this.ID +"() {"+Page.GetPost BackClientEvent (this,"")+"}</script>");
base.OnPreRende r (e);
}

}
}
ChildWindowButt on Control File

using System;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for ChildWindowButt on.
/// </summary>
[DefaultProperty ("Text"),
ToolboxData("<{ 0}:ChildWindowB utton runat=server></{0}:ChildWindow Button>")]
public class ChildWindowButt on : System.Web.UI.W ebControls.Butt on
{

public string WindowOpenButto nClientId
{
get
{
object x = ViewState["WindowOpenButt onClientId"];
return x==null?null:Co nvert.ToString( x);
}
set
{
ViewState["WindowOpenButt onClientId"] = value;
}
}

public void PostBackParentA ndExitWindow()
{
Page.RegisterSt artupScript("Pa rentPostback"," <Script Language=\"Java script\">window .opener.PostBac k_" + this.WindowOpen ButtonClientId + "();window.clos e();</script>");
}

public void PostBackParent( )
{
Page.RegisterSt artupScript("Pa rentPostback"," <Script Language=\"Java script\">window .opener.PostBac k_" + this.WindowOpen ButtonClientId + "();</script>");
}

protected override void OnPreRender(Eve ntArgs e)
{
WindowOpenButto nClientId=Page. Request["ClientId"];
base.OnPreRende r (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:windowopen button id="WindowOpenB utton1" runat="server" FileToOpen="pop up.aspx" Text="Open Window 1 Button"></cc1:windowopenb utton>
<BR>
PAGE TEXT
<br>
<asp:literal id="Literal1" runat="server"> </asp:literal></P>
</form>

ParentPage.aspx .cs CodeBehind


protected WindowOpenButto n WindowOpenButto n1;
protected System.Web.UI.W ebControls.Lite ral Literal1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (!Page.IsPostBa ck)
{
DataBindPage();
}
}

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

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

private void InitializeCompo nent()
{
this.WindowOpen Button1.Click += new System.EventHan dler(this.Windo wOpenButton1_Cl ick);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void WindowOpenButto n1_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:ChildWi ndowButton id="ChildWindow Button1" Text="Local Postback Only" runat="server" /></P>
<P><cc1:ChildWi ndowButton id="ChildWindow Button2" Text="PostBack Parent but Keep this Window Open" runat="server" /></P>
<P><cc1:ChildWi ndowButton id="ChildWindow Button3" 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.W ebControls.Lite ral Literal2;
protected ChildWindowButt on ChildWindowButt on1;
protected ChildWindowButt on ChildWindowButt on2;
protected System.Web.UI.W ebControls.Text Box TextBox1;
protected ChildWindowButt on ChildWindowButt on3;

private void Page_Load(objec t sender, System.EventArg s e)
{
//ChildCloseWithP ostbackButton1. WindowOpenButto nClientId=Reque st["ClientId"];
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.ChildWindo wButton1.Click += new System.EventHan dler(this.Child WindowButton1_C lick);
this.ChildWindo wButton2.Click += new System.EventHan dler(this.Child WindowButton2_C lick);
this.ChildWindo wButton3.Click += new System.EventHan dler(this.Child WindowButton3_C lick);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void ChildWindowButt on1_Click(objec t sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This is local postback only";
}
private void ChildWindowButt on2_Click(objec t sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent (For Apply Button)";
ChildWindowButt on2.PostBackPar ent();
}

private void ChildWindowButt on3_Click(objec t sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent and Close Window";
ChildWindowButt on3.PostBackPar entAndExitWindo w();
}
If you have any comments or suggestions, please let me know

Earl
Nov 18 '05 #1
1 11580
CORRECTION

THE LINE

PostBackParentA ndExitWindow() fires the event hander for the WindowOpenButto n that opened this window but does NOT close the current child window.

SHOULD READ

PostBackParentA ndExitWindow() fires the event hander for the WindowOpenButto n that opened this window and closes the current child window.

Earl

"Earl Teigrob" <ea******@hotma il.com> wrote in message news:OE******** ******@TK2MSFTN GP12.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.But ton control.

The WindowOpenButto n 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 ChildWindowButt on 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 WindowOpenButto n button. PostBackParentA ndExitWindow() fires the event hander for the WindowOpenButto n that opened this window but does NOT close the current child window. PostBackParent( ) fires the event hander for the WindowOpenButto n that opened this window but does NOT close the current child window.

Here are the code files.

WindowOpenButto n Control File

using System;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for WindowOpenButto n.
/// </summary>
[DefaultProperty ("Text"),
ToolboxData("<{ 0}:WindowOpenBu tton runat=server></{0}:WindowOpenB utton>")]
public class WindowOpenButto n : System.Web.UI.W ebControls.Butt on
{
public string FileToOpen
{
get
{
object x = ViewState["WindowToOp en"];
return x==null?"":Conv ert.ToString(x) ;
}
set
{
ViewState["WindowToOp en"] = value;
}
}
protected override void OnPreRender(Eve ntArgs e)
{
this.Attributes .Add("onclick", "javascript:win dow.open(\""+Fi leToOpen+"?clie ntid="+ this.ID +"\",\""+this.I D+"\",\"toolbar ,width=500,heig ht=500\");retur n false");
Page.RegisterSt artupScript("Po stBack_" + this.ID,"<Scrip t Language=\"Java script\">functi on PostBack_"+ this.ID +"() {"+Page.GetPost BackClientEvent (this,"")+"}</script>");
base.OnPreRende r (e);
}

}
}
ChildWindowButt on Control File

using System;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;

namespace PopupDialog
{
/// <summary>
/// Summary description for ChildWindowButt on.
/// </summary>
[DefaultProperty ("Text"),
ToolboxData("<{ 0}:ChildWindowB utton runat=server></{0}:ChildWindow Button>")]
public class ChildWindowButt on : System.Web.UI.W ebControls.Butt on
{

public string WindowOpenButto nClientId
{
get
{
object x = ViewState["WindowOpenButt onClientId"];
return x==null?null:Co nvert.ToString( x);
}
set
{
ViewState["WindowOpenButt onClientId"] = value;
}
}

public void PostBackParentA ndExitWindow()
{
Page.RegisterSt artupScript("Pa rentPostback"," <Script Language=\"Java script\">window .opener.PostBac k_" + this.WindowOpen ButtonClientId + "();window.clos e();</script>");
}

public void PostBackParent( )
{
Page.RegisterSt artupScript("Pa rentPostback"," <Script Language=\"Java script\">window .opener.PostBac k_" + this.WindowOpen ButtonClientId + "();</script>");
}

protected override void OnPreRender(Eve ntArgs e)
{
WindowOpenButto nClientId=Page. Request["ClientId"];
base.OnPreRende r (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:windowopen button id="WindowOpenB utton1" runat="server" FileToOpen="pop up.aspx" Text="Open Window 1 Button"></cc1:windowopenb utton>
<BR>
PAGE TEXT
<br>
<asp:literal id="Literal1" runat="server"> </asp:literal></P>
</form>

ParentPage.aspx .cs CodeBehind


protected WindowOpenButto n WindowOpenButto n1;
protected System.Web.UI.W ebControls.Lite ral Literal1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if (!Page.IsPostBa ck)
{
DataBindPage();
}
}

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

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

private void InitializeCompo nent()
{
this.WindowOpen Button1.Click += new System.EventHan dler(this.Windo wOpenButton1_Cl ick);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void WindowOpenButto n1_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:ChildWi ndowButton id="ChildWindow Button1" Text="Local Postback Only" runat="server" /></P>
<P><cc1:ChildWi ndowButton id="ChildWindow Button2" Text="PostBack Parent but Keep this Window Open" runat="server" /></P>
<P><cc1:ChildWi ndowButton id="ChildWindow Button3" 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.W ebControls.Lite ral Literal2;
protected ChildWindowButt on ChildWindowButt on1;
protected ChildWindowButt on ChildWindowButt on2;
protected System.Web.UI.W ebControls.Text Box TextBox1;
protected ChildWindowButt on ChildWindowButt on3;

private void Page_Load(objec t sender, System.EventArg s e)
{
//ChildCloseWithP ostbackButton1. WindowOpenButto nClientId=Reque st["ClientId"];
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.ChildWindo wButton1.Click += new System.EventHan dler(this.Child WindowButton1_C lick);
this.ChildWindo wButton2.Click += new System.EventHan dler(this.Child WindowButton2_C lick);
this.ChildWindo wButton3.Click += new System.EventHan dler(this.Child WindowButton3_C lick);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void ChildWindowButt on1_Click(objec t sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This is local postback only";
}
private void ChildWindowButt on2_Click(objec t sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent (For Apply Button)";
ChildWindowButt on2.PostBackPar ent();
}

private void ChildWindowButt on3_Click(objec t sender, EventArgs e)
{
Session["Data"]=TextBox1.Text;
Literal2.Text = "This does Postback Parent and Close Window";
ChildWindowButt on3.PostBackPar entAndExitWindo w();
}
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
1626
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 deallocating heap based mem. Imagine if you will, a parent window - creating a child window dynamically. When the child window is Destroyed, the child window needs to delete itself. I don't believe the child window tells the parent when he is
10
9801
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
6199
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 : a) Form data on the Parent (opener) page to be submitted. b) Some (asp.net) code behind executed for the Parent page. c) Pop-Up window to close.
3
4016
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 IsPostBack=False by now!. The ASPX is opened via client side script (into an iFrame) inside a .HTM file (as ASPX postback causes problems in a modal internet explorer dialog if not in an iFrame!) Anyone know how to get around this - i need to...
4
2765
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 displayed by the parent page. In order to refresh the parent page, I would like to raise an event in the popup (child) page that is subscribed to by the parent so that I can rebind the data in the parent page once the data in the child window data is...
2
1384
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 textbox on the PopUp. Then when they hit the Submit button, the MainPage has a textbox that should be updated with the information from the textbox on PopUp. I am having a problem getting the MainPage to be updated from the PopUp window. Any...
5
11070
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 close the form. Thks for your help Stan
5
2793
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 save the contents of the form, and then I put an onload event to do a window.close. this works fine for hundreds of people on different browsers. However, for one user the window does not close.
6
19308
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 know, this shouldn't be an issue, but I just can't get it to work. The script only needs to work with Firefox/Mozilla, so XP code isn't an issue. I have tried to open a window like so.
0
8979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8818
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9522
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9355
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6790
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4589
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.