2013-04-05 97 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); 
    } 
} 

基本上是:每个组名的变化,我们创建了一个新的组和所有的孩子添加到该组,直到该组的名称再次改变时间。

相关问题