0

我正在尝试创建一个数据库驱动的菜单,但我不确定如何使用我实现代码的方式将我的子页面排列在我的父页面下方。

SQL 调用:

/// <summary>
    /// Get all of the pages on the website
    /// </summary>
    /// <returns></returns>
    public static DataTable GetAllWebsitePages()
    {
        DataTable returnVal = new DataTable();
        using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["websiteContent"].ConnectionString))
        {
            sqlCon.Open();
            string SQL = "SELECT * FROM Website_Pages";
            using (SqlCommand sqlComm = new SqlCommand(SQL, sqlCon))
            {
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlComm))
                {
                    dataAdapter.Fill(returnVal);
                }
            }
            sqlCon.Close();
        }
        return returnVal;
    }

C#:

private void refreshPages()
{
    lvPages.DataSource = CMS.WebsitePages.Pages.GetAllWebsitePages();
    lvPages.DataBind();
}

ASP.NET:

<ItemTemplate>
            <tr>
                <td>
                    <asp:Label runat="server" ID="lblTitle" CssClass="cmsTitle"><%#Eval("Website_Page_Name")%></asp:Label>
                </td>
                <td>
                    <asp:ImageButton runat="server" ID="btnEdit" CommandArgument='<%#Eval("Website_Page_ID")%>'
                        CommandName="editPage" ImageUrl="../../images/NTU-CMS-Edit.png" />
                    &nbsp;&nbsp;&nbsp;
                    <asp:ImageButton runat="server" ID="btnDelete" CommandArgument='<%#Eval("Website_Page_ID")%>'
                        CommandName="deletePage" OnClientClick="return confirm('Do you want to delete this page?');"
                        ImageUrl="../../images/NTU-CMS-Delete.png" />
                </td>
            </tr>
        </ItemTemplate>

在数据库中,我的所有页面都有一个 ID,子页面有一个父 ID。例如,Our History 的父页面为 2,即 About Us。

4

1 回答 1

0

对于这样的分组数据有一种古老的技术 - 首先是让你的 SQL 输出这样的东西......

Parent     Child
---------- ----------
Home       Home
Products   Products
Products   Widget
Products   Midget
Products   Fidget
About Us   About Us
About Us   History
About Us   Contact Us

然后你遍历它并使用每一行来构建你的菜单......以下是伪代码......

String currentGroup = "";
MenuItem currentParentMenuItem = null;
foreach(DBRow r in results) {
  if (currentGroup != r.Group) {
      //create a new group
      currentGroup = r.Group;

      //add that group as a "Parent" item
      //Store the Parent in a MenuItem so we can add the children next time through
      currentParentMenuItem = newParentWeCreatedAbove;
  } else {
      //add the current row as a child to the current Parent menu group
      currentParentMenuItem.AddChild(r.Child);
  }
}

基本上就是:每次组名更改时,我们都会创建一个新组并将所有子组添加到该组,直到组名再次更改。

于 2013-04-05T20:32:51.367 回答