简体   繁体   中英

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`

I thought that first print the groupName ie (metal,oil) then inside the repeater use another repeater to show the details about the material. But I am stuck at the first point. Please suggest !! Here is the code for the class 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM