• 0

[ASP.NET] User controls ....


Question

Hello,

So I created my first user control today. Everything is working fine, however, I can't understand how to add 10 user controls using a forloop.

In the holder page (the .aspx that hosts my control) I created a panel control, in the code behind I simply write this:

for (int i = 0; i < 10; i++)
{
   panel1.Controls.Add( .... so how do I add my control here ....);
}

at the front end, the register directive looks like this:

<%@ Register TagPrefix="test" TagName="mine" Src="~/MyContentWrapper.ascx" %>

Thanks.

Link to comment
https://www.neowin.net/forum/topic/638435-aspnet-user-controls/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

The LoadControl function is used to load and instantiate and instance of your control:

for (int i = 0; i &lt;10; i++)
{
   UserControl control = LoadControl("~/MyContentWrapper.ascx");
   panel1.Controls.Add(control);
}

I don't personally like this method, you've got to think about your viewstate etc.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.