How to add a usercontrol dynamically without using or ?

I have some usercontrol which gets loaded dynamicaly. I know how to add these usercontrols dynamically. The problem is, which usercontrol are to be added is decided only at runtime. So I cannot use <%@ register %> or <%@ Reference %> on the '.aspx' page.

How to access the usercontrols without using these directives?

-------------Problems Reply------------

You can register all of your user controls within your web.config eliminating the need to register them at page level -- or needing to make a decision on which ones to register (at page level). e.g.

<pages>
<controls>
<add tagPrefix="myControl" src="~/Controls/uc.ascx" tagName="header"/>
</controls>
</pages>

Check out this post by Scott Gu for additional help Tip/Trick Registering User Controls

Maybe this is what you mean, in Page_Load method:

var ctrl = LoadControl("yourcontrol.ascx"); Controls.Add(ctrl);

Can you not simply resister all usercontrols in your page (or even better in your web.config), and then this will not be an issue.

Regardless of where you register the control, it won't be automatically loaded on the page if you don't put a tag for it on the page.

To load the user control dynamically from code, you use the LoadControl method:

void Page_Init(object sender, System.EventArgs e)
{
// Instantiate the UserControl object
MyControl myControl1 =
(MyControl)LoadControl("TempControl_Samples1.ascx.cs");
PlaceHolder1.Controls.Add(myControl1);
}

Knowing that this is the method to use, you will be able to find plenty of examples. Here is one place to start.

Why don't you put them into panel or multiview and display accordingly. But you also need to register it into webconfig.

On the page's code-behind:

var ctrl = LoadControl("user.ascx");
this.Controls.Add(ctrl);

Category:asp.net Views:0 Time:2011-03-15

Related post

  • How do I add a UserControl dynamically when all I have is the name of the control in a string format? Silverlight, C# 2011-02-25

    I'm working on calling different user controls, and the only data that I've been given is a string in which to call the user control that I have created. When I know the name, I can hard code and add that control in the code behind and add it dynamic

  • Dynamically add a usercontrol in VB.net 2008-12-17

    I have made a custom UserControl i Vb.net (windows application). How can I add this dynamically to a form? --------------Solutions------------- A UserControl is essentially just another class. It inherits from Control, so you can do all kinds of thin

  • Create UserControl dynamically (WPF) 2010-07-06

    How can I create an instance of my UserControl dynamically (in code behind) and display it on the Canvas? --------------Solutions------------- YourUserControlClassName myUC = new YourUserControlClassName(); myCanvas.Children.Add(myUC); If myCanvas is

  • loading usercontrols dynamically - postback issue 2011-10-21

    Im trying to load a usercontrol dynamically on a aspx page however it works but i get postback issues?? I have a image button on usercontrol which I want to show a image however when i do click on button the page refreshes and does not show image?. I

  • how to Manage UserControl dynamically on a page-? 2011-12-31

    my scenerio is like this, i have to make a admin page ( header section ) in which i have to select single or multiple User Control from my dropdownlist .... which will be added in the page dynamically .... how should i do it ? currently my idea is li

  • How do I add a textbox dynamically in ASP.NET? 2009-04-10

    I've a following requirement for my asp.net page: User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink He clicks submit button on page A and gets redirected to page B. When he clicks on page A link from th

  • Two Webpart load usercontrol dynamically connect together 2009-07-20

    in Webpart connection. I want to provider webpart load usercontrol dynamic, and then tranfer a parameter to the second webpart, and then second webpart load usercontrol of it with parameter received. How can i do that? thank you in advance. ---------

  • How to add columns & rows dynamically during runtime by code in GridView 2009-08-19

    How to add columns & rows dynamically during runtime by code in GridView? --------------Solutions------------- Columns you add by adding them to your GridView.Columns() collection. Rows you add by putting additional records in the DataSource(). I

  • Add a row dynamically in TableView of iphone 2009-10-26

    I need to add a new row in the tableview when i select a row in the table. Am i suppose to use the following method??? - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation What shoud i write inside

  • Add grid column dynamically in xaml 2009-11-25

    Is there any way I can add grid column dynamically in xaml (e.g. using trigger/datatrigger)? The case is that I want to add/remove grid column depending on the Visibility property that is binded to that grid. --------------Solutions------------- Prob

  • If I cannot use OnPreInit in Controls, how can I add my controls dynamically on the right time? 2009-12-23

    I want to have a user control which will add some checkboxes in a asp.NET panel dynamically. Simply I was believing I can do that easily in control's OnPreInit method. But the thing is I have learn that I cannot use and override OnPreInit method on C

  • Add a usercontrol to a panel 2010-08-12

    I want to add a usercontrol to a panel. My code does not work: Panel1.Controls.Add(uc1) --------------Solutions------------- You need to use an instance of the class: Dim usrCntl as New uc1 Panel1.Controls.Add(userCntl) But you will also have to defi

  • I can't add html elements dynamically in IE 2010-10-15

    I have this javascript code to add html elements dynamically. It works perfectly in chrome but it doesn´t work in IE, nothing happens, just seems to add spaces that are maybe divs. Please Help Me!! <script type="text/javascript"> var numero = 0

  • ASP.NET Wizard control, how to add a step dynamically? 2010-10-21

    Code-behind: public partial class WebForm1 : System.Web.UI.Page { protected override void OnInit(EventArgs e) { var t = new TemplatedWizardStep { Title = "Lalalal" }; t.Controls.Add(new Step1UserControl()); _WizardWebControl.WizardSteps.Add(t); base.

  • Is it possible to add table cells dynamically and give them a different id? 2010-11-03

    In this fiddle you see a table with a select-field where you should select a name. Below there are 5 input-fields where one could type in some text and 3 input-fields which are set to readonly. I wanted to ask whether there is a way to add table cell

  • [jquery]How do I add file uploads dynamically? 2011-01-02

    I want to upload multiple files, so I want to add upload fields dynamically through jquery. Now I can do it if I have a button like "add another field", and append the new upload to the form, but I want to do it a bit differently. Initially the form

  • Add / Remove Elements Dynamically 2011-04-07

    Now i done the ADD /Remove Elements How to add/remove elements dynamically, pease find the image attachment, which shows the better understand, category which populate records from database category table, and when user select the particular category

  • How to add items to dynamically created select (html) Control 2011-04-12

    Hi How to add items to dynamically created select (html) Control.. thanks --------------Solutions------------- For adding items on server side, use HtmlSelect.Items property. For example, var select = control as HtmlSelect; // may not be needed var i

  • Add the text dynamically on Image in flex 2011-04-14

    I want to develop an application in flex 3 in that application I can add the text dynamically on the image. Also I can able to rotate the text, change the size of text container. Can anyone help me for that? --------------Solutions------------- Prett

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.083 (s). 11 q(s)