我正在尝试通过从 MySQL 表中检索数据来生成菜单 Web 控件。我已经将所有数据存储在 DataTable 中,但到目前为止我能够做到一个级别。我确定这里一定有一个递归函数,但我不知道如何添加到更深层次。
这是我到目前为止所做的:
public Menus(Menu menu, int menuId)
{
Database db = new Database("localhost", "dotnetcms", "root");
DataTable data;
query = "SELECT * FROM links WHERE menu_id = " + menuId + " ORDER BY `order`, parent ASC";
queryRows = "SELECT COUNT(*) FROM links WHERE menu_id = " + menuId + " ORDER BY `order`, parent ASC";
data = db.AQuery(query);
foreach (DataRow row in data.Rows)
{
if (row[3] == DBNull.Value)
{
menu.Items.Add(new MenuItem(row[1].ToString()));
}
else
{
AddChildMenus(row, menu.Items[Convert.ToInt32(row[3]) - 1]);
}
}
}
private void AddChildMenus(DataRow dr, MenuItem parent)
{
parent.ChildItems.Add(new MenuItem(dr[1].ToString()));
}