Hello Joel,
Thanks for posting in the group.
Since the control(s) are added before the page is rendered (dynamically on
the server), they don't exist until you re-add them at some point before
the page is rendered. Page and its controls are created/rendered/destroyed
on every request and there is nothing in the framework that automatically
remeber which controls were previously loaded/created. So we need to create
it again in postback. It's just the way .NET works with dynamically added
controls.
Here are some sample code for your reference: (Please add control in
Page_Init method)
WebControl:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace DynPanel
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,
INamingContainer
{
private RadioButtonList rblTemp = new RadioButtonList();
private Label lblTemp = new Label();
protected override void CreateChildControls()
{
Controls.Add(new LiteralControl("<P><B>"));
lblTemp.ID = "lblTest";
lblTemp.Text = "Label Test";
Controls.Add(lblTemp);
Controls.Add(new LiteralControl("</B>"));
rblTemp.ID = "rblTest";
rblTemp.Items.Add("A");
rblTemp.Items.Add("B");
rblTemp.Items.Add("C");
rblTemp.Items.Add("D");
Controls.Add(rblTemp);
Controls.Add(new LiteralControl("<BR><HR>"));
}
}
}
Button1_Click Event:
private void Button1_Click(object sender, System.EventArgs e)
{
WebCustomControl1 wcc1Temp = ((WebCustomControl1)Panel1.Controls[0]);
RadioButtonList rblTemp =
((RadioButtonList)wcc1Temp.FindControl("rblTest"));
Response.Write(rblTemp.SelectedItem.Text);
}
I also used the OnInit event to load the composite control as well. Here
is the code used for that:
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
WebCustomControl1 wcc1Temp = new WebCustomControl1();
Panel1.Controls.Clear();
Panel1.Controls.Add(wcc1Temp);
}
By the way, in the future, it would be best to post these questions in the
following newsgroup.
Microsoft.public.dotnet.aspnet
All .NET asp.net issues, configuration and other questions are posted in
the newsgroup above.
The reason why we recommend posting appropriately is you will get the most
qualified pool of respondents, and other partners who the newsgroups
regularly can either share their knowledge or learn from your interaction
with us. Also, this is to make sure that the responders can better track
the problem. Thank you for your understanding.
Have a nice day!
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! ¨C
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.