ASP.NET DEMO 18: 如何编程动态创建 Menu

ASP.NET DEMO 15: 如何编程动态创建 TreeView 的姊妹篇:

<% @ Page Language = " C# "   %>
<% @ Import Namespace = " System.Data "   %>
<% @ Import Namespace = " System.Data.OleDb "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< script runat = " server " >
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! IsPostBack) {
            CreateMenu();
        }
    }

    
///   <summary>
    
///  创建 Menu
    
///  此方法采取一次性加载目标数据到 DataTable,递归查询 DataTable ,创建子节点
    
///   </summary>
     private   void  CreateMenu()
    {
        
//
         const   int  VirtualRootId  =   0 //  虚拟的根节点 ID, 表中 ParentID=0 的节点表示无实际父节点
         const   string  SQL_SELECT  =   " SELECT NodeId, NodeName,ParentId FROM TreeView ORDER BY NodeId ASC " ;
        
        
//
         string  connStr  =   " Provider=Microsoft.Jet.OleDb.4.0;data source= "   +  Server.MapPath( " Menu_CreateMenuDynamicFromDb.mdb " ); ;        
        
//
        OleDbDataAdapter da  =   new  OleDbDataAdapter(SQL_SELECT, connStr);
        DataTable dt 
=   new  DataTable();
        
//
        da.Fill(dt);
        
//
        CreateMenuRecursive(mnuDemo.Items, dt, VirtualRootId);
    }

    
///   <summary>
    
///  递归查询数据,创建 MenuItem 节点
    
///   </summary>
    
///   <param name="items"></param>
    
///   <param name="dataSource"></param>
    
///   <param name="parentId"></param>
     private   void  CreateMenuRecursive(MenuItemCollection items, DataTable dataSource,  int  parentId)
    {
        
//
         string  fliter  =  String.Format( " ParentId={0} " , parentId);
        
//  查询子节点
        DataRow[] drArr  =  dataSource.Select(fliter);
        
        MenuItem item;        
        
foreach (DataRow dr  in  drArr) {                        
            
//
            item  =   new  MenuItem();
            items.Add(item);
            item.Text 
=  ( string )dr[ " NodeName " ];            
            
//  设置其他属性
            
//       
                       
            
//  递归创建子节点
            CreateMenuRecursive(item.ChildItems, dataSource, ( int )dr[ " NodeId " ]);
            
            
//  移除已添加行,提高性能
            dataSource.Rows.Remove(dr);
        }
    }

</ script >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head id = " Head1 "  runat = " server " >
    
< title > CreateMenuResursiveFromDb </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
   
< h1 > 动态创建Menu </ h1 >
        
< asp:Menu ID = " mnuDemo "  runat = " server "  ExpandDepth = " 1 "  BackColor = " #FFFBD6 "  
            DynamicHorizontalOffset
= " 2 "  Font - Names = " Verdana "  Font - Size = " 0.8em "  
            ForeColor
= " #990000 "  StaticSubMenuIndent = " 10px " >
            
< StaticSelectedStyle BackColor = " #FFCC66 "   />
            
< StaticMenuItemStyle HorizontalPadding = " 5px "  VerticalPadding = " 2px "   />
            
< DynamicHoverStyle BackColor = " #990000 "  ForeColor = " White "   />
            
< DynamicMenuStyle BackColor = " #FFFBD6 "   />
            
< DynamicSelectedStyle BackColor = " #FFCC66 "   />
            
< DynamicMenuItemStyle HorizontalPadding = " 5px "  VerticalPadding = " 2px "   />
            
< StaticHoverStyle BackColor = " #990000 "  ForeColor = " White "   />
        
</ asp:Menu >
    
</ div >
    
</ form >
</ body >
</ html >

 

示例下载

你可能感兴趣的:(ASP.NET DEMO 18: 如何编程动态创建 Menu)