Dynamically binding the Menu



Binding the menu.

MenuItem objMenuItem = new MenuItem();
        objMenuItem.Text = "Home";
        objMenuItem.NavigateUrl = "home.aspx";
        MainMenu.Items.Add(objMenuItem);



Dynamically Binding the menu and SubMenu from the DataBase.

private void BindMain()
    {
        DataTable dtmenuBind = new DataTable();
        clsGetmenu objMenuBinding = new clsGetmenu();
        dtmenuBind = objMenuBinding.GetMenu();
        if (dtmenuBind.Rows.Count != 0)
        {
            for (int i = 0; i < dtmenuBind.Rows.Count; i++)
            {
                MenuItem objMenuItem = new MenuItem();
                objMenuItem.Text = dtmenuBind.Rows[i]["MenuName"].ToString();
            
                MainMenu.Items.Add(objMenuItem);
                BindSubMenu(objMenuItem);
            }
        }

    }
    private void BindSubMenu(MenuItem MenuName)
    {
        MenuItem objSubMenuItem= new MenuItem();
        objSubMenuItem.Text = MenuName.Text   + "1";
        MenuName.ChildItems.Add(objSubMenuItem);

        
    }


To make the menu horizontal just change the Property“Orientation” to horizontal

HappyCoding ! 😉

Advertisements
%d bloggers like this: