Professional Web Applications Themes

Composite Control - Event not firing in child control - ASP.NET Building Controls

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 ...

  1. #1

    Default Composite Control - Event not firing in child control

    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
    [email]newsgroup.repliesnetchris.com[/email]
    Chris Simmons Guest

  2. #2

    Default Re: Composite Control - Event not firing in child control

    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
    <newsgroup.repliesnetchris.com> 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
    [email]newsgroup.repliesnetchris.com[/email]
    Chris Simmons Guest

  3. #3

    Default Re: Composite Control - Event not firing in child control

    (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
    [email]newsgroup.repliesnetchris.com[/email]
    Chris Simmons Guest

  4. #4

    Default Re: Composite Control - Event not firing in child control

    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
    <newsgroup.repliesnetchris.com> 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
    [email]newsgroup.repliesnetchris.com[/email]
    Chris Simmons Guest

Similar Threads

  1. Firing user control event from parent control
    By Natty Gur in forum ASP.NET Building Controls
    Replies: 2
    Last Post: February 8th, 09:09 AM
  2. Capture an Event in a composite control From control on a page
    By gdick@kerrhenderson.com in forum ASP.NET Building Controls
    Replies: 1
    Last Post: August 13th, 08:51 AM
  3. Composite Control - How to Expose a Child Control
    By dsmolen@gmail.com in forum ASP.NET Building Controls
    Replies: 0
    Last Post: September 28th, 07:13 PM
  4. Replies: 2
    Last Post: July 22nd, 10:58 PM
  5. Replies: 1
    Last Post: April 8th, 11:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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