我正在尝试创建一个数据库驱动的菜单,但我不确定如何按照我实现代码的方式在子页面下方安排子页面。
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" />
<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 :(得分: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);
}
}
基本上就是:每次组名更改时,我们都会创建一个新组,并将所有子组件添加到该组,直到组名再次更改为止。