asp.net如何在listView中显示产品详细信息?

时间:2015-01-17 20:24:45

标签: c# asp.net

我正在尝试动态查看列表视图中的产品,这是我的尝试

    <asp:ListView ID="mylistView" runat="server" GroupItemCount="3" 

            onitemdatabound="mylistView_ItemDataBound" 
            onitemcreated="mylistView_ItemCreated">
            <GroupTemplate>
                <tr id="itemPlaceholderContainer" runat="server">
                    <td id="itemPlaceholder" runat="server">
                    </td>
                </tr>
            </GroupTemplate>
            <LayoutTemplate>
                <asp:Label ID="lab" runat="server" Text='<%#Eval("MenuName") %>'></asp:Label>
                <asp:Image ID="foo" ImageUrl='<%#Eval("MenuName") %>' runat="server" />
                <table id="Table1" runat="server">
                    <tr id="Tr1" runat="server">
                        <td id="Td1" runat="server">
                            <table id="groupPlaceholderContainer" runat="server">
                                <tr id="groupPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr id="Tr2" runat="server">
                        <td id="Td2" runat="server">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <table border="0" width="300" style="display: inline-block; background-color: Lime;">
                    <tr>
                        <td>
                            &nbsp
                        </td>

                        <td>
                            <a><b style="text-decoration: underline;">
                                <%# Eval("mainItem") %></b> </a>
                            <br />

                        <asp:Repeater ID="rep" runat="server">
                            <HeaderTemplate>

                            </HeaderTemplate>
                            <ItemTemplate>
                            <li>
                                  <%#((ProductsItems)Container.DataItem).subItems%>
                            </li>

                            </ItemTemplate>
                            <FooterTemplate>

                            </FooterTemplate>
                      </asp:Repeater> 


                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="dataSource" runat="server"></asp:SqlDataSource>

中的代码

     public void databind()
{
    string prod1 = "P1";
    string prod2 = "P2";
    string prod3 = "P3";
    List<string> list1 = new List<string>();
    list1.Add("P11");
    list1.Add("P12");
    list1.Add("P13");

    List<string> list2 = new List<string>();
    list1.Add("P21");
    list1.Add("P22");
    list1.Add("P23");

    List<string> list3 = new List<string>();
    list1.Add("P31");
    list1.Add("P32");
    list1.Add("P33");

     List<ProductsItems> listprodItems = new List<ProductsItems>
 {
      new ProductsItems(){ mainItem = prod1, subItems=list1 },
      new ProductsItems(){ mainItem = prod2, subItems=list2 },
      new ProductsItems(){ mainItem = prod3, subItems=list3 }
 };
    mylistView.DataSource = listprodItems;
    mylistView.DataBind();

 }

这是我的产品

public class ProductsItems
{
   public string mainItem { get; set; }
   public List<string> subItems { get; set; }
}

如何在每个产品下使用转发器显示每个产品及其子项目的标题? 非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

通过使用这一行<%#((ProductsItems)Container.DataItem).subItems%>,你只是为了渲染一个字符串而堆叠,所以将你的代码移动到一个函数,然后以更大的灵活性做出你想要的东西,这是一个例子。

添加此函数调用(而不是上一行)...

<ItemTemplate>
    <br /><%# GetDetails(Container.DataItem) %>
</ItemTemplate>

在你背后的代码上可以有类似的东西:

public string GetDetails(object oItem)
    {
        // use the oData to get your Data and formats them
        var oData = (ProductsItems)oItem;

        // use a String Builder to render inside of it
        StringBuilder sbRenderOnMe = new StringBuilder();

        // oData.subItems this must be a list of strings base on your code
        foreach (var Title in oData.subItems)
        {
            sbRenderOnMe.AppendFormat("Title:{0} <br>", Title);
        }

        // and here return the results
        return sbRenderOnMe.ToString();     
    }