C# Asp.NET ListView 数据控件动态数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/388624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:33:06  来源:igfitidea点击:

Asp.NET ListView Data Control Dynamically Databind

c#asp.netdata-bindinglistview

提问by mirezus

I am trying to implement a ListView data control for displaying and editing lookup table/ application level variables. There are multiple entity classes which can be bound to the ListView, so the ItemTemplate needs to be dynamically bound to the selected entity object.

我正在尝试实现一个 ListView 数据控件,用于显示和编辑查找表/应用程序级变量。ListView可以绑定多个实体类,因此ItemTemplate需要动态绑定到选中的实体对象上。

For example i have:

例如我有:

AddressType { AddressTypeId, AddressTypeDescription}, 
PhoneType { PhoneTypeId, PhoneType}, 
MarriageStatusType { MarriageStatusId, marriageStatusType}

Those generated entity objects prevent me from simply doing something like the following snippet, because the ID and Type properties are different on each business object.

这些生成的实体对象阻止我简单地执行类似以下代码段的操作,因为每个业务对象的 ID 和类型属性都不同。

<ListView>
...

<itemTemplate>
    <tr> 
       <td runat="server" id="tdId"> <%# Eval("ID") %> </td> 
       <td runat="server" id="tdType"> <%# Eval("TypeNameDescription") %> </td> 
    </tr>
</itemTemplate>

...
</ListView>

I am trying to discover : 1. How to iterate over the ListView Items to insert the appropriate property value into the server side html td tags. 2. How to use Databinder.Eval on the ListView items to insert that property value.

我试图发现: 1. 如何遍历 ListView 项目以将适当的属性值插入到服务器端 html td 标签中。2. 如何在 ListView 项上使用 Databinder.Eval 来插入该属性值。

Thanks in advance!

提前致谢!

采纳答案by flesh

Ok in answer to your questions:

好的,回答你的问题:

  • You can iterate over the ListViewItems by using the OnItemDataBound event in the parent ListView. You can then use this to databind nested child ListViews (or insert Html or manipulate the contents of the list in any way you need). Make sure you use ListViewItemEventArgs in your code behind handler so you can access the databound item easily...
  • You can use Databinder.Eval to dynamically populate a Repeater in your ListView using something like this (note 'GetChildCategoryData' is a code behind method):
  • 您可以使用父 ListView 中的 OnItemDataBound 事件迭代 ListViewItems。然后,您可以使用它对嵌套的子 ListViews 进行数据绑定(或插入 Html 或以您需要的任何方式操作列表的内容)。确保在处理程序背后的代码中使用 ListViewItemEventArgs,以便您可以轻松访问数据绑定项...
  • 您可以使用 Databinder.Eval 使用类似这样的东西在 ListView 中动态填充中继器(注意“GetChildCategoryData”是一个代码隐藏方法):

Hope it helps..

希望能帮助到你..

<asp:ListView ID="parentList" runat="server">
   <ItemTemplate>
      <asp:Repeater ID="childData" runat="server" DataSource='<%# GetChildCategoryData(DataBinder.Eval(Container.DataItem, "parentcategoryID")) %>'>.. </asp:Repeater>
   </ItemTemplate>
</asp:ListView>

回答by Moran Helman

maybe your answer is to bind a repeater inside the itemTemplate

也许你的答案是在 itemTemplate 内绑定一个中继器

and the repeater will get the datasource of the <%# Eval("DataDictionary") %>.

并且转发器将获取<%# Eval("DataDictionary") %> 的数据源。

回答by likeuclinux

Ok, I find way to render repeater inside the listview, I don't know if I can paste the whole code because it is pretty long. the result html like following:

好的,我找到了在列表视图中渲染转发器的方法,我不知道是否可以粘贴整个代码,因为它很长。结果 html 如下所示:

 
  • time1 [comment: following is repeater inside listview] scenarioNamegroupName1groupName2 S1g1Conc1g2Conc1 S2g1Conc2g2Conc2 S3g1Conc1g2Conc3
  • time2 [comment: following is repeater inside listview] scenarioNamegroupName1groupName2 S1g1Conc1g2Conc1 S2g1Conc2g2Conc2 S3g1Conc1g2Conc3
  • time3 [comment: following is repeater inside listview] scenarioNamegroupName1groupName2 S1g1Conc1g2Conc1 S2g1Conc2g2Conc2 S3g1Conc1g2Conc3

The hard part is that the number of group can be different

困难的部分是组的数量可以不同

my aspx page like following:

我的 aspx 页面如下所示:

...hm...the forum I cannot use verbatim for code, the pre block won't work for some character

