在父页面下面排序子页面

本文关键字:排序 | 更新日期: 2023-09-27 18:07:47

我试图创建一个数据库驱动的菜单,但我不确定如何安排我的子页面下面我的父页面与我已经实现了我的代码的方式。

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。净:

<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。例如,我们的历史有一个父页2,即About Us。

在父页面下面排序子页面

对于分组数据有一种古老的技术-首先是让您的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);
  }
}

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