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 には About Us という 2 の親ページがあります。

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 に答える