asp.net - ListView ItemDataBound 问题
问题描述
我有一个显示产品的 ListView。我正在尝试在每个产品列表之前添加一个类别标签,其中每个类别可能有多个产品。我试图在不嵌套 ListViews 的情况下实现这一点。现在的问题是它在每个产品名称上重复类别名称。这不是我想要的。我只希望它在类别名称更改时列出产品的类别名称。Product 和 Category 都从同一个数据库表中返回。
第一类
产品一
产品 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 Load 方法移到了一个名为 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++;
}
}
解决方案
我在问题中更新的解决方案是正确的方法——唯一的错误是 continue 语句——它绕过了计数器。这是任何处于类似情况的人的工作版本:
<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++;
}
}
推荐阅读
- javascript - 如何根据该字符串中的单词数组获取字符串中单词字符的位置
- javascript - 将多个文件上传到 Firebase 存储
- r - 如何获得具有多个单位的输出的时差
- arrays - 跨数组元素求和
- java - TableColumn 的默认 CellValueFactory 是什么?
- mongodb - 如何使用 Robo 3T 连接到 MongoDB Atlas?
- r - 了解有关逆变换采样 R 的代码
- android - 带有firebase实时数据库的android
- php - 在PHP中的另一个数组中保存两个数组的差异
- react-native - 如何在另一个 React 组件中使用 createBottomTabNavigator