Dynamically Adding Controls to User Control

 Hi to all,

first, here is what I have on my .ascx code:

<asp:Label ID="lblCategory" runat="server" Text="Category" /> <asp:DropDownList ID="ddlCategory" runat="server"></asp:DropDownList>
            <asp:Label ID="lblItem" runat="server" Text="Item Question"/>
            <asp:Label ID="lblType" runat="server" Text="Item/ Question Type" />
            <asp:Label ID="lblChoices" runat="server" Text="Choices" />
        <asp:PlaceHolder ID="phItemPlaceholder" runat="server"></asp:PlaceHolder>
<asp:LinkButton ID="lbtnAdd" runat="server" Text="[Add More Item/Question]" />


What I would want to happen is that, when the user clicks on the lbtnAdd,
it will automatically add text boxes on the specified place holder (phItemPlaceHolder) with in the .ascx page

I already know how to programmatically add the user control on the, say for example default.aspx page.
My problem is how to add controls dynamically, say textboxes on the user control.


Please aide me with this.

thanks in advance


5/5/2009 3:03:04 AM
asp.net.web-forms 93655 articles. 3 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 41

On the click event of the LinkButton use:

 phItemPlaceholder.Controls.Add(  )


Plz remember to click "Mark as Answer" if this helped you.
5/5/2009 3:47:09 AM


thank you for your reply, i tried that one but, i got an Exception:


I've read some articles but basically it says that i will need to use
LoadControl() method, but I dont fully understand that on and how
can I use to to my User Control to dynamically add controls to it?


Can you help me understand this one?




5/5/2009 4:22:05 AM

Post your code and the exception.  I need a clearer understanding of what you are doing.

Plz remember to click "Mark as Answer" if this helped you.
5/5/2009 4:49:59 AM

TextBox t = new TextBox();
        t.ID = "TextBox5";


Bind GridView to Dynamically created DataTable | Bind DataTable to DropDownList

My Blog
5/5/2009 6:48:09 AM

Write follwing code on click of lbtnAdd button:

TextBox tx = new Textbox(); 

TextBox tx = new TextBox();



Khushboo Nayak

Please Mark as Answered if this helps you.


5/5/2009 6:57:42 AM

Similar Artilces:

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

Dynamically adding listbox controls in a web based user control
Hi, I am building a web control and trying to dynamically add checkboxs. I try to create the controls on the render event and i've tried these 3 methods- none of which work.   This method compiles but the checkbox is not visible on the page.  I am unsure as to where it is adding to because I can't reference the parents web page. /*CheckBox blah = new CheckBox(); blah.Text = CheckText; blah.ID = CheckValue; this.Controls.Add(blah);*/   This one compiles but the controls also do not display on the page (I knew this wouldn't work but it was worth a try) //output.W...

Controlling a variable numbers of controls dynamically added to a user control
 Ok, thinking caps on people, this has been causing me grief for a couple of days now. I have an .aspx page which contains various user controls housed in an Accordion control from the Ajax Toolkit and lots of UpdatePanels, these user controls all interact with each other through the parent page, e.g clicking buttons in one control effects visibility of other controls. Two of these controls contain just a series of ImageButtons. In the first of these controls the ImageButtons are generated in the page_load method and added to a panel using Panel.Controls.Add(ImageButtonCont...

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

Dynamically added user controls.. Restoring user control events ...
Hello, i ve made a simple aspx page to focus on the problem..In the aspx page i have a button that when it is pressed a user control is added dynamically, So if you press the button twice , two user controls will be added..Here is how i implement it :When a user control is added ,i add it to an arraylist (which contains all the  user controls that have been inserted to the page ) and this array list is then added to the session.So when the page is loaded ,i retrieve that arraylist back from the session i iterate through it ,and i enter again to the page all the previous added user contr...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

User Control controls not created when dynamically creating user control
  Have created a user control for displaying and editing a dynamically number of values. The user control contains two labels and an edit box. When dynamically loading the user control I get an error when the control itself tries to change setting on the fields within the user control.   Why doesnt this work? It works okay if I dont load it dynamically but create a user-control-instance in the aspx-page but thats no solution.   Thankyou!     Here is my code:   ucDataValue.ascx   <%@ Control Language="C#" AutoEventWireup="true&...

Dynamically adding User Controls or custom controls
Hi, I would like to know how to add Web User controls dynamically in asp.net 2.0. The user control consists of several labels,image buttons and link buttons.I can't add Web user controls dynamically to an aspx page while it is easy for a builtin control to add dynamically--=A J E E S H=-- Have you try to use Page.LoadControl method ??? you can use that like: PlaceHolder1.Controls.Add(Page.LoadControl("usercontrol.ascx")) hope that helps   As Andoko said, the key is in using PlaceHolder control.  Once you put this control in your desire place, you can add your user contro...

Dynamic Control problem in Web user control
hi friends, i am developing an web user control.   in that i require to add RADEditor control dynamically when the user press ADD button.   But i am unable to retain the control when the page is post back.   Can anybode send me the sample..   Note that it should be in an Web User Control  Just to make sure, Did you place the code addinthe control dynamically inside the "if (!IsPostback)" inside the page_load? santhoshonet: hi friends, i am developing an web user control.   in that i require to add RADEditor control dynamically when the...

web controls and web user control
i have a problem in a web form , I have a webform that contains a web user control which in turn contains a html input form ,the web control events don't fire up ,When I click button ,no effect ,I don't know what is the effect of the html form which is in the web user control on the web controls in the web form ?? can any one help me the web control event should be handle in the code behind page, is the event handler for the button you are clicking registered? You can try double clicking the button, should give you the default btn_click event handler in code behind. Also, make ...

Dynamically adding "Child" User Controls to "Parent" User Control
Hello group..Need some advice/help with some issue...Am having trouble dealing with nested user controls that i will be having. My business need is as follows...Need to display a Page Header , data Grid(for some details) and Page footer...And the grid will display only a set of data. If i have more data, the parent page should have another set of Page Header, data Grid and Page Footer. So, this is how it looks like***************************Page HeaderGridPage Footer--------Page Break---------Page HeaderGridPage Footer--------Page Break---------Page HeaderGridPage Footer--------Page Break---...

Adding multiple user controls to a web page -- Tutorial example involving copyRughtNotice and pageHeader user controls.
Hello: Please help me... I am trying to work through the tutorial example for adding the pageHeader.ascx usr control to a web form that already has a user conrol embedded in it, I followed the instructions verbatim, and, NO PAGE HEADER AT ALL!!!! What am I doing wrong? I really need help on this ASAP!!! Thnx, Carle Eric Giannelli Carle Eric Giannelli Can you post the code at the top of the .ASPX page? Specifically the <%@ parts... http://www.heliosfx.com Sure, The code at the top of the copyRightNotice.aspx page is: <%@ Page Language="VB" %>...

Web resources about - Dynamically Adding Controls to User Control - asp.net.web-forms

iPhone 5 A6 chip to dynamically up-clock up to 1.3GHz (and downclock too)?
Interesting video from our friends at TLDToday: If this is not just a Geekbench app glitch, it could indicate why that A6 processor is so 2X ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

iPhone 5 Lightning port dynamically reassigns pins
... of Apple's Lightning USB cable shows that the pins on the plug aren't arranged symmetrically, suggesting that the Lightning port can dynamically ...

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...

A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 12/25/2015 3:23:15 PM