How do I display a collection of UserControls in html.ascx?

263 views Asked by At

I have created a list of user controls that use the same interface to populate the data fields in each card depending on what the data object's label is. The problem I am running into is I don't know how to display this collection of objects in the html page.

var compiledList = new List<ICard>();
foreach (DataResult entityObject in entityList)
{
    switch (entityObject.Label)
    {
        case "Employee":
            //create control as ICard
            ICard usrControl = new empsumCardUserControl(entityObject) as ICard;
            ((ICard)usrControl).Populate();
            compiledList.Add(usrControl);
            break;
    }
}

How do I display the compiledList in a formatted section of my HTML page?

1

There are 1 answers

0
blaqksilhouette On

The solution was to use a place holder in the HTML and create an instance of the user control to load it in the code behind, and then add it to the placeholder.

case "Employee":    
     empsumCardUserControl ec = (empsumCardUserControl)LoadControl("~/View/empsumCardUserControl.ascx");
     ((ICard)ec).Populate(entityObject);
     PlaceHolder1.Controls.Add(ec);
     break;