Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

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:

Web.sitemap is also a simple xml file. But I just wanted to show that you can also populate the menu using ordinary XML file.

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

 

 

 

 

 

powered by IMHO

Posted on Saturday, September 17, 2005 10:10 PM | Back to top


Comments on this post: Playing around with Menu Control in ASP.NET 2.0

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
hi,
in last section u have populated the menu using simple XML file .....could u please show me what would be the XML file format for menu like this

Level 1 -> Level 1.1
Level 1.2
Level 2 -> Level 2.1
Level 2.2


..................................


thank u
Left by pranav on Oct 17, 2005 8:46 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
Ohh sorry about that the XML file looks something like this:

<fruit>

<green taste="bad">

<pears>
My Pear
</pears>

<guana>
My Guana
</guana>

<watermelon>
My Water melon
</watermelon>

</green>

<yellow>
<banana>
My banana
</banana>

</yellow>



</fruit>
Left by Azamsharp on Oct 17, 2005 12:23 PM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
hi,
how do i bind the menu with data from database but design the look in aspx, i.e., not using panel, how would i do that?

thanks
aaron
Left by aaron on Oct 18, 2005 5:02 PM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
You don't need to use the Panel control if you are not making the tree dynamically. Simply add the childs and bind the tree control on the page thats it.

Left by Azamsharp on Oct 19, 2005 2:44 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
Hi,

Can we place Checkboxes inside the Menu Control of ASP.Net 2.0. If it is please give us some information how to do that... any reference sites that would be helpful for us.

Thanks
MDeevi
Left by MDeevi on Oct 19, 2005 4:51 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
In your example "Populating using Database: " for a Menu, how do you tie the associated URL of the web page you want the user to go to?
Left by Rick Gates on Oct 24, 2005 9:04 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
how to populate menucontrols using the menu's datasource?
Left by pran on Nov 20, 2005 7:35 PM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
Hello, does anyone know how to programatically create flyout menus using the ASP. net 2.0 controls?


Thanks
Left by MHOward on Apr 26, 2006 9:01 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
Really HelpFul Article !
Left by Raj on May 18, 2006 6:53 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
I am populating menu control with database. How can I trun off script postback off. Because in my case when I have to click first time the menu doing postback and on second click it going to required URL.

Thanks
Left by Mohammad on Jun 06, 2007 3:40 PM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
i want to create menu for my project using ASP.NET 2.0 menu control & XML file can give me some ideas & also i want the menu control like this
Book File
->System ->Oprating
->Data ->Data
what is the format for above for XML
Left by Pankaj on Aug 29, 2007 4:08 AM

# re: Playing around with Menu Control in ASP.NET 2.0
Requesting Gravatar...
Hi,

I have try to work with the menu click item using master page.the result is not load i \\n single click.
it take two post back to load the result.i have check wether any postback option for the menu but no post back option for thr menu.plz help me
Left by vani on Sep 29, 2007 2:40 AM

# how to add url to menu items
Requesting Gravatar...
Hi.. I plz tell me how to add url to menu items when i am populating it with database.
Left by Needhi on May 07, 2008 7:38 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net