Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a problem i.e. related to menus actually i want that at run time menu and submenu should be generated.what shoul i do for that.i have used one code in that i have 2 menus taht are in table and there are 4 menus regarding to one but its not showing.its showing only one at a time.. and another one is showing for another menu.i m using this code:>

C#
void menu_MenuItemClick(object sender, MenuEventArgs e)
    {
        string selected = e.Item.Text;
        //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlDataAdapter adCat = new SqlDataAdapter("SELECT menu_id FROM menu where menu_name='" + selected + "'", con);
        DataTable dt = new DataTable();
        adCat.Fill(dt);
        int id = Convert.ToInt32(dt.Rows[0]["menu_id"].ToString());
        //Response.Redirect("Default2.aspx?id=" + id);

    }
    private void PopulateMenu()
    {
        DataSet ds = GetDataSetForMenu();

        Menu menu = new Menu();
        
        menu.MenuItemClick += new MenuEventHandler(menu_MenuItemClick);
        foreach (DataRow parentItem in ds.Tables["menu"].Rows)
        {
            MenuItem categoryItem = new MenuItem((string)parentItem["menu_name"]);

            menu.Items.Add(categoryItem);

            foreach (DataRow childItem in parentItem.GetChildRows("Children"))
            {

                MenuItem childrenItem = new MenuItem((string)childItem["submenu_name"]);
                categoryItem.ChildItems.Add(childrenItem);
                
            }
            
        }

        Panel1.Controls.Add(menu);
        Panel1.DataBind();

    }
    private DataSet GetDataSetForMenu()
    {
        
        SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM menu", con);
        SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM submenu", con);

        DataSet ds = new DataSet();
        adCat.Fill(ds,"menu");
        adProd.Fill(ds,"submenu");

        ds.Relations.Add("Children", ds.Tables["menu"].Columns["menu_id"], ds.Tables["submenu"].Columns["submenu_id"],false);
        return ds;

    }


and at page load this method i.e. PopulateMenu(); is called.
Posted
Updated 1-Nov-11 1:06am
v2

1 solution

Hi,

just simple change

C#
foreach (DataRow parentItem in ds.Tables["menu"].Rows)
        {
            MenuItem categoryItem = new MenuItem((string)parentItem["menu_name"]);
 
            
 
            foreach (DataRow childItem in parentItem.GetChildRows("Children"))
            {
 
                MenuItem childrenItem = new MenuItem((string)childItem["submenu_name"]);
                categoryItem.ChildItems.Add(childrenItem);
                
            }
            menu.Items.Add(categoryItem);
            //you've to add categoryitem after adding childitems to it
        }


check it once

All the Best
 
Share this answer
 
Comments
Umashankar Yadav 1-Nov-11 8:15am    
sir its not working properly actually our first menu is getting only 1 subitem not all.how is it possible?????
Umashankar Yadav 1-Nov-11 8:16am    
its not adding all childitems...why so?
Muralikrishna8811 1-Nov-11 8:19am    
can you specify your database design
Umashankar Yadav 1-Nov-11 8:28am    
ok sure
i have 2 tables one is menu which has 2 columns named,menu_id int,menu_name varchar(50), and other one is submenu inwhich there are 3 columns that are submenu_id int,submenu_name varchar(50),menu_id int
Muralikrishna8811 1-Nov-11 8:31am    
k let me try

you wanna display first menu right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900