在VS 2005中使用TREEVIEW控件
在VS2005中提供了TREE VIEW控件,在本人的 远友CRM系统 中,左侧菜单栏就是使用的该控件(后来发现VS2005中的MENU控件也可以实现相同的功能)
以下特把实现代码贴出来,供大家参考。
.ASPX页面代码:
数据库表的设计说明:
以下特把实现代码贴出来,供大家参考。
.ASPX页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="Left" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="LeftTree" runat="server" ShowLines="True" Font-Size="10pt">
</asp:TreeView>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="LeftTree" runat="server" ShowLines="True" Font-Size="10pt">
</asp:TreeView>
</form>
</body>
</html>
以下是.ASPX.CS代码页面源码:
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class Left : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
InitTree();
}
}
// 递归绑定同一个表数据
private void InitTree()
{
DataTable dt = GetTreeViewTable();
DataView dv = new DataView(dt);
dv.RowFilter = "TreeParentID='0'";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["TreeName"].ToString();
node.Value = drv["TreeValue"].ToString();
node.ImageUrl = drv["TreeIco"].ToString();
node.Expanded = true;
LeftTree.Nodes.Add(node);
AddReplies(dt,node);
}
}
private DataTable GetTreeViewTable()
{
string constring = System.Configuration.ConfigurationSettings.AppSettings["OledbConnectionStr"];
OleDbConnection con = new OleDbConnection(constring);
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM TreeTb", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private void AddReplies(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
dv.RowFilter = "TreeParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
{
TreeNode replyNode = new TreeNode();
replyNode.Text = row["TreeName"].ToString();
replyNode.Value = row["TreeValue"].ToString();
replyNode.ImageUrl = row["TreeIco"].ToString();
replyNode.NavigateUrl = row["TreeLink"].ToString().Trim();
replyNode.Target = row["TargetStr"].ToString().Trim();
replyNode.Expanded = false;
node.ChildNodes.Add(replyNode);
AddReplies(dt,replyNode);
}
}
}
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class Left : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
InitTree();
}
}
// 递归绑定同一个表数据
private void InitTree()
{
DataTable dt = GetTreeViewTable();
DataView dv = new DataView(dt);
dv.RowFilter = "TreeParentID='0'";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["TreeName"].ToString();
node.Value = drv["TreeValue"].ToString();
node.ImageUrl = drv["TreeIco"].ToString();
node.Expanded = true;
LeftTree.Nodes.Add(node);
AddReplies(dt,node);
}
}
private DataTable GetTreeViewTable()
{
string constring = System.Configuration.ConfigurationSettings.AppSettings["OledbConnectionStr"];
OleDbConnection con = new OleDbConnection(constring);
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM TreeTb", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private void AddReplies(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
dv.RowFilter = "TreeParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
{
TreeNode replyNode = new TreeNode();
replyNode.Text = row["TreeName"].ToString();
replyNode.Value = row["TreeValue"].ToString();
replyNode.ImageUrl = row["TreeIco"].ToString();
replyNode.NavigateUrl = row["TreeLink"].ToString().Trim();
replyNode.Target = row["TargetStr"].ToString().Trim();
replyNode.Expanded = false;
node.ChildNodes.Add(replyNode);
AddReplies(dt,replyNode);
}
}
}
数据库表的设计说明:
远友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 |
√ |
|
相信上面的代码,大家都能看懂吧,因为不能把源码程序打包提供,所以只能以文字方式贴出部分了。