473,383 Members | 1,798 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.

User Control and Page Events

Ok...I've been looking for an answer to this problem and can't seem to find
one...Framework 1.1 mind you.

I have a base class that inherits from UserControl. I have 5 and soon to be
12 user controls that derive from this base class that represent US States
with appropriate business logic and rules. Inside the base class is an event
that is raised whenever you click a checkbox in the datagrid of each of the
user controls...no biggie... This event is to be seen by the page so that it
can take appropriate action upon handling the event (such as disabling
selections, etc...) however the page cannot see this event to handle it.

Now another event needs to be fired by the page when you make a selection in
one or more dropdowns that are then handled by each of the user controls as
appropriate. For example, if one dropdown value gets selected, each state
user control will compare its business rules to the selection and uncheck a
checkbox in its grid if the rules dictate. However, this event is never seen
by the control.

My problem is that I don't know where to define these events so that the
page and the controls can see and use them. The UC event i've defined in the
baseclass so that all the derived controls can access it through a protected
method. However, I cannot construct a handles on the page because the page
can't see it. Likewise I cannot construct a handles in the controls because
they cannot see the page event.

Any ideas or need for further info?
Mar 14 '07 #1
3 1425
>Likewise I cannot construct a handles in the controls because
>they cannot see the page event.
Any ideas or need for further info?
What about this idea:

1. You define a Notification List (NL) on the page for each event you need
to handle (or one list for all events):

public ArrayList NotificationList = new ArrayList();

2. Each UC has one base class, wich in its OnLoad method registers itself in
the NL:

public class MyBaseControl: UserControl
{
override OnLoad()
{
if (Page != null) (Page as MyBasePage).NotificationList.Add(this);
}

public HandlePageEvent(EventArgs e)
{
DoSomething();
}
}

3. When you have an event on a page, you do something like this:

protected MyList_SelectedIndexChanged(EventArgs e)
{
foreach (MyBaseControl control in NotificationList)
control.HandlePageEvent(e);
}

So, all your UC will see the page's events.

PS. I typed directly into this window, so, it is a "psevdocode".
Mar 14 '07 #2
That is what I wanted to avoid actually. But it may be the only way I can
achieve what I want to do. I do similar stuff elsewhere but it doesn't seem
as elegant as just doing it all with events and not making methods that
iterate over all the controls when each control could just subscribe the the
event as needed.

I would rather do this with pure events. It seems that if you don't derive
from a base class then event handling is 1:1. However, as soon as I started
deriving then the whole event model blew up because nothing can see the
other's events. Which seems counterintuitive.

Thanks for the input - any other ideas?

"Sergey Gorbachev" wrote:
Likewise I cannot construct a handles in the controls because
they cannot see the page event.
Any ideas or need for further info?

What about this idea:

1. You define a Notification List (NL) on the page for each event you need
to handle (or one list for all events):

public ArrayList NotificationList = new ArrayList();

2. Each UC has one base class, wich in its OnLoad method registers itself in
the NL:

public class MyBaseControl: UserControl
{
override OnLoad()
{
if (Page != null) (Page as MyBasePage).NotificationList.Add(this);
}

public HandlePageEvent(EventArgs e)
{
DoSomething();
}
}

3. When you have an event on a page, you do something like this:

protected MyList_SelectedIndexChanged(EventArgs e)
{
foreach (MyBaseControl control in NotificationList)
control.HandlePageEvent(e);
}

So, all your UC will see the page's events.

PS. I typed directly into this window, so, it is a "psevdocode".
Mar 15 '07 #3
Thanks for the input - any other ideas?

Maybe the UC can add their own event handlers to the Page's controls?
Something like this:

public class MyBaseControl: UserControl
{
override OnInit()
{
base.OnInit();

if (Page != null) (Page.MyList.SelectedIndexChanged += new
EventHandler(MyList_SelectionChanged);
}

void MyList_SelectionChanged(object sender, EventArgs e)
{
DoSomething();
}
}
Mar 15 '07 #4

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

Similar topics

1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
10
by: George G. | last post by:
Hi there, I am busy writing a new asp.net application and I am reusing some of my existing asp functions and methods in a user control. I need access to session, request and response in some of...
6
by: grist2mill | last post by:
I want to create a standard tool bar that appears on all pages that is a control. The toolbar has a button 'New'. What I wolud like when the user clicks on 'New' depends on the page they are on. I...
4
by: thomson | last post by:
Hi all, i do have a user control with 4 buttons, and all the events are firing properly, My problem is that i need to right an event handler in the user control, which gets fired after a...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.