C# UserControl Bubbling events

Discussion in 'ASP .Net' started by Andy, Oct 27, 2003.

  1. Andy

    Andy Guest

    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:

    RaiseBubbleEvent 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
     
    Andy, Oct 27, 2003
    #1
    1. Advertising

  2. I might well be missing something here but...

    You can simplify what you do. The simplest is:
    1) Call RaiseBubbleEvent 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
    RaiseBubbleEvent.

    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.EventArgs
    2) Define a delgate event handler: MyEvent( object source, MyEventArgs
    args)
    3) Call RaiseBubbleEvent 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
    RaiseBubbleEvent.

    HTH
    Charles

    "Andy" <> wrote in message
    news:...
    > 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:
    >
    > RaiseBubbleEvent 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
     
    Charles Rumbold, Oct 27, 2003
    #2
    1. Advertising

  3. Andy

    David Guest

    In article <>, 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).

    >
    > RaiseBubbleEvent 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_OnClick(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.MyEvent += new EventHandler(myHandler);
    }


    I hope this helps, though I feel as if I'm actually missing something
    here.


    --
    David
    dfoster at
    hotpop dot com
     
    David, Oct 27, 2003
    #3
  4. Andy

    Andy Guest

    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 <dfoster@127.0.0.1> wrote in message news:<>...
    > In article <>, 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).
    >
    > >
    > > RaiseBubbleEvent 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_OnClick(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.MyEvent += new EventHandler(myHandler);
    > }
    >
    >
    > I hope this helps, though I feel as if I'm actually missing something
    > here.
     
    Andy, Oct 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    1
    Views:
    493
  2. =?Utf-8?B?RGFsZQ==?=

    Internal ASP.Net error bubbling events

    =?Utf-8?B?RGFsZQ==?=, Apr 11, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    487
    Steven Cheng[MSFT]
    Apr 14, 2006
  3. Javi

    Composite Controls and Bubbling events

    Javi, Jul 8, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    180
  4. Anonymous Poster

    Bubbling up custom events in datagrid

    Anonymous Poster, Jan 25, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    157
    Anonymous Poster
    Jan 25, 2005
  5. a

    Bubbling Up Events

    a, Nov 9, 2003, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    163
Loading...

Share This Page