473,383 Members | 1,980 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,383 software developers and data experts.

Page and Control Event Sequence

I'm just wondering if this would work. Please note that I'm not asking *how*
to raise events. I'm clear on that. What I'm not clear on is the sequence in
which events are raised by custom controls relative to the hosting Page, and
therefore if the following specific arrangement would even work.

What I'm looking to do is dynamically insert multiple custom composite Web
server controls onto a blank Page. This is no problem, I know how to do this
part.

What I need is for one of these controls to "broadcast" events of interest
to any other controls on the hosting Page that may be interested. But I do
not want for any of the controls on the page to directly know anything about
each other (hold references to each other).

So, to make this happen I am thinking that this would work:
A control gets notified - via the hosting Page - of the event originating
in another control.

Specifically:

1. During PostBack, control #1 raises an event (that is of interest to
Control #2).

2. The hosting Page is subscribed to the event in Control #1

3. The hosting page then raises an event of its own, passing along the
EventArgs received from Control #1.

4. Control #2 is subscribed to the Page's event [that is raised in step 3
above], and consequently receives notification (from the Page) that includes
a copy of the EventArgs-derived class sent from Control #1.

Will this work? Will ASP.NET, during PostBack, raise the events in the
correct sequence? (Control riases event ----- page raises another
vent ----- Control handles event raised by Page)

Thanks!


Jun 27 '08 #1
1 2055
Hi,

I think it should work. Only downside with this approach is that you need
"tighter" coupling between the page and the control (e.g the control that's
listening for page's passthrough event needs to know more about the type of
the page, in practise some sort of interface etc)

Here's a sample

//INTERFACE - page implements

using System;

public interface IClickEventPage
{
event EventHandler Click;
}

//UC1 - raises an event

<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="WebApplication2.WebUserControl1" %>
<asp:Button ID="Button1" runat="server"
Text="Clicking on this will raise UC's click event"
onclick="Button1_Click" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
private static readonly Object EventClick = new Object();

public event EventHandler Click
{
add
{
Events.AddHandler(EventClick, value);
}
remove
{
Events.RemoveHandler(EventClick, value);
}
}
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
EventHandler h = Events[EventClick] as EventHandler;
if (h != null)
h(this, EventArgs.Empty);
}
}
}
//UC2 - listens for page's event
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl2.ascx.cs"
Inherits="WebApplication2.WebUserControl2" %>
<asp:Label ID="lblInfo" runat="server" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public partial class WebUserControl2 : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
IClickEventPage p = Page as IClickEventPage;
if(p != null)
{
p.Click += new EventHandler(p_Click);
}

}

void p_Click(object sender, EventArgs e)
{
lblInfo.Text = "Something occurred, informed by the Page:" +
DateTime.Now.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{

}
}
}

//PAGE - acts as middle-man

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication2._Default" %>

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1"
tagprefix="uc1" %>

<%@ Register src="WebUserControl2.ascx" tagname="WebUserControl2"
tagprefix="uc2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<uc1:WebUserControl1 ID="USerControl1" runat="server"
OnClick="UserControl1_Click" />

</div>
<uc2:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{

public partial class _Default : System.Web.UI.Page, IClickEventPage
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void UserControl1_Click(object sender, EventArgs e)
{
EventHandler click = Events[EventClick] as EventHandler;
if (click != null)
click(this, e);

}

#region IClickEventPage Members

private static readonly Object EventClick = new Object();
public event EventHandler Click
{
add
{
Events.AddHandler(EventClick, value);
}
remove
{
Events.RemoveHandler(EventClick, value);
}
}

#endregion
}
}


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"Jordan S." <A@B.comwrote in message
news:O5**************@TK2MSFTNGP04.phx.gbl...
I'm just wondering if this would work. Please note that I'm not asking
*how* to raise events. I'm clear on that. What I'm not clear on is the
sequence in which events are raised by custom controls relative to the
hosting Page, and therefore if the following specific arrangement would
even work.

What I'm looking to do is dynamically insert multiple custom composite Web
server controls onto a blank Page. This is no problem, I know how to do
this part.

What I need is for one of these controls to "broadcast" events of interest
to any other controls on the hosting Page that may be interested. But I do
not want for any of the controls on the page to directly know anything
about each other (hold references to each other).

So, to make this happen I am thinking that this would work:
A control gets notified - via the hosting Page - of the event originating
in another control.

Specifically:

1. During PostBack, control #1 raises an event (that is of interest to
Control #2).

2. The hosting Page is subscribed to the event in Control #1

3. The hosting page then raises an event of its own, passing along the
EventArgs received from Control #1.

4. Control #2 is subscribed to the Page's event [that is raised in step 3
above], and consequently receives notification (from the Page) that
includes a copy of the EventArgs-derived class sent from Control #1.

Will this work? Will ASP.NET, during PostBack, raise the events in the
correct sequence? (Control riases event ----- page raises another
ent ----- Control handles event raised by Page)

Thanks!


Jun 27 '08 #2

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

Similar topics

4
by: owingruters | last post by:
Hi all, I've created an User control. It's an extension of a textbox wich has some extra properties so that validation becomes a lot faster. The control wordks great if autopostback is on....
6
by: Glenn Owens | last post by:
I have an ASP.Net page on which there are serveral static controls (listboxes, radiobuttonlist and textboxes). These controls are used to create criteria from which the code-behind will dynamically...
3
by: TPS | last post by:
Instead of using Hyperlinks and URL / Response.QueryString() parameters to pass state around in my app, I am using LinkButtons with a corresponding command event and command arguments. This page...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
3
by: spmm# | last post by:
Hi! Could someone please help me with the following; I have a WebControl that basically looks like this: public class LeftMenu : System.Web.UI.WebControls.WebControl { private string...
0
by: mike.varley1 | last post by:
Hi All We are getting the warning messages below several times a day in the app event logs on our Win2k3 server. Can anyone shed any light on the meaning\cause of the DC issue, especially: "This...
2
by: MCM | last post by:
I'm working on a plotting control. The plotting control will have a context menu with basic commands for "scaling", "zooming", etc. Is there a way that, from the parent form, I can add more...
0
by: Bali | last post by:
Default.aspx is the starting page containing a control(ascx) which has asp:button control on it. On the button click event it has to open a new page as a modal control. Since refreshing a page in...
1
by: Bali | last post by:
Default.aspx is the starting page containing a control(ascx) which has asp:button control on it. On the button click event it has to open a new page as a modal control. Since refreshing a page in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.