我有一个ListView显示产品。我试图在每个产品列表之前添加一个类别标签,其中每个类别可能有几个产品。我试图在不嵌套ListViews的情况下实现这一点。现在的问题是,它在每个产品名称上重复类别名称。这不是我想要的。我只希望当类别名称更改时,它会列出产品的类别名称。产品和类别都是从同一个DB表返回的。
类别1
产品1
产品2
类别2
产品3
产品4
<asp:ListView ID="classesList" ItemPlaceholderID="classesPlaceHolder"
OnItemDataBound="ClassesList_ItemDataBound" runat="server">
<LayoutTemplate>
<asp:Panel ID="classesPanel" CssClass="classesPanel" runat="server">
<asp:PlaceHolder runat="server" ID="classesPlaceHolder"></asp:PlaceHolder>
</asp:Panel>
</LayoutTemplate>
<ItemTemplate>
<strong><asp:Label ID="categoryNameLabel" runat="server"></asp:Label></strong>
<a href='#'><%# Eval("product_name") %></a><br />
</ItemTemplate>
</asp:ListView>
protected void ClassesList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
Label categoryNameLabel = e.Item.FindControl("categoryNameLabel") as Label;
if (!categoryNameLabel.Text.Contains(row["cat_name"].ToString()))
{
categoryNameLabel.Text = row["cat_name"].ToString() + "<br />";
}
}
}在循环中这样做是很简单的。但是,由于ItemDataBound事件对返回的每个数据项都会触发,所以我试图找到一种简单的方法来实现这一点。
更新
我将数据绑定从Page方法移到一个名为GetClasses()的方法中。这个新方法是从第一个页面加载调用的。我放弃了ItemDataBound方法,因为我想实现一个循环并切换categoryLabel的可见性。不幸的是,现在没有什么分类出现了,所以也许我遗漏了什么?
<ItemTemplate>
<strong><asp:Label ID="categoryNameLabel" Visible="false" runat="server"></asp:Label></strong>
<i class="fa fa-angle-double-right" aria-hidden="true"></i>
<a href='#'><%# Eval("product_name") %></a><br />
</ItemTemplate>
public void GetClasses()
{
DataSet ds = CatalogDALC.GetClassesByCategory(categoryID, DateTime.Now);
classesList.DataSource = ds;
classesList.DataBind();
// Set main category header for classes - loop here and set category name headers
int count = 0;
foreach (ListViewDataItem item in classesList.Items)
{
Label categoryNameLabel = item.FindControl("categoryNameLabel") as Label;
if (count == 0) // Set the first category name header regardless
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
else
{
if (ds.Tables[0].Rows[count-1]["cat_name"].ToString() != ds.Tables[0].Rows[count]["cat_name"].ToString())
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
else
{
categoryNameLabel.Visible = false;
continue;
}
}
count++;
}
}发布于 2019-04-19 12:46:19
我在问题中更新的解决方案是正确的方法--唯一的错误是继续语句--它绕过了计数器。这是适用于类似情况的任何人的工作版本:
<ItemTemplate>
<strong><asp:Label ID="categoryNameLabel" Visible="false" runat="server"></asp:Label></strong>
<i class="fa fa-angle-double-right" aria-hidden="true"></i>
<a href='#'><%# Eval("product_name") %></a><br />
</ItemTemplate>
public void GetClasses()
{
DataSet ds = CatalogDALC.GetClassesByCategory(categoryID, DateTime.Now);
classesList.DataSource = ds;
classesList.DataBind();
// Set main category header for classes - loop here and set category name headers
int count = 0;
foreach (ListViewDataItem item in classesList.Items)
{
Label categoryNameLabel = item.FindControl("categoryNameLabel") as Label;
if (count == 0) // Set the first category name header regardless
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
else
{
if (ds.Tables[0].Rows[count-1]["cat_name"].ToString() != ds.Tables[0].Rows[count]["cat_name"].ToString())
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
}
count++;
}
}https://stackoverflow.com/questions/55750432
复制相似问题