Loading user controls dynamically - c#

How to load a user control dynamically in a page?
I have a page that contains radioButtons. Each click on a radio button loads a user control (.ascx) in the page.
What I am doing is loading all controls at the same time, but set their visibility to false. When a user clicks a radiobutton I set the visibility of the specific user control to true.
As a result I am loading all the user controls on each postback.
Is there any other possible way of doing this?

Add a div with runat server on the page with an id "divControls" for example.
Asp allow you to load a user control ".ascx" dynamically.
The below code should solve your problem.
Control ctrl = Page.LoadControl("UserControlPath");
divControls.Controls.Clear();
divControls.Controls.Add(ctrl);

If you don't keep them in a List, and that list in session, you'll have lots of trouble.
Ghyath's way is the right way but you should also add them to a List.
List<Object> Usercontrols = new List<Objects>{};
Control ctrl = Page.LoadControl("UserControlPath");
Usercontrols.Add(ctrl);
Session["Usercontrols"] = Usercontrols;
On each postback, you need to reload your div with the controls in your List.
Edit: I've corrected the last line.

Is there any specific reasons to hold the usercontrols in a single page?
Think about the page view state as you are loading all the controls and setting it's visibility.
I think there are two possible solutions:
Either create seprate page hosting different user control and when user click on the certain radio button, redirect to the respective page.
Load on demand i.e. when user request a user control, only then load it but removing all other loaded user controls and hence page will have only one user control at any time.

Related

Load contents to an ajax tab when the user clicks on it

I have a page(C#-asp.net application) with more than 5 ajax tabs that gets data on page load. Since it takes a long time, i want to populate the tab contents only when the user clicks on the tab.not able to find any event in tab panel. BTW i'm using an AJAX Tab container Please Advice me ASAP.
Thanks in Advance. :)
You need to set AutoPostBack="true" add ActiveTabChanged to tab control. For details go here
Consider to extract tabs content to user controls and load them into active tab in Page_Load method depending on ActiveTabIndex property

Load User Controls dynamically based on other control events

I have a "cross section" of variables that I need to use to generate a set of user controls dynamically. In the center of the page I have a Telerik multitab/page. I have a custom tree menu and a custom menubar, based on the combination of menu input each of the tabs should load a user control relevant to that cross section of data.
For clarity, almost each tab, treeview, menubar combination needs a unique control.
My problem is that all the postback/loading happens well before the "OnMenuChanged" event triggers, so I'm one "set" of user controls behind. Even if I were to use session/viewstate they wouldn't get assigned until after I needed the value stored in them.
Currently what is happening is the default user controls are loaded in the "pageviewcreated" event, then in the onMenuItemChanged I go back and reload the user controls. It seems very inefficient and is complicating up the approach for selecting the right .ascx.
How do I manage this?
If I understand what you're trying to do could you not use a placeholder control and inject your needed usercontrol into that inside of the events you're managing? Try doing something like this inside of the "OnMenuSelected" event.
WebUserControl1 uc = (WebUserControl1) Page.LoadControl("WebUserControl1.ascx");
PlaceHolder1.Controls.Add(uc);

Controls inside the dynamically added web user control does not retain there value on post back caused by the dropdown list

i am dynamically adding and removing a different user control from a to a place holders.
The user controls have lot of asp.net controls like cascading dropdowns with autopostback true.
The whole page is in a update panel.
After every postback i again add the control added previously and the which control is added previously is maintained in a view state.
The problem: Only first time the content in the textboxes get cleared after the first postback(caused by changing the dropdown)
Any help is appreciated
You need to make sure that your dynamic controls are being added in the Page_Init event in order for them to retain their data.

Dynamics controls lost on postback

This old chestnut again.
My page is constructed as follows; I have a dropdownlist which is databound on first load. When the user selects a value from this, a postback is performed which then databinds a repeater control.
The ItemTemplate of this repeater control contains a placeholder control. In code behind in the ItemDataBound event of the repeater, I am adding two controls dynamically to this placeholder, a hiddenfield and a checkbox.
When the user clicks the save button, I then want to iterate over all those dynamically created hiddenfields and checkboxes and determine their values. However when the user clicks the save button, those controls no longer exist as shown in the page trace.
I know this is a lifecycle issue and the articles I've seen on this suggest using Init methods to dynamically create your controls but I can't because of the way my page works, e.g. the repeater control only appears and binds after a value is chosen from the dropdownlist.
What do I need to do to maintain the dynamic controls through the postback caused by clicking on the save button?
The problem is when you hit the save button probabily you dont re-bind the repeater and the controls you have added at run time withint the ItemDataBound event are not longer available(because they don't exist anymore)
Why don't you add those control at design time using the Eval function the set up the value of the hidden field?
You just don't create them dynamically just on the on selection change of the drop-down set visibility true or false for the repeater that will solve your problem.on post back you have to again create those control as they are Dynamically created.

Loading & Retrieving data from multiple user controls

I have multiple user controls on the page that are used primarily for data entry purposes. When a product is loaded, I need to load the product data into all those user controls and retrieve data when I need to save the product. The user controls are not visible to the user directly, instead user would click on a link and the user control will open up in a modal popup.
Currently the way I'm doing this is, I've loaded all the user controls on the page in separate div controls, and showing the modal popup when the link is clicked. I'm sure loading all the user controls on the page is not a good idea. Is there a better way to handle this? I think we can show the markup using JSON with jQuery - but how can I load and retrieve the data using that? Can someone help please?
Thanks.
Sounds like a case for AJAX. You could use an UpdatePanel within the div that defines the modal dialog. From the click handler for whatever control brings up a particular modal, you can dynamically replace one usercontrol with another in that UpdatePanel, pre-populate the usercontrol's data, then show a single modal that could be anything. Doesn't have to be a modal either; you could set up the UI with tab-like controls that switch between these UserControls in an always-visible div.
For a simpler approach, you could keep your Usercontrols in a div that is initially hidden by JQuery.
On postback, ie. when the user clicks the button/link for a particular product you can populate all your controls in a standard ASP.Net way so they're ready to display and then call a bit of JS/JQuery to show the div as a modal dialog.

Categories