You ought to check out the DynamicPlaceHolder control instead of using the
builtin Placeholder.

[url]http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx[/url]

Denis has created a control that if you use it and add loaded controls to
it, it will take care of loading them for you so it will act as if it was a
built-in server control and the viewstate etc, will be managed for you
automatically.

HTH.
Ben Miller
ASP.NET MVP Lead

--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Utter Newbie" <dabba@yabbahey.com> wrote in message
news:ggptivsuv1meqlndiqa69qi1pvh134oua6@4ax.com...
> Does anyone have any experience loading controls dynamically? I've
> been trying to have it so that I can click one of many buttons and
> load a specific web user control dynamically into a placeholder
> control. In order to maintain that controls viewstate though you have
> to add it back again in the page load event. Right now it appears to
> work but I wonder if I'm missing something or it could be done more
> efficiently? It seems to work anyway..
>
> Currently I have the following code
>
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> //have to reload control at run time...
> if(ViewState["CurrentControl"]!=null)
>
> LoadWebUserControl(ViewState["CurrentControl"].ToString());
> }
>
>
> private void LoadWebUserControl(string controlToLoad)
> {
> phCompanyUserControls.Controls.Clear();
> switch(controlToLoad)
> {
> case "AddCompanyForm":
> {
> uc_AddCompanyForm webUserControl =
> (uc_AddCompanyForm) LoadControl("uc_AddCompanyForm.ascx");
>
> phCompanyUserControls.Controls.Add(webUserControl) ;
>
> break;
> }
> case "EditCompanies":
> {
> uc_EditCompanies webUserControl =
> (uc_EditCompanies) LoadControl("uc_EditCompanies.ascx");
>
>
> phCompanyUserControls.Controls.Add(webUserControl) ;
>
> break;
> }
> }
>
> }
>
> //this is one of the main buttons to switch user controls
> private void btnAddCompany_Click(object sender, System.EventArgs e)
> {
> ViewState["CurrentControl"] = "AddCompanyForm";
> LoadWebUserControl("AddCompanyForm");
> }
>
> //this is one of the main buttons to switch user controls
> private void btnEditCompanies_Click(object sender, System.EventArgs e)
> {
> ViewState["CurrentControl"] = "EditCompanies";
> LoadWebUserControl("EditCompanies");
> }