Hey John: Here's the sample (sry for the delay, I was away for the weekend):
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
ArrayList DynamicControls
{
get
{
if (ViewState["DynamicControls"] == null)
{
ViewState["DynamicControls"] = new ArrayList();
}
return (ArrayList)ViewState["DynamicControls"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
for (int i = 0; i < DynamicControls.Count; i++)
{
string s = (string)DynamicControls[i];
Control c = null;
switch (s)
{
case "t":
c = new TextBox();
break;
case "d":
c = new DropDownList();
break;
case "l":
c = new Label();
break;
}
if (c != null)
{
_ph.Controls.Add(c);
}
}
}
}
protected void _button1_Click(object sender, EventArgs e)
{
TextBox t = new TextBox();
_ph.Controls.Add(t);
DynamicControls.Add("t");
}
protected void _button2_Click(object sender, EventArgs e)
{
DropDownList d = new DropDownList();
_ph.Controls.Add(d);
d.Items.Add("Red");
d.Items.Add("Green");
d.Items.Add("Blue");
DynamicControls.Add("d");
}
protected void _button3_Click(object sender, EventArgs e)
{
Label l = new Label();
_ph.Controls.Add(l);
l.Text = DateTime.Now.ToString();
DynamicControls.Add("l");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat=server ID=_button1 Text="Add TextBox" OnClick="_button1_Click"
/>
<asp:Button runat=server ID=_button2 Text="Add DropDownList" OnClick="_button2_Click"
/>
<asp:Button runat="server" ID="_button3" Text="Add Label" OnClick="_button3_Click"
/>
<br />
<asp:PlaceHolder runat="server" ID="_ph"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
-Brock
DevelopMentor
http://staff.develop.com/ballen Brock,
Please could you perhaps just post a small example showing what you
stated in your previous post?
I've just tried what you suggested by "remembering" to load the page
and it does load but now I've lost my viewstate.
An example would do me a world of good right now. Please.
It's 3 in the morning and I've gotta sleep a bit.
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:48**********************@msnews.microsoft.com ...
So create the control the first time in the button click, and
"remember"
that you created that control. Then it's up to you to recreate all
the
right controls on the page upon every postback. This is typically
done in
Page_Init, Page_Load or by overriding CreateChildControls.
-Brock
DevelopMentor
http://staff.develop.com/ballen Hi all,
I need finality on this once and for all please.
I have a main page which contains a couple of placeholders and
within these placeholders, depending on what the user presses, I
load different user controls. This loading of user controls is done
within the code-behind of the main page. The problem is that before
loading a user control in place of another, I need to do a
Controls.Add then a Controls.Remove so the viewstate is loaded then
cleared so I can continue.
Now I have posted this message in different fomrs on this NG before
but never had viable solutions to my problem.
Surely people out there are using dynamic user controls (and simply
placing 20 user controls inside an area on a page and setting the
visible property isn't practical).
Please someone, provide me with an explanation of how to do this
correctly. Providing pointers to some web sites that explain this
(and not just viewstate) would really help me. I'm not impartial to
reading, believe me. I really need an understaing of this once and
for all - again, please.
Regards
John.