Composite Control - Event not firing in child control

Discussion in 'ASP .Net Building Controls' started by Chris Simmons, Oct 28, 2004.

  1. Hello:

    I am experiencing an issue where I have a composite control
    (TestOuter) composed of more composite (TestInner) controls. When I
    am seeing is if the TestInner control is placed on a webform, the
    events of the control fire. If TestInner controls are placed within
    the TestOuter control, these events never fire.

    Here's some sample code:

    // BEGIN CODE
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace MyControls
    {
    /// <summary>
    /// Inner collection of RadioButtons
    /// </summary>
    public class TestInner : WebControl, INamingContainer
    {

    public TestInner() : base( HtmlTextWriterTag.Div )
    {
    }

    public override ControlCollection Controls
    {
    get
    {
    EnsureChildControls();
    return base.Controls;
    }
    }

    RadioButton firstRadioButton, secondRadioButton;

    Label output;

    protected override void CreateChildControls()
    {
    firstRadioButton = new RadioButton();
    firstRadioButton.ID = UniqueID + "_first";
    firstRadioButton.Text = "First";
    firstRadioButton.GroupName = UniqueID;
    firstRadioButton.CheckedChanged +=
    new EventHandler(firstRadioButton_CheckedChanged);
    firstRadioButton.AutoPostBack = true;
    Controls.Add( firstRadioButton );

    secondRadioButton = new RadioButton();
    secondRadioButton.ID = UniqueID + "_second";
    secondRadioButton.Text = "Second";
    secondRadioButton.GroupName = UniqueID;
    secondRadioButton.CheckedChanged +=
    new EventHandler(secondRadioButton_CheckedChanged);
    secondRadioButton.AutoPostBack = true;
    Controls.Add( secondRadioButton );


    output = new Label();

    Controls.Add( output );
    }

    private void firstRadioButton_CheckedChanged(object sender,
    EventArgs e)
    {
    output.Text = "First chosen!";
    }

    private void secondRadioButton_CheckedChanged(object sender,
    EventArgs e)
    {
    output.Text = "Second chosen!";
    }
    }
    }

    // END CODE

    When added to a webform, ViewState is maintained (the radio buttons
    remain where they were clicked) between postbacks. Now, if I create a
    control which is composed of two of these controls, the events
    (first_CheckedChanged or second_CheckedChanged) for the respective
    TestInner controls do not fire.

    Here is the sample code for TestOuter:

    // BEGIN CODE

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace Com.Fpl.Pgen.RiskManagement.Web.Controls
    {
    /// <summary>
    /// Inner collection of RadioButtons
    /// </summary>
    public class TestOuter : WebControl, INamingContainer
    {

    public TestOuter() : base( HtmlTextWriterTag.Div )
    {
    }

    public override ControlCollection Controls
    {
    get
    {
    EnsureChildControls();
    return base.Controls;
    }
    }

    TestInner firstTestInner, secondTestInner;

    protected override void CreateChildControls()
    {
    firstTestInner =
    new TestInner();
    Controls.Add( first );

    secondTestInner =
    new TestInner();
    Controls.Add( second );
    }

    }
    }

    // END CODE

    Would someone be so kind as to show me where I'm fouling up? How can
    I make the TestInner's (first and second) events fire when encased in
    another control?

    --
    Thanks,
    Chris Simmons
    Chris Simmons, Oct 28, 2004
    #1
    1. Advertising

  2. Sorry, to clarify I have both a TestInner and a TestOuter on the
    webform. The TestOuter, as you can see from the second snippet,
    contains two TestInners. The events (firstRadioButton_CheckedChanged
    and secondRadioButton_CheckedChanged) on the single TestInner on the
    webform fire just fine and ViewState is maintained when one of the
    radio buttons in the single TestInner are clicked. When one of the
    RadioButtons in TestOuter is clicked, postback occurs but no event is
    fired and ViewState is not maintained (the RadioButtons revert back to
    unchecked).

    On Thu, 28 Oct 2004 14:31:55 -0400, Chris Simmons
    <> wrote:

    >Hello:
    >
    >I am experiencing an issue where I have a composite control
    >(TestOuter) composed of more composite (TestInner) controls. When I
    >am seeing is if the TestInner control is placed on a webform, the
    >events of the control fire. If TestInner controls are placed within
    >the TestOuter control, these events never fire.
    >
    >Here's some sample code:
    >
    >// BEGIN CODE
    >using System;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.ComponentModel;
    >
    >namespace MyControls
    >{
    > /// <summary>
    > /// Inner collection of RadioButtons
    > /// </summary>
    > public class TestInner : WebControl, INamingContainer
    > {
    >
    > public TestInner() : base( HtmlTextWriterTag.Div )
    > {
    > }
    >
    > public override ControlCollection Controls
    > {
    > get
    > {
    > EnsureChildControls();
    > return base.Controls;
    > }
    > }
    >
    > RadioButton firstRadioButton, secondRadioButton;
    >
    > Label output;
    >
    > protected override void CreateChildControls()
    > {
    > firstRadioButton = new RadioButton();
    > firstRadioButton.ID = UniqueID + "_first";
    > firstRadioButton.Text = "First";
    > firstRadioButton.GroupName = UniqueID;
    > firstRadioButton.CheckedChanged +=
    > new EventHandler(firstRadioButton_CheckedChanged);
    > firstRadioButton.AutoPostBack = true;
    > Controls.Add( firstRadioButton );
    >
    > secondRadioButton = new RadioButton();
    > secondRadioButton.ID = UniqueID + "_second";
    > secondRadioButton.Text = "Second";
    > secondRadioButton.GroupName = UniqueID;
    > secondRadioButton.CheckedChanged +=
    > new EventHandler(secondRadioButton_CheckedChanged);
    > secondRadioButton.AutoPostBack = true;
    > Controls.Add( secondRadioButton );
    >
    >
    > output = new Label();
    >
    > Controls.Add( output );
    > }
    >
    > private void firstRadioButton_CheckedChanged(object sender,
    >EventArgs e)
    > {
    > output.Text = "First chosen!";
    > }
    >
    > private void secondRadioButton_CheckedChanged(object sender,
    >EventArgs e)
    > {
    > output.Text = "Second chosen!";
    > }
    > }
    >}
    >
    >// END CODE
    >
    >When added to a webform, ViewState is maintained (the radio buttons
    >remain where they were clicked) between postbacks. Now, if I create a
    >control which is composed of two of these controls, the events
    >(first_CheckedChanged or second_CheckedChanged) for the respective
    >TestInner controls do not fire.
    >
    >Here is the sample code for TestOuter:
    >
    >// BEGIN CODE
    >
    >using System;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.ComponentModel;
    >
    >namespace Com.Fpl.Pgen.RiskManagement.Web.Controls
    >{
    > /// <summary>
    > /// Inner collection of RadioButtons
    > /// </summary>
    > public class TestOuter : WebControl, INamingContainer
    > {
    >
    > public TestOuter() : base( HtmlTextWriterTag.Div )
    > {
    > }
    >
    > public override ControlCollection Controls
    > {
    > get
    > {
    > EnsureChildControls();
    > return base.Controls;
    > }
    > }
    >
    > TestInner firstTestInner, secondTestInner;
    >
    > protected override void CreateChildControls()
    > {
    > firstTestInner =
    > new TestInner();
    > Controls.Add( first );
    >
    > secondTestInner =
    > new TestInner();
    > Controls.Add( second );
    > }
    >
    > }
    >}
    >
    >// END CODE
    >
    >Would someone be so kind as to show me where I'm fouling up? How can
    >I make the TestInner's (first and second) events fire when encased in
    >another control?


    --
    Thanks,
    Chris Simmons
    Chris Simmons, Oct 28, 2004
    #2
    1. Advertising

  3. (Sorry to post so often)

    Here's a compact, reproducable representation of what I mean. Below
    is the index.aspx file and the code-behind. Placing these in an empty
    web project and compiling will reproduce the behavior I outlined in my
    previous posts (mainly, why will the RadioButtons in the TestOuter
    composite control not maintain ViewState?):

    index.aspx:
    --------------
    <%@ Page language="c#" Codebehind="index.aspx.cs"
    AutoEventWireup="false" Inherits="Test.index" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <html>
    <head>
    <title>Test</title>
    </head>
    <body >
    <form id="Form1" method="post" runat="server">
    <asp:panel id="outputPanel" runat="server"></asp:panel>
    </form>
    </body>
    </html>
    --------------
    // index.aspx.cs
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace Test
    {

    /// <summary>
    /// Code-behind for webform
    /// </summary>
    public class index : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Panel outputPanel;

    protected override void CreateChildControls()
    {
    outputPanel.Controls.Add( new TestInner() );
    outputPanel.Controls.Add( new TestOuter() );
    }
    }

    /// <summary>
    /// Inner collection of RadioButtons
    /// </summary>
    public class TestInner : CompositeControlDiv
    {

    RadioButton firstRadioButton, secondRadioButton;

    Label output;

    protected override void CreateChildControls()
    {
    Controls.Clear();
    firstRadioButton = new RadioButton();
    firstRadioButton.ID = UniqueID + "_first";
    firstRadioButton.Text = "First";
    firstRadioButton.GroupName = UniqueID;
    firstRadioButton.CheckedChanged +=
    new EventHandler( first_CheckedChanged );
    firstRadioButton.AutoPostBack = true;
    Controls.Add( firstRadioButton );

    secondRadioButton = new RadioButton();
    secondRadioButton.ID = UniqueID + "_second";
    secondRadioButton.Text = "Second";
    secondRadioButton.GroupName = UniqueID;
    secondRadioButton.CheckedChanged +=
    new EventHandler( second_CheckedChanged );
    secondRadioButton.AutoPostBack = true;
    Controls.Add( secondRadioButton );

    Controls.Add( new HtmlGenericControl( "br" ) );

    output = new Label();

    Controls.Add( output );
    }

    private void first_CheckedChanged(object sender, EventArgs e)
    {
    output.Text = "First chosen!";
    RaiseBubbleEvent( this, e );
    }

    private void second_CheckedChanged(object sender, EventArgs e)
    {
    output.Text = "Second chosen!";
    RaiseBubbleEvent( this, e );
    }

    }


    /// <summary>
    /// Collection of TestInners
    /// </summary>
    public class TestOuter : CompositeControlDiv
    {

    TestInner firstTestInner, secondTestInner;

    protected override void CreateChildControls()
    {
    Controls.Clear();
    firstTestInner =
    new TestInner();
    Controls.Add( firstTestInner );

    secondTestInner =
    new TestInner();
    Controls.Add( secondTestInner );
    }

    }

    public class CompositeControlDiv : WebControl, INamingContainer
    {
    public CompositeControlDiv() : base( HtmlTextWriterTag.Div )
    {
    }

    public override ControlCollection Controls
    {
    get
    {
    EnsureChildControls();
    return base.Controls;
    }
    }


    }
    }

    --
    Thanks,
    Chris Simmons
    Chris Simmons, Oct 28, 2004
    #3
  4. Nevermind. I found my problem. It was the reassignment of the
    RadioButton's ID. When I commented out this line like so:

    //firstRadioButton.ID = UniqueID + "_first";
    //secondRadioButton.ID = UniqueID + "_second";

    it worked fine.


    On Thu, 28 Oct 2004 16:10:05 -0400, Chris Simmons
    <> wrote:

    >(Sorry to post so often)
    >
    >Here's a compact, reproducable representation of what I mean. Below
    >is the index.aspx file and the code-behind. Placing these in an empty
    >web project and compiling will reproduce the behavior I outlined in my
    >previous posts (mainly, why will the RadioButtons in the TestOuter
    >composite control not maintain ViewState?):
    >
    >index.aspx:
    >--------------
    ><%@ Page language="c#" Codebehind="index.aspx.cs"
    >AutoEventWireup="false" Inherits="Test.index" %>
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    ><html>
    > <head>
    > <title>Test</title>
    > </head>
    > <body >
    > <form id="Form1" method="post" runat="server">
    > <asp:panel id="outputPanel" runat="server"></asp:panel>
    > </form>
    > </body>
    ></html>
    >--------------
    >// index.aspx.cs
    >using System;
    >using System.Web;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.Web.UI.HtmlControls;
    >
    >namespace Test
    >{
    >
    > /// <summary>
    > /// Code-behind for webform
    > /// </summary>
    > public class index : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Panel outputPanel;
    >
    > protected override void CreateChildControls()
    > {
    > outputPanel.Controls.Add( new TestInner() );
    > outputPanel.Controls.Add( new TestOuter() );
    > }
    > }
    >
    > /// <summary>
    > /// Inner collection of RadioButtons
    > /// </summary>
    > public class TestInner : CompositeControlDiv
    > {
    >
    > RadioButton firstRadioButton, secondRadioButton;
    >
    > Label output;
    >
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    > firstRadioButton = new RadioButton();
    > firstRadioButton.ID = UniqueID + "_first";
    > firstRadioButton.Text = "First";
    > firstRadioButton.GroupName = UniqueID;
    > firstRadioButton.CheckedChanged +=
    > new EventHandler( first_CheckedChanged );
    > firstRadioButton.AutoPostBack = true;
    > Controls.Add( firstRadioButton );
    >
    > secondRadioButton = new RadioButton();
    > secondRadioButton.ID = UniqueID + "_second";
    > secondRadioButton.Text = "Second";
    > secondRadioButton.GroupName = UniqueID;
    > secondRadioButton.CheckedChanged +=
    > new EventHandler( second_CheckedChanged );
    > secondRadioButton.AutoPostBack = true;
    > Controls.Add( secondRadioButton );
    >
    > Controls.Add( new HtmlGenericControl( "br" ) );
    >
    > output = new Label();
    >
    > Controls.Add( output );
    > }
    >
    > private void first_CheckedChanged(object sender, EventArgs e)
    > {
    > output.Text = "First chosen!";
    > RaiseBubbleEvent( this, e );
    > }
    >
    > private void second_CheckedChanged(object sender, EventArgs e)
    > {
    > output.Text = "Second chosen!";
    > RaiseBubbleEvent( this, e );
    > }
    >
    > }
    >
    >
    > /// <summary>
    > /// Collection of TestInners
    > /// </summary>
    > public class TestOuter : CompositeControlDiv
    > {
    >
    > TestInner firstTestInner, secondTestInner;
    >
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    > firstTestInner =
    > new TestInner();
    > Controls.Add( firstTestInner );
    >
    > secondTestInner =
    > new TestInner();
    > Controls.Add( secondTestInner );
    > }
    >
    > }
    >
    > public class CompositeControlDiv : WebControl, INamingContainer
    > {
    > public CompositeControlDiv() : base( HtmlTextWriterTag.Div )
    > {
    > }
    >
    > public override ControlCollection Controls
    > {
    > get
    > {
    > EnsureChildControls();
    > return base.Controls;
    > }
    > }
    >
    >
    > }
    >}


    --
    Thanks,
    Chris Simmons
    Chris Simmons, Oct 28, 2004
    #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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,084
    David Schwartz
    Dec 9, 2003
  2. debartsa

    Composite Control not receiving DataGrid (child control) events?

    debartsa, Apr 19, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    181
    debartsa
    Apr 19, 2004
  3. Chad
    Replies:
    0
    Views:
    209
  4. David Boike

    Child controls in Composite Controls not firing events

    David Boike, Aug 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    304
    David Boike
    Aug 7, 2003
  5. studen771
    Replies:
    0
    Views:
    494
    studen771
    Mar 28, 2007
Loading...

Share This Page