【问题标题】:ListView ItemDataBound IssueListView ItemDataBound 问题
【发布时间】:2019-04-19 20:46:19
【问题描述】:

我有一个显示产品的 ListView。我正在尝试在每个产品列表之前添加一个类别标签,其中每个类别可能有多个产品。我试图在不嵌套 ListViews 的情况下实现这一点。现在的问题是它在每个产品名称上重复类别名称。这不是我想要的。我只希望它在类别名称更改时列出产品的类别名称。 Product 和 Category 都从同一个数据库表中返回。

第 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 事件,因此我试图找到一种简单的方法。

更新

我将数据绑定从页面加载方法移到了一个名为 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++;
       }
 }

【问题讨论】:

  • 添加全局currentCategory,相同时隐藏(visible="false")标签。更改时更新。
  • 您查看过 ListView GroupTemplate 吗? reference
  • Alex,我尝试切换可见性,但无法正常工作。如果你能提供一个很酷的代码 sn-p。
  • zgood,如果平铺布局,我没有想到外面的 GroupTemplate,但会看看。
  • Alex,我尝试了您的建议并更新了问题。你能看一下吗?

标签: asp.net listview webforms


【解决方案1】:

我在问题中的更新解决方案是正确的方法 - 唯一的错误是 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++;
       }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多