Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9278725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:26:54+00:00 2026-06-18T17:26:54+00:00

How do I programmatically create an array of web user controls? I created a

  • 0

How do I programmatically create an array of web user controls?

I created a web user control. I already know how to implement one of them coding in the aspx file, however I would like to know if it is possible to do that from the code behind in either the Page_Init() or the Page_Load() event.

I already know the initialization on the aspx page.

<%@ Register TagPrefix="uc" TagName="myUsercontrol1" Src="/Controls/myUsercontrol1.ascx" %>

In the traditional way, I would do something like:

<div id="divMyusercontrols" runat="server">
    <uc:myUsercontrol1 id="ctlTheControl" runat="server" />
</div>

What I was wanting to do, which does not work, as I tried, is:

In the aspx file:

<div id="divMyusercontrols" runat="server">
</div>

In the code behind in let us say the Page_Init() event the following:

String strControl = "<uc:myUsercontrol1 id="ctlTheControl{0}" runat="server" />";
String strHtml = null;
for (int i = 0; i < 5; i++)
     strHtml += String.Format(strControl, i.ToString());
this.divMyusercontrols.InnerHTML = strHtml;

This code sadly does not work. I realize that I can simply do that manually, but I do not know the actual count. I will know that ahead of time.

UPDATE (to show answer #3 proper code):

The C# for the aspx file is the following. I have a call to a static C# code behind, which returns the count. I then set a property to indicate which item to show and then the for-loop. Cool!

<%int iCount = MyProject.Items;%>
<%for (int iIndex = 0; iIndex < iCount; iIndex++)%>
<%{ %>
    <div runat="server">
    <uc:myUsercontrol1 id="ctlItem<%=iIndex %>" runat="server" />
    </div>
<%}%>

SOLUTION (2013-02-12):
I tried out all three answers below, sadly after I marked one as a solution. The winning one is a modified version of the Page_LoadControl(). Here is the code that works, and yes, I ran the code. Everything works.

// Load an array of controls onto a predefined panel.
for (int iIndex = 0; iIndex < 10; iIndex++)
{
    // Load the control.
    MyProject.Controls.MyControl ctlItem = (MyProject.Controls.MyControl)Page.LoadControl(@"/Controls/MyControl.ascx");

    // Initialize the control.
    ctlItem.MyIndex = iIndex;

    // Add the control to the panel.
    this.pnlItems.Controls.Add(ctlItem);
}

Here is the fixed aspx code.

<div id="divItems" runat="server" class="divItems">
    <dx:ASPxPanel ID="pnlItems" runat="server" Width="200px">
    </dx:ASPxPanel>
</div>

I tried doing, MyProject.Controls.MyControl ctlItem = new MyProject.Controls.MyControl(), however that does not work. I got a null excemption. Loading the control worked.

The answer, which I too hastilly marked as a solution, does not work. When ran the designer complained. Here is the code from the designer.

    /// <summary>
    /// ctlPurchases<%=iIndex %> control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::MyProject.Controls.MyControl ctlItem<%=iIndex %>;

The designer was unhappy about the <%…%> part. This solution has other problems. The cleanest one is using the Page_LoadControl.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:26:56+00:00Added an answer on June 18, 2026 at 5:26 pm

    Can you use an ASP.NET Panel and use Page.LoadControl()?

    for (int i = 0; i < 10; i++)
    {
        pnlContent.Controls.Add(Page.LoadControl("ucDemo.ascx"));
    }
    

    This is in C# BTW.

    Or if these UserControls will be data bound, you could try using a Repeater control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created the following array manually (bottom), I wish to create it programmatically using
So I create an UIImageView programmatically and I place it on an array. In
I'm trying to create and draw a UITableView populated by an array. I've created
I'm using these two methods to create orders programmatically in Magento. The first one
Is there an easy way to programmatically create a 2d array in javascript? What
I have created an NSPopUpButton programmatically and I made an array for my choices,
How do I programmatically create buttons with images on them. I want to store
I programmatically create a Picture Box in c# windows program. I assign it with
How programmatically create an element based on UserControl and dock it to the DockPanel?
I'm trying to programmatically create a window with my own custom OpenGL NSView as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.