Groups | Blog | Home
all groups > asp.net building controls > october 2004 >

asp.net building controls : Composite Control - Event not firing in child control


Chris Simmons
10/28/2004 2:31:55 PM
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
10/28/2004 3:02:58 PM
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
[quoted text, click to view]

--
Thanks,
Chris Simmons
Chris Simmons
10/28/2004 4:10:05 PM
(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
10/28/2004 5:45:08 PM
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
[quoted text, click to view]

--
Thanks,
Chris Simmons
AddThis Social Bookmark Button