简体   繁体   English

使用列表时打印属性名称 <List<> &gt;使用C#在ASP.NET中作为中继器数据源进行收集

[英]Print property name when using a List<List<>> Collection as a Repeater Datasource in ASP.NET with C#

For some raw materials(eg coper, aluminium, oil, carbon etc.) I have grouped them into types(Metal , Oil). 对于某些原材料(例如铜,铝,油,碳等),我将它们分为类型(金属,油)。

    var metals = new List<RawMaterial> { rMat, rMat2 };
    var oil = new List<RawMaterial> {rMat3, rMat4};

    var allMetals = new List<List<RawMaterial>> {metals, oil};

    repOuterMetalGroup.DataSource = allMetals;
    repOuterMetalGroup.DataBind();

<asp:Repeater runat="server" ID="repOuterMetalGroup" OnItemDataBound="repOuterMetalGroup_OnItemDataBound">
                    <ItemTemplate>
                      <asp:Label runat="server" ID="lblMetalName"></asp:Label>
                  </ItemTemplate>
                </asp:Repeater>

protected void repOuterMetalGroup_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var lblMetalName = (Label)e.Item.FindControl("lblMetalName");

    lblMetalName.Text = e.Item.DataItem.ToString();

}

For the label lblMetalName I want to show metails and oil But I am getting following line two times System.Collections.Generic.List 1` 对于标签lblMetalName我想显示metails oiloil但是我两次跟随行两次System.Collections.Generic.List 1`

I thought that first print the groupName ie (metal,oil) then inside the repeater use another repeater to show the details about the material. 我认为首先打印groupName即(金属,油),然后在中继器内部使用另一个中继器显示有关材料的详细信息。 But I am stuck at the first point. 但是我停留在第一点。 Please suggest !! 请建议! Here is the code for the class RawMaterial : 这是RawMaterial类的代码:

   public class RawMaterial
   {
       public string Name;
       public string Source;
       public string Unit;
       public DateTime Date;
       public decimal Value;

   }

You should probably use a dictionary instead: 您可能应该改用字典:

var allMetals = new Dictionary<<string>, List<RawMaterial>> {
    {"Metals", metals}
    {"Oil", oil}
};

Now you can get the names. 现在您可以获得名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#在ASP.NET中将List &lt;&gt; Collection用作Repeater数据源 - How can I use a List<> Collection as a Repeater Datasource in ASP.NET with C# 将ASP Repeater数据源转换为C#列表 - Cast ASP Repeater DataSource to C# List 在ASP.net中使用中继器的有序列表 - Ordered list using repeater in ASP.net 从mysql检索blob并在asp.net c#和linq数据源中使用转发器显示 - retrieve blob from mysql and display using repeater in asp.net c# and linq datasource 将文本传递到Dropdownlist(使用Enum作为其数据源列表)时,ASP.net C#错误 - ASP.net C# Error when passing text to Dropdownlist, which uses an Enum as its datasource list 从List集合c#/ asp.net填充Dropdownlist - Populating Dropdownlist from List collection c#/asp.net C#/ ASP.NET嵌套中继器的确更改了具有新父级的列表 - C#/ASP.NET Nested Repeater does change list with new parent 在C#中动态创建ASP.NET转发器绑定到对象列表 - Creating an ASP.NET Repeater Dynamically in C# Bound to an Object List 如果DataSource不包含任何项目,如何在ASP.NET C#中隐藏转发器? - How can I hide a repeater in ASP.NET C# if the DataSource contains no items? 如何使用C#将数据源分配给gridview ASP.net中的转发器内的下拉列表 - how to assign a datasource to a dropdownlist inside a repeater in a gridview ASP.net with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM