Asp.net 1.1 Treeview 控件的一个简单的数据绑定,Asp.net1.1自身没有Treeview控件,可以从网上download安装即可,树形控件绑定的一个重要的思想就是递归的思想,所以在绑定的时候就要使用递归的方法,考录到性能问题,通常是将数据一次性load到一个容器中如:缓存,然后根据条件在缓存中将数据绑定到树形控件上,下面是个简单的例子
后台代码:
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12using Microsoft.Web.UI.WebControls;
13namespace TreeTest
14{
15 /**//// <summary>
16 /// WebForm1 的摘要说明。
17 /// </summary>
18 public class WebForm1 : System.Web.UI.Page
19 {
20 protected Microsoft.Web.UI.WebControls.TreeView Tree2;
21
22 private void Page_Load(object sender, System.EventArgs e)
23 {
24 if(!IsPostBack)
25 {
26 InitTree();
27 }
28 }
29
30 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
31 override protected void OnInit(EventArgs e)
32 {
33 //
34 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
35 //
36 InitializeComponent();
37 base.OnInit(e);
38 }
39
40 /**//// <summary>
41 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
42 /// 此方法的内容。
43 /// </summary>
44 private void InitializeComponent()
45 {
46 this.Load += new System.EventHandler(this.Page_Load);
47
48 }
49 #endregion
50
51
52 private void InitTree()
53 {
54 try
55 {
56 TreeNode node=new TreeNode();
57 node.Text="根节点";
58 node.ID="-1";
59 string sqlString="Select * From Tree Where ParentID=0";
60 Tree2.Nodes.Add(node);
61 SqlDataReader rd=GetDataReader(sqlString);
62 while(rd.Read())
63 {
64 string pId=rd["ID"].ToString();
65 TreeNode node1=new TreeNode();
66 node1.Text=rd.GetString(1);
67 node1.ID=rd["ID"].ToString();
68 node.Nodes.Add(node1);
69 MakeTree(node1,pId);
70 }
71 }
72 catch(Exception err){Console.WriteLine(err.Message);}
73 finally{}
74 }
75
76 private void MakeTree(TreeNode fnode,string pId)
77 {
78 try
79 {
80 TreeNode node;
81 string sqlString="Select ID,Name,ParentID From Tree Where ParentID="+pId;
82 SqlDataReader rd=GetDataReader(sqlString);
83 while(rd.Read())
84 {
85 string PID=rd["ID"].ToString();
86 node=new TreeNode();
87 node.Text=rd["Name"].ToString();
88 node.ID=rd["ID"].ToString();
89 fnode.Nodes.Add(node);
90 MakeTree(node,PID); //递归
91 }
92 rd.Close();
93 }
94 catch(Exception err)
95 {
96 Console.WriteLine(err.Message);
97 }
98 finally{}
99 }
100
101 private SqlDataReader GetDataReader(string sqlString)
102 {
103 try
104 {
105 string conString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
106 SqlConnection conn=new SqlConnection(conString);
107 SqlCommand cmd=new SqlCommand(sqlString);
108 conn.Open();
109 cmd.Connection=conn;
110 return cmd.ExecuteReader();
111 }
112 catch (System.Exception e)
113 {
114 return null;
115 }
116 finally
117 { }
118 }
119
120 }
121}
122
2<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TreeTest.WebForm1" %>
3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
4<HTML>
5 <HEAD>
6 <title>WebForm1</title>
7 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
8 <meta name="CODE_LANGUAGE" Content="C#">
9 <meta name="vs_defaultClientScript" content="JavaScript">
10 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
11 </HEAD>
12 <body MS_POSITIONING="GridLayout">
13 <form id="Form1" method="post" runat="server">
14 <FONT face="宋体">
15 <iewc1:TreeView id="Tree2" style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 160px" runat="server"></iewc1:TreeView></FONT>
16 </form>
17 </body>
18</HTML>
2drop table [dbo].[Tree]
3GO
4
5CREATE TABLE [dbo].[Tree] (
6 [ID] [int] IDENTITY (1, 1) NOT NULL ,
7 [Name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
8 [ParentID] [int] NOT NULL
9) ON [PRIMARY]
10GO
11