如何使一个孩子的ListView相关的项目使用产品ID从父 [英] How to render related items in a child ListView using the item ID from the parent

查看:172
本文介绍了如何使一个孩子的ListView相关的项目使用产品ID从父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

请让我知道如果您有任何疑问

 < ASP:ListView控件ID =ListView1的
    而itemtype =E_Store_Template.Models.Category
    =服务器
    SelectMethod =GetCategories
    OnItemDataBound =brandList_ItemDataBound>
    <&ItemTemplate中GT;
        < UL>
            <李风格=名单样式类型:无;文本对齐:左;>
                < B风格=FONT-SIZE:大;字体风格:正常>
                    &所述; A HREF =&下;%#:GetRouteUrl(ProductsByCategoryRoute,新的{类别名称= Item.CategoryName})%>中与GT;
                        <%#:Item.CategoryName%GT;
                    &所述; / A>
                    < ASP:ListView控件ID =brandList=服务器的ItemType =E_Store_Template.Models.Brand>
                        <&ItemTemplate中GT;
                            < UL风格=名单样式类型:无;文本对齐:左;>
                                <立GT;
                                    &所述; A HREF =&下;%#:GetRouteUrl(ProductsByCatBrandRoute,新的{名优产品= Item.BrandName})%>中与GT;
                                        <%#:Item.BrandName%GT;
                                    &所述; / A>
                                < /李>
                            < / UL>
                        < / ItemTemplate中>
                    < / ASP:的ListView>
                < / B>
            < /李>
        < / UL>
    < / ItemTemplate中>
< / ASP:的ListView>

我觉得事件处理程序需要看起来像这样

 保护名单,LT;品牌> brandList_ItemDataBound(对象发件人,ListViewItemEventArgs E)
{
    ListViewDataItem的DataItem =(ListViewDataItem)e.Item;    如果(e.Item.ItemType == ListViewItemType.DataItem)
    {
        类别MYDATA =(类别)dataItem.DataItem;
        INT CurrentCategoryId = mydata.CategoryID;
        VAR的查询= _db.Products.Where(P => p.Category.CategoryID == CurrentCategoryId)。选择(P => p.Brand).Distinct();
        返回query.ToList<品牌>();
    }
    其他
    {
        返回null;
    }
}

但给我的错误:

 'System.Collections.Generic.List< E_Store_Template.Models.Brand> E_Store_Template.SiteMaster.brandList_ItemDataBound(对象,System.Web.UI.WebControls.ListViewItemEventArgs)'有错误的返回类型


解决方案

的ItemDataBound的目的不是要返回数据,它被设计来处理事件。在这种情况下,该事件被呈现需要发生作为子事件类别与任何东西。动作正确的做法是让从父项的子ListView和数据绑定到它。标记:

 < ASP:ListView控件ID =所属分类=服务器
    OnItemDataBound =brandList_ItemDataBound的ItemType =E_Store_Template.Models.CategorySelectMethod =GetCategories>
        <&LayoutTemplate模板GT;
            < UL风格=名单样式类型:无;>
                < ASP:占位符=服务器ID =itemPlaceholder/>
            < / UL>
        < / LayoutTemplate模板>
        <&ItemTemplate中GT;
            <李风格=文本对齐:左;>
                < B风格=FONT-SIZE:大;字体风格:正常>
                    &所述; A HREF =&下;%#:GetRouteUrl(ProductsByCategoryRoute,新的{类别名称= Item.CategoryName})%>中与GT;
                        <%#:Item.CategoryName%GT;
                    &所述; / A>
                < / B>
                < ASP:ListView控件ID =brandList=服务器>
                    <&LayoutTemplate模板GT;
                        < UL风格=名单样式类型:无;文本对齐:左;>
                            < ASP:占位符=服务器ID =itemPlaceholder/>
                        < / UL>
                    < / LayoutTemplate模板>
                    <&ItemTemplate中GT;
                        <立GT;
                            &所述; A HREF =&下;%#:GetRouteUrl(ProductsByCatBrandRoute,新的{名优产品= Item.BrandName})%>中与GT;
                                <%#:Item.BrandName%GT;
                            &所述; / A>
                        < /李>
                    < / ItemTemplate中>
                < / ASP:的ListView>
            < /李>
        < / ItemTemplate中>
    < / ASP:的ListView>

背后code:

 保护无效brandList_ItemDataBound(对象发件人,ListViewItemEventArgs E)
{
    ListViewDataItem的DataItem =(ListViewDataItem)e.Item;    如果(e.Item.ItemType == ListViewItemType.DataItem)
    {
        类别MYDATA =(类别)dataItem.DataItem;        INT CurrentCategoryId = mydata.CategoryID;
        VAR的查询= _db.Products.Where(P => p.Category.CategoryID == CurrentCategoryId)。选择(P => p.Brand).Distinct();        ListView控件brandList =(ListView控件)e.Item.FindControl(brandList);
        brandList.DataSource = query.ToList<品牌>();
        brandList.DataBind();
    }
}

我添加了占位符LayoutTemplates让你的ListView不重复的标签为每个记录。我也动了你的结束标记,因为你不应该有一个块元素(无序列表)是一个大胆的标记集的子项。

我会考虑通过适当的类别ID获得所有种类和品牌的1(或2)SQL查询然后过滤品牌的数据列表。你会存储品牌的数据列表作为页面级的变量,但不要将其加载到Session或ViewState的,你只在页面渲染时需要它。

I have a nested list view and the goal is to pull the ID from outer list view and pass it into the method below as the CurrentCategoryId This will allow my second list view to populate correctly. From what i read this should be done with a eventhandler OnItemDataBound but i am having trouble understanding how it that works?

Please let me know if you have any questions

<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>

I think the event handler needs to look like this

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;
    }
}

but gives me the error:

'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

解决方案

ItemDataBound isn't designed to return data, it is designed to handle an event. In this case the event is rendering a category and anything that needs to happen as a child event. The proper course of action is to get the child ListView from the parent item and bind your data to it. Markup:

<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>

Code behind:

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();
    }
}

I added LayoutTemplates with PlaceHolders so your ListView doesn't repeat your tags for each record. I also moved your closing tag because you shouldn't have a block element (unordered list) as a child of a bold tag set.

I would consider getting all the Categories and Brands in 1 (or 2) SQL queries then filtering the Brands data list by the appropriate CategoryID. You would store the Brands data list as a page level variable, but don't load it into Session or ViewState, you only need it during the page rendering.

这篇关于如何使一个孩子的ListView相关的项目使用产品ID从父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