RestoreControlTree
Markup:
<%@ Register TagPrefix="cc1"
Namespace="TemplatedSampleSite.aspnet_forum"
Assembly="TemplatedSampleSite"
%>
<%@ Page language="c#"
Codebehind="RestoreControlTree_Example.aspx.cs"
AutoEventWireup="false"
Inherits="TemplatedSampleSite.aspnet_forum.RestoreControlTree_Example"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN" >
<HTML>
<HEAD>
<title>RestroreControlTreeExample</title>
<meta
name="GENERATOR" Content="Microsoft Visual Studio
7.0">
<meta name="CODE_LANGUAGE"
Content="C#">
<meta
name="vs_defaultClientScript" content="JavaScript">
<meta
name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body
MS_POSITIONING="FlowLayout">
<form
id="RestroreControlTreeExample" method="post"
runat="server">
<P>
<cc1:RestoreMyChildren
id="LoadWriteUserControl1" runat="server" />
<asp:Button
id="Button1" runat="server"
Text="Button"></asp:Button></P>
<P> </P>
<P> </P>
<P>
<asp:Button
id="Button2" runat="server"
Text="Nothing"></asp:Button></P>
</form>
</body>
</HTML>
A codebehind and a class.
using System;
using System.Collections;
using
System.ComponentModel;
using System.Data;
using
System.Drawing;
using System.Web;
using
System.Web.SessionState;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
namespace TemplatedSampleSite.aspnet_forum
{
///
<summary>
/// Summary description for
RestroreControlTreeExample.
/// </summary>
public
class RestoreControlTree_Example : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.Button Button1;
protected
System.Web.UI.WebControls.Button Button2;
protected
TemplatedSampleSite.aspnet_forum.RestoreMyChildren
LoadWriteUserControl1;
private void
Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
LoadWriteUserControl1.DataSource
= FakeData;
LoadWriteUserControl1.DataBind();
}
}
protected
ArrayList FakeData
{
get
{
ArrayList
temp = new ArrayList();
temp.Add(new
string[]{"1","USD"});
temp.Add(new
string[]{"2","CAD"});
return temp;
}
}
#region
Web Form Designer generated code
override protected
void OnInit(EventArgs e)
{
//
//
CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
<summary>
/// Required method for Designer
support - do not modify
/// the contents of this
method with the code editor.
/// </summary>
private
void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load
+= new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs
e)
{
TextBox innerBox =
LoadWriteUserControl1.FindControl("1") as TextBox;
Response.Write(innerBox.Text
+ "<HR>");
innerBox =
LoadWriteUserControl1.FindControl("2") as TextBox;
Response.Write(innerBox.Text
+ "<HR>");
Label innerlabel =
LoadWriteUserControl1.FindControl("Label2") as Label;
innerlabel.Text
= "BOO";
}
}
public class
RestoreMyChildren : Control
{
ArrayList
_dataSource;
public ArrayList DataSource
{
get
{
return
_dataSource;
}
set
{
_dataSource
= value;
}
}
private object[] Data
{
get
{
return
ViewState["Data"] as object[];
}
set
{
ViewState["Data"]
= value;
}
}
override protected void
LoadViewState(object savedState)
{
base.LoadViewState(savedState);
EnsureChildControls();
}
protected
override void OnDataBinding(System.EventArgs e)
{
PrepareData();
base.OnDataBinding(e);
}
private
void PrepareData()
{
if(DataSource==null)
{
return;
}
object[]
buildData = new
object[DataSource.Count];//2,DataSource.Count];
for(int
index=0;index<DataSource.Count;index++)
{
string[]
item = DataSource[index] as string[];
buildData[index]
= new string[]{item[0],item[1]};
}
Data =
buildData;
}
protected override void
CreateChildControls()
{
base.CreateChildControls();
foreach(object
dataItem in Data)
{
string[] item =
dataItem as string[];
this.Controls.Add(new
LiteralControl("<HR>"));
Label
label = new Label();
label.ID="Label" + item[0];
label.Text=item[1];
this.Controls.Add(label);
TextBox textbox = new TextBox();
textbox.ID=item[0];
textbox.Text=item[1];
this.Controls.Add(textbox);
this.Controls.Add(new
LiteralControl("<HR>"));
}
}
}
}