[英]Generate Menu from the database using asp:Menu control programatically not working
I am trying to create dynamic menus from the database using the following example http://www.dotnetfunda.com/articles/article1477-how-to-create-a-menu-in-aspnet-using-aspmenu-control.aspx 我正在尝试使用以下示例从数据库创建动态菜单:http://www.dotnetfunda.com/articles/article1477-how-to-create-a-menu-in-aspnet-using-aspmenu-control.aspx
I a modified the Code which is posted below only displays the parent menu but doesn't show the child menu, i am sure something is wrong while debugging the code i notice that it doesnt enter the foreach (DataRowView row in dvMenu) of AddChildItems Function 我修改了下面发布的代码,仅显示父菜单,但不显示子菜单,我确定调试代码时出了点问题,我注意到它没有进入AddChildItems函数的foreach(dvMenu中的DataRowView行)
My SQL query is like 我的SQL查询就像
Select PageID, PageName,PageInternalLinkURL, PageInheritance from pg_Pages
Code snippet i am using 我正在使用的代码段
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" >
// I am convert ds to table for now.
DataTable table = dsMenu.Tables[0]; ;
DataView dvMenu = new DataView(table);
dvMenu.RowFilter = "PageInheritance is NULL";
foreach (DataRowView row in dvMenu)
{
MenuItem menuItem = new MenuItem(row["PageName"].ToString(), row["PageId"].ToString());
menuItem.NavigateUrl = row["PageURL"].ToString() + "?PageId=" + row["PageId"] + "&Language=" + sLangCode;
Menu1.Items.Add(menuItem);
AddChildItems(dvMenu.Table, menuItem);
}
//Function to look for child menu
private static void AddChildItems(DataTable table, MenuItem menuItem)
{
DataView viewItem = new DataView(table);
viewItem.RowFilter = "PageInheritance = " + menuItem.Value;
foreach (DataRowView childView in viewItem)
{
MenuItem childItem = new MenuItem(childView["PageName"].ToString(),
childView["PageId"].ToString());
childItem.NavigateUrl = childView["PageURL"].ToString();
menuItem.ChildItems.Add(childItem);
AddChildItems(table, childItem);
}
}
I am not sure what i am doing wrong. 我不确定自己在做什么错。 Based on my database it should show me child menus for row Page2 .
根据我的数据库,应该向我显示第Page2行的子菜单。 when AddChildItems function is called for the match child row it just skill the loop and doesn't show any thing from the child rows.
当为匹配的子行调用AddChildItems函数时,它只是对循环进行技巧训练,而不会显示子行中的任何内容。
OUTPUT with Current Code 使用当前代码输出
HOME | 主页| Page2 |
Page2 | Page3 |
第3页 Page4
第4页
答案代码或逻辑没什么不对,是因为SQL查询在修复了像魅力一样的查询后得到了错误的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.