No events are fired in child control

Discussion in 'ASP .Net Building Controls' started by Casper Hornstrup, Jan 29, 2004.

  1. I have developed a custom web control that may contain any child controls.
    Now I have a problem that even though I register an event handler on a child
    control,
    the event handler does not fire when it should. Do I need to handle event
    registrations
    of child controls specially?

    Casper
     
    Casper Hornstrup, Jan 29, 2004
    #1
    1. Advertising

  2. hi casper,
    Your going to have to override your onBubbleEvent method for your control
    and take action here or bubble it up futher to the container(the page).
    providing a common event handler for all events fired. To differeniate btw
    controls and who is posting back your end users will have to supply a name
    to the buttons commandName property which is exposed by controls that
    postback and then let them use this to handle handle events in one common
    handler.


    "Casper Hornstrup" <> wrote in message
    news:...
    > I have developed a custom web control that may contain any child controls.
    > Now I have a problem that even though I register an event handler on a

    child
    > control,
    > the event handler does not fire when it should. Do I need to handle event
    > registrations
    > of child controls specially?
    >
    > Casper
    >
    >
     
    Alessandro Zifiglio, Jan 29, 2004
    #2
    1. Advertising

  3. Hi Casper,

    Thank you for posting in the community!

    Based on my understanding, you are building a custom control, in which you
    created several child controls. Althrough you have added event handler for
    your child control, this event handle does not take effect.

    ===================================================
    Where do you create your child controls? Do you create your child control
    through override Control.CreateChildControls?

    Acutally, in Composite Control, if you want to hook the child controls'
    event, you must implement the System.Web.UI.INamingContainer interface. It
    will provide you the unique names in the hierarchical tree of controls. If
    you do not implement this interface, asp.net model will not associate the
    event handler well.

    For example, this code will work well:
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace CustomControls
    {
    public class EventBubbler : System.Web.UI.WebControls.WebControl,
    INamingContainer
    {
    protected override void CreateChildControls()
    {
    Button button1 = new Button();
    button1.Text = "Click";
    button1.Click+=new EventHandler(button1_Click);
    Controls.Add(button1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
    this.Page.Response.Write("Child button event fires");
    }
    }
    }

    But this will not fire your event:
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace CustomControls
    {
    public class EventBubbler : System.Web.UI.WebControls.WebControl
    {
    protected override void CreateChildControls()
    {
    Button button1 = new Button();
    button1.Text = "Click";
    button1.Click+=new EventHandler(button1_Click);
    Controls.Add(button1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
    this.Page.Response.Write("Child button event fires");
    }
    }
    }

    For more information, please refer to "Developing a Composite Control" at:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
    l/cpcondevelopingcompositecontrols.asp

    ================================================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.
    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Jan 30, 2004
    #3
  4. Hi Jeffrey.

    I implemented INamingContainer in the control and events now work.

    Thanks,
    Casper


    ""Jeffrey Tan[MSFT]"" <> wrote in message
    news:...
    >
    > Hi Casper,
    >
    > Thank you for posting in the community!
    >
    > Based on my understanding, you are building a custom control, in which you
    > created several child controls. Althrough you have added event handler for
    > your child control, this event handle does not take effect.
    >
    > ===================================================
    > Where do you create your child controls? Do you create your child control
    > through override Control.CreateChildControls?
    >
    > Acutally, in Composite Control, if you want to hook the child controls'
    > event, you must implement the System.Web.UI.INamingContainer interface. It
    > will provide you the unique names in the hierarchical tree of controls. If
    > you do not implement this interface, asp.net model will not associate the
    > event handler well.
    >
    > For example, this code will work well:
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    >
    > namespace CustomControls
    > {
    > public class EventBubbler : System.Web.UI.WebControls.WebControl,
    > INamingContainer
    > {
    > protected override void CreateChildControls()
    > {
    > Button button1 = new Button();
    > button1.Text = "Click";
    > button1.Click+=new EventHandler(button1_Click);
    > Controls.Add(button1);
    > }
    >
    > private void button1_Click(object sender, EventArgs e)
    > {
    > this.Page.Response.Write("Child button event fires");
    > }
    > }
    > }
    >
    > But this will not fire your event:
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    >
    > namespace CustomControls
    > {
    > public class EventBubbler : System.Web.UI.WebControls.WebControl
    > {
    > protected override void CreateChildControls()
    > {
    > Button button1 = new Button();
    > button1.Text = "Click";
    > button1.Click+=new EventHandler(button1_Click);
    > Controls.Add(button1);
    > }
    >
    > private void button1_Click(object sender, EventArgs e)
    > {
    > this.Page.Response.Write("Child button event fires");
    > }
    > }
    > }
    >
    > For more information, please refer to "Developing a Composite Control" at:
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
    > l/cpcondevelopingcompositecontrols.asp
    >
    > ================================================================
    > Please apply my suggestion above and let me know if it helps resolve your
    > problem.
    >
    > Thank you for your patience and cooperation. If you have any questions or
    > concerns, please feel free to post it in the group. I am standing by to be
    > of assistance.
    > Have a nice day!!
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
     
    Casper Hornstrup, Jan 30, 2004
    #4
  5. hi Jeffery, thanks for pointing this out. Somehow I had always missed that
    part on the docs where it says that implementing the INamingContainer
    interface is important also when you need to route events to its child
    controls. I was always under the impression that when inheriting from
    webControls this interface was useless the reason being that I started
    facing naming problems for child controls when trying to pass an explicit ID
    whereas to the generic uniqueID supplied by asp.net

    However now i see that supplying "clientID" & "_name" or me.ID & "_name" to
    a child controls ID is wrong. This has just fixed a bug a bug for me in my
    control.
    Thanks again ;)

    ""Jeffrey Tan[MSFT]"" <> wrote in message
    news:...
    >
    > Hi Casper,
    >
    > Thank you for posting in the community!
    >
    > Based on my understanding, you are building a custom control, in which you
    > created several child controls. Althrough you have added event handler for
    > your child control, this event handle does not take effect.
    >
    > ===================================================
    > Where do you create your child controls? Do you create your child control
    > through override Control.CreateChildControls?
    >
    > Acutally, in Composite Control, if you want to hook the child controls'
    > event, you must implement the System.Web.UI.INamingContainer interface. It
    > will provide you the unique names in the hierarchical tree of controls. If
    > you do not implement this interface, asp.net model will not associate the
    > event handler well.
    >
    > For example, this code will work well:
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    >
    > namespace CustomControls
    > {
    > public class EventBubbler : System.Web.UI.WebControls.WebControl,
    > INamingContainer
    > {
    > protected override void CreateChildControls()
    > {
    > Button button1 = new Button();
    > button1.Text = "Click";
    > button1.Click+=new EventHandler(button1_Click);
    > Controls.Add(button1);
    > }
    >
    > private void button1_Click(object sender, EventArgs e)
    > {
    > this.Page.Response.Write("Child button event fires");
    > }
    > }
    > }
    >
    > But this will not fire your event:
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    >
    > namespace CustomControls
    > {
    > public class EventBubbler : System.Web.UI.WebControls.WebControl
    > {
    > protected override void CreateChildControls()
    > {
    > Button button1 = new Button();
    > button1.Text = "Click";
    > button1.Click+=new EventHandler(button1_Click);
    > Controls.Add(button1);
    > }
    >
    > private void button1_Click(object sender, EventArgs e)
    > {
    > this.Page.Response.Write("Child button event fires");
    > }
    > }
    > }
    >
    > For more information, please refer to "Developing a Composite Control" at:
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
    > l/cpcondevelopingcompositecontrols.asp
    >
    > ================================================================
    > Please apply my suggestion above and let me know if it helps resolve your
    > problem.
    >
    > Thank you for your patience and cooperation. If you have any questions or
    > concerns, please feel free to post it in the group. I am standing by to be
    > of assistance.
    > Have a nice day!!
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
     
    Alessandro Zifiglio, Jan 30, 2004
    #5
  6. Hi Casper,

    Thanks for your feedback.

    I am glad it works.

    If you have any further concern, please feel free to tell me, I will work
    with you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Jan 30, 2004
    #6
  7. Hi Alessandro,

    Yes, I also through INamingContainer is useless in the past, but not now.
    :)
    You are welcome.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Jan 30, 2004
    #7
    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. Nick Lewis
    Replies:
    1
    Views:
    454
    Gary McCormack
    Aug 1, 2003
  2. Gunjan Garg
    Replies:
    0
    Views:
    1,780
    Gunjan Garg
    Jul 12, 2005
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,182
    David Schwartz
    Dec 9, 2003
  4. Emmanuel

    Events of datagrid in user control not fired

    Emmanuel, Jun 28, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    105
    Emmanuel
    Jun 28, 2004
  5. Justin

    How do I capture events fired in child user controls?

    Justin, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    251
    Justin
    Jan 27, 2004
Loading...

Share This Page