No events are fired in child control

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default No events are fired in child control

    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 Guest

  2. Similar Questions and Discussions

    1. HELP!! Datagrid Child Control Events
      Hi All Does anyone have an idea on how to do the following? In the editmode template of a datagrid control I have 2 dropdownlist controls ...
    2. WebControl - Child control events
      Have you added the button to the 'Controls' property of the custom-web control? How are you rendering the button? Well, "runat" attribute is not...
    3. Notify child control of events in parent control
      I have a parent control that has a imagebutton on it, with code to handle the click event. I also have various ascx controls that are loaded by the...
    4. Events of datagrid in user control not fired
      Hi, I have a user control which contains a datagrid. The page hosting the user control passes to it the values, which the user control transforms...
    5. Catching events in web form fired by user control
      I'm trying to process an event raised by a user control in the web form that contains that control. I've fathomed out how to handle the event...
  3. #2

    Default Re: No events are fired in child control

    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" <msdn@csite.com> wrote in message
    news:O45w3Zm5DHA.2764@TK2MSFTNGP09.phx.gbl...
    > 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 Guest

  4. #3

    Default RE: No events are fired in child control


    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:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm[/url]
    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! - [url]www.microsoft.com/security[/url]
    This posting is provided "as is" with no warranties and confers no rights.

    Jeffrey Tan[MSFT] Guest

  5. #4

    Default Re: No events are fired in child control

    Hi Jeffrey.

    I implemented INamingContainer in the control and events now work.

    Thanks,
    Casper


    ""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
    news:bxHHiov5DHA.1992@cpmsftngxa07.phx.gbl...
    >
    > 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:
    >
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm[/url]
    > 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! - [url]www.microsoft.com/security[/url]
    > This posting is provided "as is" with no warranties and confers no rights.
    >

    Casper Hornstrup Guest

  6. #5

    Default Re: No events are fired in child control

    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]"" <v-jetan@online.microsoft.com> wrote in message
    news:bxHHiov5DHA.1992@cpmsftngxa07.phx.gbl...
    >
    > 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:
    >
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm[/url]
    > 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! - [url]www.microsoft.com/security[/url]
    > This posting is provided "as is" with no warranties and confers no rights.
    >

    Alessandro Zifiglio Guest

  7. #6

    Default Re: No events are fired in child control


    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! - [url]www.microsoft.com/security[/url]
    This posting is provided "as is" with no warranties and confers no rights.

    Jeffrey Tan[MSFT] Guest

  8. #7

    Default Re: No events are fired in child control


    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! - [url]www.microsoft.com/security[/url]
    This posting is provided "as is" with no warranties and confers no rights.

    Jeffrey Tan[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139