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>
<table>
    <tr>
        <td>
            <asp:Label ID="lblItem" runat="server" Text="Item Question"/>
        </td>
        <td>
            <asp:Label ID="lblType" runat="server" Text="Item/ Question Type" />
        </td>
        <td>
            <asp:Label ID="lblChoices" runat="server" Text="Choices" />
        </td>
    </tr>
    <tr>
        <asp:PlaceHolder ID="phItemPlaceholder" runat="server"></asp:PlaceHolder>
    </tr>
</table>
<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
Moist
0
moisescujardo
5/5/2009 3:03:04 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 5 Replies
👁️‍🗨️ 3005 Views



On the click event of the LinkButton use:
 phItemPlaceholder.Controls.Add(  )
 

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

hello,
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?
Thanks..
-1
moisescujardo
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.
-1
Ugly
5/5/2009 4:49:59 AM

TextBox t = new TextBox();
        t.ID = "TextBox5";
 phItemPlaceHolder.Controls.Add(t);

Bind GridView to Dynamically created DataTable | Bind DataTable to DropDownList

My Blog
-1
shahed
5/5/2009 6:48:09 AM

Write follwing code on click of lbtnAdd button:
TextBox tx = new Textbox(); TextBox tx = new TextBox();
phItemPlaceholder.Controls.Add(tx);
Thanks,
Khushboo Nayak
Please Mark as Answered if this helps you.
1
khushi_nayak
5/5/2009 6:57:42 AM
Reply: