我有一个数据表如下:
id menuname url parentid
1 Home ~/Home.aspx NULL
2 Product ~/products.aspx NULL
3 Services ~/services.aspx NULL
4 ERP ~/erp.aspx 2
5 HRM ~/hrm.aspx 4
7 Payroll ~/payroll.aspx 4
8 Programming ~/programming.aspx 3
9 Advertising ~/advert.aspx 3
10 Television Advert ~/tvadvert.aspx 9
11 Radio Advert ~/radioadvert.aspx 9
........
........
所以我想根据上面的数据表将菜单项生成到一个无序列表中,这样具有 null parentid 的项目应该是第一级菜单,而其他项目将是基于它们的 parentid 的子菜单,如下所示:
<ul class="menu">
<li><a href="home.aspx">Home</a></li>
<li><a href="produc.aspx">Product</a>
<ul>
<li>
<a href="erp.aspx">ERP</a>
<ul>
<li><a href="hrm.aspx">HRM</a></li>
<li><a href="payroll.aspx">Payroll</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="services.aspx">Services</a>
<ul>
<li><a href="programming.aspx">Advertising</a></li>
<li><a href="advert.aspx">Programming</a></li>
</ul>
</li>
.....etc
</ul>
下面是我看起来不完整的代码:
public static String AddToList()
{
DataTable table = new DataTable();
table = GetMenus();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (DataRow row in table.Rows())
{
string parentId = row["parentmenuId"].ToString();
//string url = Server.MapPath(m.Url);
if (string.IsNullOrEmpty(parentId))
{
sb.Append(String.Format("<ul class=\"menu\"><li><a href=\"{0}\">{1}</a></li></ul>", row["Url"].ToString(), row["Description"].ToString()));
}
}
return sb.ToString();
}
This gets all top menu but all other effort to get submenu doesnt work. Pls help me out.
Thanks in advance