473,808 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# UserControl Bubbling events

Alright, I am beyond confused here and need some guidance. I need a
C# only sample. I have a simple Page and within it i am creating a
user control (ascx). The user control contains textboxes, buttons,
ect. I would like to catch/add a handler/recieve the Button.OnClick
event from the buttons that are in the user control, in the Page.
How? As far as I can tell, my only route is to:

RaiseBubbleEven t in the Button.OnClick in the UserControl
Override the OnBubbleEvent in the UserControl
Override the OnBubbleEvent in the Page from the already Override(n)
UserControl

This is stupid. All I want a custom event, call it CustomEvent with
what ever signature I want, and I would like to allow the instance to
have the event CustomEvent override(n). Please help. I must be
missing the bus on this one...I can't seem to find a good C# sample
for what seems so common/trivial. TIA
Nov 17 '05 #1
4 10123
I might well be missing something here but...

You can simplify what you do. The simplest is:
1) Call RaiseBubbleEven t in your Button.OnClick event handler in your
user control.
2) Override OnBubbleEvent in in the page.
The OnBubbleEvent handler may gets lots of events, so you need to
identify your event. You do this by either identifying the source of
the event or defining your own event args and passing them in the
RaiseBubbleEven t.

That is the minimum. I think good programming practice, especially if
third parties use the control, would be to provide a public delegate
as an event handler in the control. This requires:
1) Define MyEventArgs from System.EventArg s
2) Define a delgate event handler: MyEvent( object source, MyEventArgs
args)
3) Call RaiseBubbleEven t in the Button.OnClick
4) Override OnBubbleEvent in the control. This calls the delegate if
it is initialised.
5) Define a delegate function in Page and assign it to the delegate
event handler of the control.

Basically all you are doing in steps 1 to 4 is define your own event
handler for the control. This is done once in the control. Step 5 is
done on every page that uses the control - which is nicer than
overriding OnBubbleEvent in every page that uses the control.

I used the simple example in the on-line documentation for
RaiseBubbleEven t.

HTH
Charles

"Andy" <an*******@yaho o.com> wrote in message
news:3b******** *************** ***@posting.goo gle.com...
Alright, I am beyond confused here and need some guidance. I need a
C# only sample. I have a simple Page and within it i am creating a
user control (ascx). The user control contains textboxes, buttons,
ect. I would like to catch/add a handler/recieve the Button.OnClick
event from the buttons that are in the user control, in the Page.
How? As far as I can tell, my only route is to:

RaiseBubbleEven t in the Button.OnClick in the UserControl
Override the OnBubbleEvent in the UserControl
Override the OnBubbleEvent in the Page from the already Override(n)
UserControl

This is stupid. All I want a custom event, call it CustomEvent with
what ever signature I want, and I would like to allow the instance to have the event CustomEvent override(n). Please help. I must be
missing the bus on this one...I can't seem to find a good C# sample
for what seems so common/trivial. TIA

Nov 17 '05 #2
In article <3b************ **************@ posting.google. com>, Andy wrote:

I'm not sure I totally understand this, but...
Alright, I am beyond confused here and need some guidance. I need a
C# only sample. I have a simple Page and within it i am creating a
user control (ascx). The user control contains textboxes, buttons,
ect. I would like to catch/add a handler/recieve the Button.OnClick
event from the buttons that are in the user control, in the Page.
How? As far as I can tell, my only route is to:
Well, there's three choices.
1. Use BubbleEvent

2. Make the button public and let the owner page attach the event
directly

3. Define your own event that runs in response to Button.OnClick,
and have the page attach to that event (I suspect this is what you
really want here).

RaiseBubbleEven t in the Button.OnClick in the UserControl
Override the OnBubbleEvent in the UserControl
Override the OnBubbleEvent in the Page from the already Override(n)
UserControl

This is stupid. All I want a custom event, call it CustomEvent with
what ever signature I want, and I would like to allow the instance to
have the event CustomEvent override(n). Please help. I must be
missing the bus on this one...I can't seem to find a good C# sample
for what seems so common/trivial. TIA


I'm not sure you actually have a real need for BubbleEvent here.
BubbleEvent is largely for those situation where there's a lot of
dynamic control creation going on. If all you want is a custom event,
why not just define one and use standard delegate/event stuff, and skip
the BubbleEvent stuff altogether? Am I missing something?

// inside the control

public event EventHandler MyEvent;

private void MyButton_OnClic k(object sender, EventArgs e)
{
if(MyEvent != null)
MyEvent(this, e); // or whatever args you want
}

// and inside the Page object

private void Page_Load(...)
{
MyUserControl.M yEvent += new EventHandler(my Handler);
}
I hope this helps, though I feel as if I'm actually missing something
here.
--
David
dfoster at
hotpop dot com
Nov 17 '05 #3
Think my brain was miss firing yesterday from reading the mounds of
crap on bubbling ect...anyways, went the route you described which was
exactly what I was looking to do. Was mostly confused on how the
custom events related back to intrinsic events for the the internal
user controls (read as, exposing internal control events).

David <df*****@127.0. 0.1> wrote in message news:<sl******* *************@w oofix.local.dom >...
In article <3b************ **************@ posting.google. com>, Andy wrote:

I'm not sure I totally understand this, but...
Alright, I am beyond confused here and need some guidance. I need a
C# only sample. I have a simple Page and within it i am creating a
user control (ascx). The user control contains textboxes, buttons,
ect. I would like to catch/add a handler/recieve the Button.OnClick
event from the buttons that are in the user control, in the Page.
How? As far as I can tell, my only route is to:


Well, there's three choices.
1. Use BubbleEvent

2. Make the button public and let the owner page attach the event
directly

3. Define your own event that runs in response to Button.OnClick,
and have the page attach to that event (I suspect this is what you
really want here).

RaiseBubbleEven t in the Button.OnClick in the UserControl
Override the OnBubbleEvent in the UserControl
Override the OnBubbleEvent in the Page from the already Override(n)
UserControl

This is stupid. All I want a custom event, call it CustomEvent with
what ever signature I want, and I would like to allow the instance to
have the event CustomEvent override(n). Please help. I must be
missing the bus on this one...I can't seem to find a good C# sample
for what seems so common/trivial. TIA


I'm not sure you actually have a real need for BubbleEvent here.
BubbleEvent is largely for those situation where there's a lot of
dynamic control creation going on. If all you want is a custom event,
why not just define one and use standard delegate/event stuff, and skip
the BubbleEvent stuff altogether? Am I missing something?

// inside the control

public event EventHandler MyEvent;

private void MyButton_OnClic k(object sender, EventArgs e)
{
if(MyEvent != null)
MyEvent(this, e); // or whatever args you want
}

// and inside the Page object

private void Page_Load(...)
{
MyUserControl.M yEvent += new EventHandler(my Handler);
}
I hope this helps, though I feel as if I'm actually missing something
here.

Nov 17 '05 #4
Think my brain was miss firing yesterday from reading the mounds of
crap on bubbling ect...anyways, went the route you described which was
exactly what I was looking to do. Was mostly confused on how the
custom events related back to intrinsic events for the the internal
user controls (read as, exposing internal control events).

David <df*****@127.0. 0.1> wrote in message news:<sl******* *************@w oofix.local.dom >...
In article <3b************ **************@ posting.google. com>, Andy wrote:

I'm not sure I totally understand this, but...
Alright, I am beyond confused here and need some guidance. I need a
C# only sample. I have a simple Page and within it i am creating a
user control (ascx). The user control contains textboxes, buttons,
ect. I would like to catch/add a handler/recieve the Button.OnClick
event from the buttons that are in the user control, in the Page.
How? As far as I can tell, my only route is to:


Well, there's three choices.
1. Use BubbleEvent

2. Make the button public and let the owner page attach the event
directly

3. Define your own event that runs in response to Button.OnClick,
and have the page attach to that event (I suspect this is what you
really want here).

