**** c# ***
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
CreateCtrls();
}
private void CreateCtrls()
{
DropDownList ddl1 = new DropDownList();
ddl1.ID = "ddl1";
ddl1.Items.Add("asdf0");
ddl1.Items.Add("asdf1");
ddl1.Items.Add("asdf2");
ddl1.Items.Add("asdf3");
pnl.Controls.Add(ddl1);
DropDownList ddl2 = new DropDownList();
ddl2.ID = "ddl2";
ddl2.Items.Add("lkjh0");
ddl2.Items.Add("lkjh1");
ddl2.Items.Add("lkjh2");
ddl2.Items.Add("lkjh3");
pnl.Controls.Add(ddl2);
ViewState["ControlsAdded2"] = true;
}
protected void btn1_Click(object sender, EventArgs e)
{
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (ViewState["controlsadded1"] == null)
{
CreateCtrls();
}
}
**** aspx ***
<body>
<form id="form1" runat="server">
<div>
<asp:Panel runat="server" ID="pnl">
</asp:Panel>
<asp:Button runat="server" ID="btn1" onclick="btn1_Click" Text="click me" />
</div>
</form>
</body>
|