2013-04-05 93 views
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。例如,我們的歷史有一個關於我們的2的父頁面。

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-04-05 20:20:52

+0

請發佈一些示例數據以及如何讓它出現在您的頁面上。 – Melanie 2013-04-05 20:22:27

+0

例如,有(ID 1)首頁,(ID 2)關於,(ID 3)展示位置和(ID 4)聯繫我們。我想添加一個關於被稱爲我們的歷史的子頁面,該頁面的ID爲5,父ID爲2.我需要一個帶有我們的歷史的垂直列表,出現在About下面,然後我可以用CSS縮進。 – lauw0203 2013-04-05 20:33:42

回答

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); 
    } 
} 

基本上是:每個組名的變化,我們創建了一個新的組和所有的孩子添加到該組,直到該組的名稱再次改變時間。