2013-02-25 84 views
1

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

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

<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 

回答

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

我加LayoutTemplates占位符让你的ListView不重复的标签为每个记录。我还移动了结束标记,因为您不应将块元素(无序列表)作为粗体标记集的子元素。

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

相关问题