1

前阵子问过这个问题,一直没反应,后来一直忙着别的事,今天又来了,还是破解不了。

我原来的问题:https ://stackoverflow.com/questions/11868289/using-a-one-to-many-relationship-to-show-a-category-menu-using-asp-repeaters-vb

现在,从那时起,我一直试图查看我在尝试遵循这篇文章http://www.codeproject.com/Articles/6140/A-quick-guide-to-using-nested-repeaters-in时做错了什么 -ASP-NET和各种 Microsfot 教程

我在填充数据集时遗漏了一步或误解了一些东西......现在早上花了很多时间在圈子里转,我很困惑。

它把我逼到了墙角。

我已经做到了这一点:(顺便说一下,我正在使用 ASP.NET 2.0 和 MySQL 数据库)

Sub getCategories() Handles Me.Load

    Dim DBConnection = ConfigurationManager.ConnectionStrings("dbConnNew").ConnectionString
    Dim connection As OdbcConnection = New OdbcConnection(DBConnection)
    Dim connect As New OdbcCommand("SELECT masterCat.name AS masterCat, masterCat.id as mcId, category.id as cId, category.name AS category, category.mapsTo as catMapsTo, subCat.mapsTo as subCatMapsTo, subCat.name AS subCat FROM masterCat INNER JOIN category ON masterCat.id = category.mapsTo INNER JOIN subCat ON category.id = subCat.mapsTo WHERE masterCat.id <> '9' ORDER BY masterCat.priority", connection)

    Dim DS As New DataSet()
    connection.Open()
    Dim read As OdbcDataReader = connect.ExecuteReader(CommandBehavior.CloseConnection)
    DS.Load(read, LoadOption.OverwriteChanges, "masterCat, category, subCat")

    'DS.Relations.Add("mCatid", DS.Tables("masterCat").Columns("mcId"), DS.Tables("category").Columns("cId")) << This Line Breaks

    connection.Close()

    masterCat.DataSource = DS
    masterCat.DataBind()

End Sub

Private Sub CategoryRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles masterCat.ItemDataBound
    Dim item As RepeaterItem = e.Item
    If (item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem) Then
        Dim catRpt = DirectCast(item.FindControl("catRepeater"), Repeater)
        Dim drv As DataRowView = DirectCast(item.DataItem, DataRowView)
        catRpt.DataSource = drv.CreateChildView("mcId")
        catRpt.DataBind()
    End If
End Sub

和标记:

         <ul>
            <asp:Repeater ID="masterCat" runat="server">
                <ItemTemplate>
                    <li>
                        <%# Container.DataItem("masterCat")%>
                        <ul>                            
                            <asp:Repeater ID="catRepeater" runat="server">
                            <ItemTemplate>
                                <li>
                                <%# Container.DataItem("category")%>
                            </li>
                            </ItemTemplate>
                            </asp:Repeater>
                        </ul>
                    </li>
                </ItemTemplate>
            </asp:Repeater>
         </ul>

回顾一下,我正在尝试生成一个包含主类别、类别和子类别的嵌套中继器。上面的代码只进行了两步,我认为第三步一定是重复的。

我的数据排列在三个表中,看起来像这样

masterCat

id    |    name    | priority (for sorting)

category

id    |    name    |    mapsTo (this is the ID of the masterCat to which this relates)

subCat

 id    |    name    | mapsTo (ID of the category to which this relates)

上面的 SQL 查询正确地返回了包含这些列的整个数据:

masterCat   - name of the master category
mcId        - ID of the master category
cId         - ID of a category
category    - name of a category
catMapsTo   - the ID of the masterCategory to which category relates
subCat       - subCategory name
subCatMapsTo - the ID of the category to which the subCategory relates

我最终需要的是看起来像的东西(在这个例子中只有两个层次)

  • 猫大师
    • 类别
    • 类别
    • 类别


  • 猫大师
    • 类别
    • 类别
    • 类别

  • 猫大师
    • 类别
    • 类别
    • 类别

        </li>
    

帮助将不胜感激!

4

0 回答 0