在VS 2005中使用TREEVIEW控件

在VS2005中提供了TREE VIEW控件,在本人的 远友CRM系统 中,左侧菜单栏就是使用的该控件(后来发现VS2005中的MENU控件也可以实现相同的功能)

以下特把实现代码贴出来,供大家参考。

.ASPX页面代码:

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="Left"  %>
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  id ="Head1"  runat ="server" >
None.gif    
< title > Untitled Page </ title >
None.gif
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif        
< asp:TreeView  ID ="LeftTree"  runat ="server"  ShowLines ="True"  Font-Size ="10pt" >
None.gif        
</ asp:TreeView >
None.gif
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >

 

以下是.ASPX.CS代码页面源码:

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Data.OleDb;
None.gif
None.gif
None.gif
None.gif
public  partial  class  Left : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitTree();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif   
// 递归绑定同一个表数据
InBlock.gif
    private void InitTree()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DataTable dt 
= GetTreeViewTable();
InBlock.gif        DataView dv 
= new DataView(dt);
InBlock.gif        dv.RowFilter 
= "TreeParentID='0'";
InBlock.gif        
InBlock.gif        
foreach (DataRowView drv in dv)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode node 
= new TreeNode();
InBlock.gif            node.Text 
= drv["TreeName"].ToString();
InBlock.gif            node.Value 
= drv["TreeValue"].ToString();
InBlock.gif            node.ImageUrl 
= drv["TreeIco"].ToString();
InBlock.gif            node.Expanded 
= true;
InBlock.gif            LeftTree.Nodes.Add(node);
InBlock.gif            AddReplies(dt,node);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private DataTable GetTreeViewTable()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string constring = System.Configuration.ConfigurationSettings.AppSettings["OledbConnectionStr"];
InBlock.gif        OleDbConnection con 
= new OleDbConnection(constring);
InBlock.gif        OleDbDataAdapter da 
= new OleDbDataAdapter("SELECT * FROM TreeTb", con);
InBlock.gif        DataTable dt 
= new DataTable();
InBlock.gif        da.Fill(dt);
InBlock.gif        
return dt;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void AddReplies(DataTable dt, TreeNode node)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DataView dv 
= new DataView(dt);
InBlock.gif        dv.RowFilter 
= "TreeParentID='" + node.Value + "'";
InBlock.gif        
foreach (DataRowView row in dv)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode replyNode 
= new TreeNode();
InBlock.gif            replyNode.Text 
= row["TreeName"].ToString();
InBlock.gif            replyNode.Value 
= row["TreeValue"].ToString();
InBlock.gif            replyNode.ImageUrl 
= row["TreeIco"].ToString();
InBlock.gif            replyNode.NavigateUrl 
= row["TreeLink"].ToString().Trim();
InBlock.gif            replyNode.Target 
= row["TargetStr"].ToString().Trim();
InBlock.gif            replyNode.Expanded 
= false;
InBlock.gif            node.ChildNodes.Add(replyNode);
InBlock.gif            AddReplies(dt,replyNode);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif   
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif


数据库表的设计说明:

远友CRM数据库表和字段说明

 

系统名称:远友CRM

设计者:李锡远

日期:2006-5-11

 

 

表:TreeTb(菜单树)

字段名

用途

数据类型说明

允许为空

备注

ID

自动ID

Int 4

×

 

TreeParentID

一级菜单树

Nchar 20

 

TreeValue

二级树非显值

Nchar 20

 

TreeName

二级树显示值

Nchar 20

 

TreeLink

菜单链接

Varchar 50

 

TargetStr

链接目标

Nchar 20

 

TreeIco

图标

Varchar 50

 

 

 

相信上面的代码,大家都能看懂吧,因为不能把源码程序打包提供,所以只能以文字方式贴出部分了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值