【问题标题】:How do I display a collection of UserControls in html.ascx?如何在 html.ascx 中显示用户控件的集合?
【发布时间】:2015-06-18 12:38:15
【问题描述】:

我创建了一个用户控件列表,这些控件使用相同的界面来填充每个卡片中的数据字段,具体取决于数据对象的标签是什么。我遇到的问题是我不知道如何在 html 页面中显示这个对象集合。

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;
    }
}

如何在我的 HTML 页面的格式化部分显示已编译列表?

【问题讨论】:

  • 所以compiledList 是用户控件的集合,您想在一个 .aspx 页面上显示所有这些控件吗?
  • 是的,完全正确。把它想象成一堆需要在页面上一起显示的小框搜索存根。例如:您正在搜索红色的车辆,然后返回一堆搜索结果,包括汽车(唯一用户控制)、卡车(唯一用户控制)和轻便摩托车(唯一用户控制)的存根。它们将显示在网页上的某种容器中。
  • 你试过asp:Repeater控制吗?
  • 如何将Repeater控件附加到compiledList?
  • 你想错了。 Repeater 是 aspx 页面上的一个控件,您需要为其定义一个 ItemTemplate。这可以是您的 CustomControl。然后你只需要用数据填充它。

标签: c# html asp.net .net ascx


【解决方案1】:

解决方案是在 HTML 中使用占位符并创建用户控件的实例以将其加载到后面的代码中,然后将其添加到占位符中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多