我有一个嵌套列表视图,目标是从外部列表视图中提取 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