无限级TreeView导航

本文介绍了一个使用ASP.NET构建的用户控件——左侧导航栏,该控件通过解析数据表中的导航信息来动态生成树形菜单结构。文章详细展示了如何遍历数据源并递归地创建树节点。

 

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace GlistenCD.AdminUI.Uc
{
    
public partial class LeftNavgationBar : System.Web.UI.UserControl
    
{
        
private DataTable _navgationDataTable;
        
public DataTable NavgationDataTable
        
{
            
set
            
{
                
if (value != null)
                
{
                    _navgationDataTable 
= value;
                    InitTree();
                }

            }

        }

        
protected void Page_Init(object sender, EventArgs e)
        
{
        }


        
private void InitTree()
        
{
            
int max = Int32.Parse(_navgationDataTable.Rows[0]["NavgationParent"].ToString());         
            
for (int i = 0; i < _navgationDataTable.Rows.Count; i++)
            
{                
                
if (max < Int32.Parse(_navgationDataTable.Rows[i]["NavgationParent"].ToString()))
                
{
                    max 
= Int32.Parse(_navgationDataTable.Rows[i]["NavgationParent"].ToString());                    
                }

            }

            TreeNode rt 
= new TreeNode();
            rt.Text 
= "Home";
            AddChildNode(
0, rt, max);
            NavTree.Nodes.Add(rt);
        }


        
private void AddChildNode(int parentID, TreeNode ParentNode,int max)
        
{
            
for (int i = 0; i < _navgationDataTable.Rows.Count; i++)
            
{                
                
if (Int32.Parse(_navgationDataTable.Rows[i]["NavgationParent"].ToString()) == parentID)
                
{                    
                    TreeNode tn 
= new TreeNode();
                    tn.Text 
= _navgationDataTable.Rows[i]["NavgationTitle"].ToString();
                    tn.Value 
= _navgationDataTable.Rows[i]["NavgationID"].ToString();
                    tn.NavigateUrl 
= _navgationDataTable.Rows[i]["NavgationUrl"].ToString();
                    tn.ToolTip 
= _navgationDataTable.Rows[i]["NavgationTip"].ToString();
                    tn.Expanded 
= false;
                    ParentNode.ChildNodes.Add(tn);

                    
if (Int32.Parse(_navgationDataTable.Rows[i]["NavgationParent"].ToString()) < max)
                        AddChildNode(Int32.Parse(tn.Value), tn, max);                    
                }

            }

        }


    }

}

     

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值