0

I have to create dynamic menus from the database. I used the following example which only shows the parent menus but doesn't show the child menu. http://www.dotnetfunda.com/articles/article1477-how-to-create-a-menu-in-aspnet-using-aspmenu-control.aspx

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 xyz. when AddChildItems function is called for the match child row it just skill the loop and doesn't show any thing from teh child rows.

OUTPUT with Current Code

HOME | Page2 | Page3 | Page4

It should show me sub menus for the Page2 based on my database. rather it only shows parent page links as above.

2
  • It would be great if someone can tell me what is wrong with the above code.. Commented Apr 2, 2012 at 13:47
  • you have see previous answered stackoverflow.com/questions/3623818/… Commented Apr 19, 2012 at 10:17

1 Answer 1

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);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.