...hm...论坛我不能逐字使用代码,预块对某些字符不起作用

 KeyValuePair<string, List<scenarioItem>> myData = (KeyValuePair<string, List<scenarioItem>>)(((System.Web.UI.WebControls.ListViewDataItem)(e.Item)).DataItem);
    Repeater repeater = (Repeater)e.Item.FindControl("childData");
    repeater.HeaderTemplate = new MyTemplate(ListItemType.Header);
    repeater.ItemTemplate = new MyTemplate(ListItemType.Item);
    repeater.AlternatingItemTemplate =
       new MyTemplate(ListItemType.AlternatingItem);
    repeater.FooterTemplate = new MyTemplate(ListItemType.Footer);
    repeater.DataSource = myData.Value;
    repeater.DataBind();



public class MyTemplate : System.Web.UI.ITemplate

{ System.Web.UI.WebControls.ListItemType templateType; public MyTemplate(System.Web.UI.WebControls.ListItemType type) { templateType = type; }

{ System.Web.UI.WebControls.ListItemType templateType; public MyTemplate(System.Web.UI.WebControls.ListItemType type) { templateType = type; }

static void Item_DataBinding(object sender, System.EventArgs e)
{
    PlaceHolder ph = (PlaceHolder)sender;
    RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
    scenarioItem myDataItem = (scenarioItem)ri.DataItem;

    if (ri.ItemIndex == 0) { 
        //create the header column part once
        //Add ScenarioName
        ph.Controls.Add(new LiteralControl("<tr><td>ScenarioName</td>"));

        //Add group concentration part
        foreach (recGroupConcItem concItem in myDataItem.mGroupConcList)
        {
            ph.Controls.Add(new LiteralControl("<td>" + concItem.groupName + @"</td>"));
        }
        ph.Controls.Add(new LiteralControl("</tr>"));

    }

    //Add ScenarioName
    ph.Controls.Add(new LiteralControl("<tr><td>"+myDataItem.scenarioName+@"</td>"));

    //Add group concentration part
    foreach (recGroupConcItem concItem in myDataItem.mGroupConcList) {
        ph.Controls.Add(new LiteralControl("<td>" + concItem.groupConc.ToString() + @"</td>"));
    }
    ph.Controls.Add(new LiteralControl("</tr>"));
}

static void ItemAlt_DataBinding(object sender, System.EventArgs e)
{
    PlaceHolder ph = (PlaceHolder)sender;
    RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
    scenarioItem myDataItem = (scenarioItem)ri.DataItem;

    //Add ScenarioName
    ph.Controls.Add(new LiteralControl("<tr bgcolor=\"lightblue\"><td>" + myDataItem.scenarioName + @"</td>"));

    //Add group concentration part
    foreach (recGroupConcItem concItem in myDataItem.mGroupConcList)
    {
        ph.Controls.Add(new LiteralControl("<td>" + concItem.groupConc.ToString() + @"</td>"));
    }
    ph.Controls.Add(new LiteralControl("</tr>"));
}

static void ItemHeader_DataBinding(object sender, System.EventArgs e)
{
    PlaceHolder ph = (PlaceHolder)sender;
    RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
    scenarioItem myDataItem = (scenarioItem)ri.DataItem;

    //Add ScenarioName
    ph.Controls.Add(new LiteralControl("<tr><td>ScenarioName</td>"));

    //Add group concentration part
    foreach (recGroupConcItem concItem in myDataItem.mGroupConcList)
    {
        ph.Controls.Add(new LiteralControl("<td>" + concItem.groupName + @"</td>"));
    }
    ph.Controls.Add(new LiteralControl("</tr>"));
}


public void InstantiateIn(System.Web.UI.Control container)
{
    PlaceHolder ph = new PlaceHolder();
    Label item1 = new Label();
    Label item2 = new Label();
    item1.ID = "item1";
    item2.ID = "item2";

    switch (templateType)
    {
        case ListItemType.Header:
            ph.Controls.Add(new LiteralControl("<table border=\"1\">"));
            //    "<tr><td><b>ScenarioName</b></td>" +
            //    "<td><b>Group1</b></td> <td><b>Group2</b></td> <td><b>Group3</b></td> <td><b>Group4</b></td> </tr>"));
            //ph.DataBinding += new EventHandler(ItemHeader_DataBinding);
            break;
        case ListItemType.Item:
            ph.DataBinding += new EventHandler(Item_DataBinding);
            break;
        case ListItemType.AlternatingItem:
            ph.DataBinding += new EventHandler(ItemAlt_DataBinding);
            break;
        case ListItemType.Footer:
            ph.Controls.Add(new LiteralControl("</table>"));
            break;
    }
    container.Controls.Add(ph);
}

}

}