简体   繁体   中英

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

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

答案代码或逻辑没什么不对,是因为SQL查询在修复了像魅力一样的查询后得到了错误的数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM