0

我想知道.net 中两个 4 列 html 链接列表的最佳方法是什么?

列表将基于表格中的数据 - 选择一个类别,然后选择该类别中的每个子类别。已经有相应的 html 标记。

我可以在代码中非常简单地做到这一点,只需构建一个字符串。但我想知道是否有更好的方法来处理它。其中一个数据控件是否适用于此?我更喜欢尽可能使用控件,而不是仅仅构建原始 html 字符串。另外,我只想知道处理此类事情的最佳方法是什么。

如果重要的话,基础数据看起来像这样:类别表和产品表。使用产品表,所以我只选择其中包含项目的类别。还有另一个表 categoryrelationship,它定义了父子关系,即类别和子类别。产品表有一个链接到类别表的类别和子类别列。

html 基本上是这样的:

<div class="out-wrapper">
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
</div>

<div class="out-wrapper">
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<h3> categoryName /<h3>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
<a href=""> subcat name</a>
</div>
4

1 回答 1

1

如果您更喜欢使用控件,可以将 a 嵌套Repeater在 aListview中,例如:

HTML

<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource"
            ondatabound="ListView1_DataBound">
     <ItemTemplate>
         <h3><%# Eval("categoryName") %></h3>
         <asp:HiddenField ID="HdnId" runat="server" Value='<%# Eval("categoryID") %>' />
     </ItemTemplate>
     <asp:Repeater ID="NestedRepeater" runat="server">
        <ItemTemplate>
        <a href=''><%# Eval("subcat") %></a>
        </ItemTemplate>
     </asp:Repeater>
</asp:ListView>

代码隐藏

protected void ListView1_DataBound(object sender, EventArgs e)
{
    GetCategories();
}

private void GetCategories()
{
    DataContext db = new DataContext();

    using (db)
    {

        foreach (ListViewItem item in ListView1.Items)
        {
            Repeater rpt = (Repeater)item.FindControl("NestedRepeater");
            HiddenField hdn = (HiddenField)item.FindControl("HdnId");

            var nest = from i in db.categories
                       where i.catid == Convert.ToInt32(hdn.Value)
                       select new
                       {
                           i.subcat_name
                       };

            rpt.DataSource = nest;
            rpt.DataBind();
        }
    }
}

这个想法是将 ID 存储在隐藏字段中,然后使用它来检索子类别。

这纯粹只是一个示例,也是使用嵌套控件的一种可能方式。

于 2013-11-07T23:01:48.903 回答