RaiseBubbleEven t in the Button.OnClick in the UserControl
Override the OnBubbleEvent in the UserControl
Override the OnBubbleEvent in the Page from the already Override(n)
UserControl

This is stupid. All I want a custom event, call it CustomEvent with
what ever signature I want, and I would like to allow the instance to
have the event CustomEvent override(n). Please help. I must be
missing the bus on this one...I can't seem to find a good C# sample
for what seems so common/trivial. TIA


I'm not sure you actually have a real need for BubbleEvent here.
BubbleEvent is largely for those situation where there's a lot of
dynamic control creation going on. If all you want is a custom event,
why not just define one and use standard delegate/event stuff, and skip
the BubbleEvent stuff altogether? Am I missing something?

// inside the control

public event EventHandler MyEvent;

private void MyButton_OnClic k(object sender, EventArgs e)
{
if(MyEvent != null)
MyEvent(this, e); // or whatever args you want
}

// and inside the Page object

private void Page_Load(...)
{
MyUserControl.M yEvent += new EventHandler(my Handler);
}
I hope this helps, though I feel as if I'm actually missing something
here.

Nov 17 '05 #5

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

Similar topics

16
6680
by: schneider | last post by:
I can't get any of the Key events to fire in my UserControl. The control only contains a few labels. This is really annoying.. Anyone know whats going on? Thanks, Schneider
0
1292
by: Beatniks | last post by:
Is it possible to define a custom event for a contentplaceholder? I've got a chain of bubbling events: Button>repeater>childPage--- <ContentPlaceHolder> --- MasterPage In the childPage I get a reference to the MasterPage with: Dim myPage As MasterPage myPage = CType(Me.Master, MasterPage) myPage.FinishMyStuff()
3
1277
by: Dale | last post by:
I have an application that, on my development PC and in production, throws unexpected null reference exceptions when returning from the click event handler of a button. The only fix is to do an IISReset or to reboot the server. This is obviously not acceptable in production. The call stack shows non-user code, and my code works most of the time, so I have to assume it is internal to ASP.Net. The exception never occurs in the hander,...
1
3514
by: Apu Nahasapeemapetilon | last post by:
Hello and thank you in advance for your help. Can anyone think of a reason why this code would work properly on one PC, but not another? I've got a System.Windows.Forms.UserControl that products events which I want to consume (sink) within Internet Explorer. I'm following the instructions at: ms-http://support.microsoft.com/default.aspx?kbid=313891.
9
5138
by: Rob | last post by:
When a custom control is used within a form, that control does not get events directly. I've run into this a couple times with both mouse and keyboard events. Ex: A custom control inherits from UserControl (or Panel, etc). If that particular control is being edited, it IS possible to assign handlers and get events. However, when the control is used on the main form, assigning a handler to the particular control does nothing--the...
10
2432
by: Benton | last post by:
Hi there, I have a UserControl with a couple of textboxes and a couple of buttons ("Save" and "Cancel"). The Click event for this buttons is in the UserControl's codebehind of course, so here's my question. Once the UserControl is dropped onto the container page, how can I perform some action on the codebehind of the container page from the codebehind of the UserControl? For instance, suppose that the UserControl is dropped inside one...
5
1546
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hello, I am aware that in ASP.NET there is the concept of event bubbling. What I am after is some advice on achieveing the same thing in a standard application. Say I have 3 classes. View, Controller and Model. View calls Controller and then Controller calls Model. I would like Model to raise events which the View can handle.
0
1187
by: qreg | last post by:
I've got a problem with usercontrols and delegating events from the controls that are inside such a usercontrol control. First for a brief description of the problem. I have a user control containing a label and a textbox. I need to linkt the event like click(), mousemove, mousedown etc. in such a manner that clicking the control inside the user control (i.e. label) would result in fireing the event for the usercontrol. This would not be...
0
9721
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
9600
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
10628
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
10373
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...
0
9195
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5547
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
4331
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
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.