How to populate menu from the SiteMapDataSourc, Database, xml files.

  How to populate menu from the SiteMapDataSourc, Database, xml files.

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

转载于:https://www.cnblogs.com/Amanda2007/archive/2007/11/26/972610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值