I have to dynamically add user controls to a asp.net web page. I know that can be done as follows:

WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
            cntrlHolder.Controls.Add(webug);

In the above code cntrlHolder is a PlaceHolder/Panel. I need to add more controls of type WebUserControl into the same page. How can this be done?
The page adds only one instance of the control. Any suggestions??

I'd probably give each instance you create a unique id, as the current method looks like its overwriting them. You can do that by setting the UserControl.ID property before adding the control to the container

I'd probably give each instance you create a unique id, as the current method looks like its overwriting them

No ASP.NET will automatically give them unique ID's, you just need a loop

for(int i = 0; i < 2; i++)
{
     WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
     cntrlHolder.Controls.Add(webug);
}

What's important here is we are instantiating a NEW instance of the control each time, not appending the same one.

Comments
spot on

No ASP.NET will automatically give them unique ID's, you just need a loop

for(int i = 0; i < 2; i++)
{
     WebUserControl webug = (WebUserControl)Page.LoadControl("WebUserControl.ascx");
     cntrlHolder.Controls.Add(webug);
}

What's important here is we are instantiating a NEW instance of the control each time, not appending the same one.

How is this different from giving the command to create the control twice?

How is this different from giving the command to create the control twice?

It's not that's what we are doing.

MyUserControl control = (MyUserControl)Page.LoadControl("MyUserControl.ascx");

            PlaceHolder1.Controls.Add(control);
            PlaceHolder1.Controls.Add(control);

The above creates a reference to a new instance of the user control and then adds the SAME reference twice, the Add method will see that reference already exists in its collection so is discarded. If we do this:

for (int i = 0; i < 2; i++)
            {
                MyUserControl control = (MyUserControl)Page.LoadControl("MyUserControl.ascx");

                PlaceHolder1.Controls.Add(control);
            }

We are creating a NEW reference to a NEW instance in each loop, so you get 2 user controls appearing in the page. Right click in your web browser and choose to view source. See how ASP.NET has created unique names for the controls.

Thanks.. a lot for explaining that. I am new with ASP.NET. That's why i didn't understand why the second control won't show up when i press the add control button. But now i understand a bit.

Thanks a lot once again for this. But since i'm new with ASP.NET, a lot of questions are bound to come your way. I hope you'll answer them without hesitation like you did this one.

Hi all,

I read carefully your post but there is still an issue for me if you want to loop. I think I'm doing something wrong but I really can not find where :

For Each ...
SqlDataSource1.SelectCommand = "select [note] from [NOTES] where [id_note]='" & MyVar & "'"
Dim mycontrol As Control = LoadControl("tata.ascx")
panel.ContentContainer.Controls.Add(mycontrol)
Next

Right, for each loop a control (from tata.ascx) should load into my panel ... and its the case.
However my control in tata.ascx is a detailsview bound to SqlDataSource1, so I presume that all controls should have different values according to 'MyVar' which is changing at every loop.

Here is what I have in my tata.ascx page:

<asp:detailsview id="DetailsView1" runat="server" datasourceid="SqlDataSource3" autogeneraterows="False">
<Fields>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("note") %>'></asp:TextBox>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("note") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:detailsview>

My problem is that all the detailsview (in readonly or edit mode) have exactly the same value. (Label1 shows always the same value for example).

This value is the last one according to the loop.

I've spend a full day on this without success and I don't have any more ideas. My understanding of this must be poor and it would be very nice to have your comments.

Thank you and I hope I have been clear enough as English is not my mother tongue.

This question has already been answered. Start a new discussion instead.