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
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
My SQL query is like
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. 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.
OUTPUT with Current Code
HOME | Page2 | Page3 | Page4
Answer Nothing is wrong with the code or logic it was the SQL Query which was getting the wrong data after fixing the query it worked like charm.