【问题标题】:How to render related items in a child ListView using the item ID from the parent如何使用来自父项的项目 ID 在子 ListView 中呈现相关项目
【发布时间】:2013-02-25 22:40:03
【问题描述】:

我有一个嵌套列表视图,目标是从外部列表视图中提取 ID 并将其作为 CurrentCategoryId 传递到下面的方法中,这将允许我的第二个列表视图正确填充。根据我的阅读,这应该使用事件处理程序 OnItemDataBound 来完成,但我无法理解它是如何工作的?

如果您有任何问题,请告诉我

<asp:ListView ID="ListView1"
    ItemType="E_Store_Template.Models.Category"
    runat="server"
    SelectMethod="GetCategories"
    OnItemDataBound="brandList_ItemDataBound">
    <ItemTemplate>
        <ul>
            <li style="list-style-type: none; text-align: left;">
                <b style="font-size: large; font-style: normal">
                    <a href="<%#: GetRouteUrl("ProductsByCategoryRoute", new {categoryName = Item.CategoryName}) %>">
                        <%#: Item.CategoryName %>
                    </a>
                    <asp:ListView ID="brandList" runat="server" ItemType="E_Store_Template.Models.Brand">
                        <ItemTemplate>
                            <ul style="list-style-type: none; text-align: left;">
                                <li>
                                    <a href="<%#: GetRouteUrl("ProductsByCatBrandRoute", new {brandName = Item.BrandName}) %>">
                                        <%#: Item.BrandName %>
                                    </a>
                                </li>
                            </ul>
                        </ItemTemplate>
                    </asp:ListView>
                </b>
            </li>
        </ul>
    </ItemTemplate>
</asp:ListView>

我认为事件处理程序需要看起来像这样

protected List<Brand> brandList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem dataItem = (ListViewDataItem)e.Item;

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Category mydata = (Category)dataItem.DataItem;
        int CurrentCategoryId = mydata.CategoryID;
        var query = _db.Products.Where(p => p.Category.CategoryID == CurrentCategoryId).Select(p => p.Brand).Distinct();
        return query.ToList<Brand>();
    }
    else
    {
        return null;
    }
}

但给了我错误:

'System.Collections.Generic.List<E_Store_Template.Models.Brand> E_Store_Template.SiteMaster.brandList_ItemDataBound(object, System.Web.UI.WebControls.ListViewItemEventArgs)' has the wrong return type

【问题讨论】:

    标签: c# asp.net linq entity-framework


    【解决方案1】:

    ItemDataBound 不是为返回数据而设计的,它是为处理事件而设计的。在这种情况下,事件正在呈现一个类别以及需要作为子事件发生的任何事情。正确的做法是从父项获取子 ListView 并将您的数据绑定到它。标记:

    <asp:ListView ID="categoryList" runat="server"
        OnItemDataBound="brandList_ItemDataBound" ItemType="E_Store_Template.Models.Category" SelectMethod="GetCategories">
            <LayoutTemplate>
                <ul style="list-style-type: none;">
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                </ul>
            </LayoutTemplate>
            <ItemTemplate>
                <li style="text-align: left;">
                    <b style="font-size: large; font-style: normal">
                        <a href="<%#: GetRouteUrl("ProductsByCategoryRoute", new {categoryName = Item.CategoryName}) %>">
                            <%#: Item.CategoryName %>
                        </a>
                    </b>
                    <asp:ListView ID="brandList" runat="server">
                        <LayoutTemplate>
                            <ul style="list-style-type: none; text-align: left;">
                                <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                            </ul>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <li>
                                <a href="<%#: GetRouteUrl("ProductsByCatBrandRoute", new {brandName = Item.BrandName}) %>">
                                    <%#: Item.BrandName %>
                                </a>
                            </li>
                        </ItemTemplate>
                    </asp:ListView>
                </li>
            </ItemTemplate>
        </asp:ListView>
    

    后面的代码:

    protected void brandList_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
    
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Category mydata = (Category)dataItem.DataItem;
    
            int CurrentCategoryId = mydata.CategoryID;
            var query = _db.Products.Where(p => p.Category.CategoryID == CurrentCategoryId).Select(p => p.Brand).Distinct();
    
            ListView brandList = (ListView)e.Item.FindControl("brandList");
            brandList.DataSource = query.ToList<Brand>();
            brandList.DataBind();
        }
    }
    

    我添加了带有 PlaceHolders 的 LayoutTemplates,这样您的 ListView 就不会为每条记录重复您的标签。我还移动了您的结束标签,因为您不应该将块元素(无序列表)作为粗体标签集的子元素。

    我会考虑在 1 个(或 2 个)SQL 查询中获取所有类别和品牌,然后通过适当的 CategoryID 过滤品牌数据列表。您可以将 Brands 数据列表存储为页面级变量,但不要将其加载到 Session 或 ViewState 中,您只需要在页面呈现期间使用它。

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多