Ask a Question related to ASP.NET General, Design and Development.
-
Ben Miller [msft] #1
Re: Loading/Unloading web user controls dynamically in code - Does this look right?
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");
> }
Ben Miller [msft] Guest
-
Create User Controls dynamically
Howdy, I have a user control that needs to be displayed X amount of times on one page. How is this done? I was thinking of putting it into a... -
Dynamically loading Tree View controls
(Type your message here) Hi I have two tree view controls on my aspx page and the user canopen multiple tree view controls if he so desires. I... -
Dynamically created user controls
In ASP.Net, I am working with some in-house software that dynamically creates a form based on rows in a database table. For example, most pages... -
Loading controls dynamically + passing parameters
Hi, I can use following syntax to add user controls directly to my pages : <XYZ:myControl parameter1="value1" parameter2="value2"... -
How do I dynamically create user controls?
Thanks for any help...! My error is: Object reference not set to an instance of an object. Here's the setup: I have made a user control...



Reply With Quote

