以编程方式在Asp.net c#web表单中生成菜单

时间:2012-04-02 11:57:09

标签: asp.net c#-4.0 menu webforms

我必须从数据库创建动态菜单。我使用了以下示例,该示例仅显示父菜单但不显示子菜单。 http://www.dotnetfunda.com/articles/article1477-how-to-create-a-menu-in-aspnet-using-aspmenu-control.aspx

我使用的代码段

<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);
        }
    }

我不确定我做错了什么。根据我的数据库,它应该显示行xyz的子菜单。当为匹配子行调用AddChildItems函数时,它只是熟悉循环并且不显示来自子行的任何东西。

使用当前代码输出

首页|第2页第3页|第4页

它应该根据我的数据库显示Page2的子菜单。相反,它只显示上面的父页面链接。

1 个答案:

答案 0 :(得分:0)

public void FillMenuControl(int ACCESSID)
    {
        try
        {
            int AcessID;
            int UserSessionid = 1;
            dtchild = new DataTable();
            OBJClsMenuList = new ClsMenuList();
            ds = new DataSet();
            OBJClsMenuList = new ClsMenuList();
            ds = OBJClsMenuList.GetParentMenuItems(ACCESSID,'G');

            dt = new DataTable();
            dt = ds.Tables[0];
            MenuItem item1;
            MenuItem childitems;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    item1 = new MenuItem();
                    string t = dt.Rows[i][0].ToString();
                    item1.Value = dt.Rows[i][0].ToString();
                    item1.Text = dt.Rows[i][1].ToString();
                    item1.NavigateUrl =  dt.Rows[i][2].ToString();
                    Menu1.Items.Add(item1);

                    dschild = OBJClsMenuList.GetChildMenuitems(Convert.ToInt32(dt.Rows[i][0].ToString()),'G');
                    dtchild = new DataTable();
                    dtchild = dschild.Tables[0];
                    for (int j = 0; j < dtchild.Rows.Count; j++)
                    {
                        childitems = new MenuItem();
                        childitems.Value = dtchild.Rows[j][0].ToString();
                        childitems.Text = dtchild.Rows[j][1].ToString();
                        AcessID = Convert.ToInt32(dtchild.Rows[j][3].ToString());
                        if (UserSessionid >= AcessID)
                        {
                            childitems.NavigateUrl = dtchild.Rows[j][2].ToString();
                        }
                        else
                        {
                            childitems.NavigateUrl = "~/login.aspx";
                        }

                        Menu1.Items[i].ChildItems.Add(childitems);
                    }
                }
            }
            else
            {

            }
        }

        catch (Exception ex)
        {
            string err = ex.Message;
            if (ex.InnerException != null)
            {
                err = err + " :: Inner Exception :- " + ex.InnerException.Message;
            }
            string addInfo = "Error  :: -> ";
            ClsExceptionPublisher objPub = new ClsExceptionPublisher();
            objPub.Publish(err, addInfo);
        }
    }