My Opera is closing 3rd of March

Hoang Ha's Blog

Hoang Ha lives in HCMC, Viet Nam

Playing around with Menu Control in ASP.NET 2.0

, ,

Asp.net 2.0 is comming soon ( I mean officially released final version ) and its bringing lots of cool stuff with it. One of the cool controls is tree view control. I spent some time looking into the tree view control. Depending on your requirments you can populate the tree view control using different sources.

Populating using the SiteMapDataSource:

private void CreateMenuControl()
    {

        Menu1.DataSource = GetSiteMapDataSource();
        Menu1.DataBind();         
            
    }
 
   private SiteMapDataSource GetSiteMapDataSource()
    {
        XmlSiteMapProvider xmlSiteMap = new XmlSiteMapProvider();
        System.Collections.Specialized.NameValueCollection myCollection = new System.Collections.Specialized.NameValueCollection(1);
        myCollection.Add("siteMapFile", "Web.sitemap");
        xmlSiteMap.Initialize("provider", myCollection);
        xmlSiteMap.BuildSiteMap();

        SiteMapDataSource siteMap = new SiteMapDataSource();

        return siteMap;
    }


Populating using Database:

private void PopulateMenu()
    {
        DataSet ds = GetDataSetForMenu(); 
        
        Menu menu = new Menu();
            
        foreach (DataRow parentItem in ds.Tables["Categories"].Rows)
        {
            MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]);
            menu.Items.Add(categoryItem);

            foreach (DataRow childItem in parentItem.GetChildRows("Children"))
            {
                MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]);
                categoryItem.ChildItems.Add(childrenItem);
            }
        }

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

    }
    
  private DataSet GetDataSetForMenu()
    {
        SqlConnection myConnection = new SqlConnection(GetConnectionString());
        SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM Categories", myConnection);
        SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM Products", myConnection);

        DataSet ds = new DataSet();
        adCat.Fill(ds, "Categories");
        adProd.Fill(ds, "Products"); 

        ds.Relations.Add("Children",ds.Tables["Categories"].Columns["CategoryID"],ds.Tables["Products"].Columns["CategoryID"]);
        return ds; 

    }    


XML file to populate the menu:

private void CreateMenuWithXmlFile()
    {
        string path = @"C:\MyXmlFile.xml";
        DataSet ds = new DataSet();
        ds.ReadXml(path);

        Menu menu = new Menu();
        menu.MenuItemClick += new MenuEventHandler(menu_MenuItemClick);


        for (int i = 0; i < ds.Tables.Count; i++)
        {
            MenuItem parentItem = new MenuItem((string)ds.Tables[i].TableName);
            menu.Items.Add(parentItem);


            for (int c = 0; c < ds.Tables[i].Columns.Count; c++)
            {
                MenuItem column = new MenuItem((string)ds.Tables[i].Columns[c].ColumnName);
                menu.Items.Add(column);


                for (int r = 0; r < ds.Tables[i].Rows.Count; r++)
                {
                    MenuItem row = new MenuItem((string)ds.Tables[i].Rows[r][c].ToString());
                    parentItem.ChildItems.Add(row); 
                }
            }
            
          
        }


Hope this helps.

ASP.NET AJAX Tabs Control Creating a Crystal Report Viewer Control - Crystal Report in ASP.NET

Write a comment

New comments have been disabled for this post.

February 2014
M T W T F S S
January 2014March 2014
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28