DataSet实现父子递归

 

private   void  Page_Load( object  sender, EventArgs e)
{
  
if (!this.IsPostBack)
  
{
    
this.buildMenu();
  }

}


private   void  buildMenu()
{
  DataSet ds 
= new DataSet();
  ComponentArt.Web.UI.NavBarItem newitm ;
  ds.ReadXml(
this.Server.MapPath("menudataset.xml"));
  ds.Relations.Add(
"NodeRelation", ds.Tables[0].Columns["ItemId"], ds.Tables[0].Columns["ParentItemId"]);
  
foreach (System.Data.DataRow dbRow in ds.Tables[0].Rows)
  
{
    
if (dbRow.IsNull("ParentItemId"))
    
{
      newitm 
= this.CreateItem(dbRow["MenuText"].ToString(), dbRow["WebPage"].ToString(), true);
      
this.NavBar1.Items.Add(newitm );
      
this.PopulateSubItem(dbRow, newitm );
    }

  }

}


private  ComponentArt.Web.UI.NavBarItem CreateItem( string  MenuText,  string  WebPage,  bool  expanded)
{
  ComponentArt.Web.UI.NavBarItem itm 
= new ComponentArt.Web.UI.NavBarItem();
  itm.Text 
= MenuText;
  itm.NavigateUrl 
= WebPage;
  
return itm;
}


private   void  PopulateSubItem(System.Data.DataRow dbRow, ComponentArt.Web.UI.NavBarItem itm)
{
  
foreach (System.Data.DataRow childRow in dbRow.GetChildRows("NodeRelation"))
  
{
    ComponentArt.Web.UI.NavBarItem childitm 
= this.CreateItem(childRow["MenuText"].ToString(), childRow["WebPage"].ToString(), true);
    itm.Items.Add(childitm);
    
this.PopulateSubItem(childRow , childitm);
  }

}


 

<? xml version="1.0" standalone="yes" ?>
< NewDataSet >
< Table >
< ItemId > 1 </ ItemId >
< MenuText > Departments </ MenuText >
</ Table >
< Table >
< ItemId > 2 </ ItemId >
< ParentItemId > 1 </ ParentItemId >
< MenuText > Sales </ MenuText >
< WebPage > sales.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 3 </ ItemId >
< ParentItemId > 1 </ ParentItemId >
< MenuText > Production </ MenuText >
< WebPage > prod.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 4 </ ItemId >
< ParentItemId > 1 </ ParentItemId >
< MenuText > Product Support </ MenuText >
< WebPage > supp.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 5 </ ItemId >
< ParentItemId > 1 </ ParentItemId >
< MenuText > Administration </ MenuText >
< WebPage > admin.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 6 </ ItemId >
< ParentItemId > 1 </ ParentItemId >
< MenuText > IT </ MenuText >
< WebPage > it.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 7 </ ItemId >
< MenuText > Dealers </ MenuText >
</ Table >
< Table >
< ItemId > 8 </ ItemId >
< ParentItemId > 3 </ ParentItemId >
< MenuText > Development </ MenuText >
< WebPage > dev.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 9 </ ItemId >
< ParentItemId > 3 </ ParentItemId >
< MenuText > Research </ MenuText >
< WebPage > resrch.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 10 </ ItemId >
< ParentItemId > 3 </ ParentItemId >
< MenuText > QC </ MenuText >
< WebPage > qc.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 11 </ ItemId >
< ParentItemId > 7 </ ParentItemId >
< MenuText > Arder </ MenuText >
< WebPage > arder.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 12 </ ItemId >
< ParentItemId > 7 </ ParentItemId >
< MenuText > Finar </ MenuText >
< WebPage > finar.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 13 </ ItemId >
< ParentItemId > 7 </ ParentItemId >
< MenuText > Xarco </ MenuText >
< WebPage > xarco.aspx </ WebPage >
</ Table >
< Table >
< ItemId > 14 </ ItemId >
< MenuText > About </ MenuText >
< WebPage > about.aspx </ WebPage >
</ Table >
</ NewDataSet >  

你可能感兴趣的:(Data)