[原创]asp.net 2.0下的自定义树(myTreeView)
自己在项目里用的类,大家看一下~~
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Web.UI.WebControls;
6
7/**//*-----------------------------------------------------------
8 *
9 * Copyright (C) 2009 版权所有
10 *
11 * 文件名:myTreeView.cs
12 *
13 * myTreeView自定义树
14 *
15 * 主要功能:
16 * 压缩、解压
17 *
18 *
19 * 创建标识:进哥
20 * 作者:进哥
21 * 来源:http://www.cnblogs.com/pcsky/archive/2009/10/15/1584124.html
22 *
23-----------------------------------------------------------*/
24
25namespace LTP.Common
26{
27 /**//// <summary>
28 /// myTreeView 的摘要说明。
29 /// </summary>
30 public class myTreeView
31 {
32 private string _IDField;
33 private string _NameField;
34 private string _ParentField;
35 private string _RootID;
36 private string _NavigateUrl;
37 private string _NavigateArg;
38 private DataSet _Dst;
39 private string _Target;
40 private string _PageNameField;
41 private int _ExpandLevel;
42
43 /**//// <summary>
44 /// TreeView自定义基类
45 /// </summary>
46 /// <param name="IDField">ID字段</param>
47 /// <param name="NameField">Name字段</param>
48 /// <param name="ParentField">父级字段</param>
49 /// <param name="RootID">根节点号</param>
50 /// <param name="Dst">DataSet</param>
51 public myTreeView(string IDField, string NameField, string ParentField, string RootID, ref DataSet Dst)
52 {
53 _IDField = IDField;
54 _NameField = NameField;
55 _ParentField = ParentField;
56 _RootID = RootID;
57 _Dst = Dst;
58 _Target = "mainFrame";
59 _NavigateArg = "";
60 _ExpandLevel = 1;
61 }
62
63 public myTreeView()
64 {
65 }
66
67 参数#region 参数
68 /**//// <summary>
69 /// ID字段
70 /// </summary>
71 public string IDField
72 {
73 set { _IDField = value; }
74 get { return _IDField; }
75 }
76
77 /**//// <summary>
78 /// Name字段
79 /// </summary>
80 public string NameField
81 {
82 set { _NameField = value; }
83 get { return _NameField; }
84 }
85
86 /**//// <summary>
87 /// 父级字段
88 /// </summary>
89 public string ParentField
90 {
91 set { _ParentField = value; }
92 get { return _ParentField; }
93 }
94
95 /**//// <summary>
96 /// 根节点号
97 /// </summary>
98 public string RootID
99 {
100 set { _RootID = value; }
101 get { return _RootID; }
102 }
103
104 /**//// <summary>
105 /// 超链接
106 /// </summary>
107 public string NavigateUrl
108 {
109 set { _NavigateUrl = value; }
110 get { return _NavigateUrl; }
111 }
112
113 /**//// <summary>
114 /// 超链接参数字段
115 /// </summary>
116 public string NavigateArg
117 {
118 set { _NavigateArg = value; }
119 get { return _NavigateArg; }
120 }
121
122 /**//// <summary>
123 /// 页面名的字段
124 /// </summary>
125 public string PageNameField
126 {
127 set { _PageNameField = value; }
128 get { return _PageNameField; }
129 }
130
131
132 /**//// <summary>
133 /// 超链接目标
134 /// </summary>
135 public int ExpandLevel
136 {
137 set { _ExpandLevel = value; }
138 get { return _ExpandLevel; }
139 }
140
141
142 /**//// <summary>
143 /// 超链接目标
144 /// </summary>
145 public string Target
146 {
147 set { _Target = value; }
148 get { return _Target; }
149 }
150
151 /**//// <summary>
152 /// 超链接目标
153 /// </summary>
154 public DataSet Dst
155 {
156 set { _Dst = value; }
157 get { return _Dst; }
158 }
159 #endregion
160
161 public void BindTree(System.Web.UI.WebControls.TreeView _myTree)
162 {
163 _myTree.Nodes.Clear();
164 _myTree.MaxDataBindDepth = ExpandLevel;
165
166 InitTree(_myTree.Nodes, RootID);
167 }
168
169 private void InitTree(TreeNodeCollection _Nds, string _parentID)
170 {
171 TreeNode tmpNd;
172 DataRow[] rows = Dst.Tables[0].Select(ParentField + "=" + _parentID);
173
174 foreach (DataRow row in rows)
175 {
176 tmpNd = new TreeNode();
177 tmpNd.Value = row[IDField].ToString();
178 tmpNd.Text = row[NameField].ToString();
179
180 if (PageNameField != "" && PageNameField != null)
181 {
182 if (row[PageNameField].ToString() != "")
183 tmpNd.NavigateUrl = ReturnPlus.GetAppPath() + row[PageNameField].ToString();
184
185 if (NavigateArg != "")
186 {
187 if (row[NavigateArg].ToString() != null)
188 tmpNd.NavigateUrl = String.Format(row[PageNameField].ToString(), row[NavigateArg]);
189 }
190 }
191 else
192 {
193 tmpNd.NavigateUrl = String.Format(NavigateUrl, row[NavigateArg]);
194 }
195
196 if (tmpNd.NavigateUrl == "" || tmpNd.NavigateUrl == null)
197 {
198 //tmpNd.SelectAction = TreeNodeSelectAction.SelectExpand;
199 //tmpNd.NavigateUrl = "javascript:TreeView_ToggleNode(myTree_Data," + tmpNd. +",myTreen3,'l',myTreen3Nodes)"
200 //tmpNd.NavigateUrl = "#";
201 }
202 else
203 {
204 tmpNd.Target = Target;
205 }
206
207 _Nds.Add(tmpNd);
208 InitTree(tmpNd.ChildNodes, tmpNd.Value);
209 }
210 }
211 }
212}
213
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Web.UI.WebControls;
6
7/**//*-----------------------------------------------------------
8 *
9 * Copyright (C) 2009 版权所有
10 *
11 * 文件名:myTreeView.cs
12 *
13 * myTreeView自定义树
14 *
15 * 主要功能:
16 * 压缩、解压
17 *
18 *
19 * 创建标识:进哥
20 * 作者:进哥
21 * 来源:http://www.cnblogs.com/pcsky/archive/2009/10/15/1584124.html
22 *
23-----------------------------------------------------------*/
24
25namespace LTP.Common
26{
27 /**//// <summary>
28 /// myTreeView 的摘要说明。
29 /// </summary>
30 public class myTreeView
31 {
32 private string _IDField;
33 private string _NameField;
34 private string _ParentField;
35 private string _RootID;
36 private string _NavigateUrl;
37 private string _NavigateArg;
38 private DataSet _Dst;
39 private string _Target;
40 private string _PageNameField;
41 private int _ExpandLevel;
42
43 /**//// <summary>
44 /// TreeView自定义基类
45 /// </summary>
46 /// <param name="IDField">ID字段</param>
47 /// <param name="NameField">Name字段</param>
48 /// <param name="ParentField">父级字段</param>
49 /// <param name="RootID">根节点号</param>
50 /// <param name="Dst">DataSet</param>
51 public myTreeView(string IDField, string NameField, string ParentField, string RootID, ref DataSet Dst)
52 {
53 _IDField = IDField;
54 _NameField = NameField;
55 _ParentField = ParentField;
56 _RootID = RootID;
57 _Dst = Dst;
58 _Target = "mainFrame";
59 _NavigateArg = "";
60 _ExpandLevel = 1;
61 }
62
63 public myTreeView()
64 {
65 }
66
67 参数#region 参数
68 /**//// <summary>
69 /// ID字段
70 /// </summary>
71 public string IDField
72 {
73 set { _IDField = value; }
74 get { return _IDField; }
75 }
76
77 /**//// <summary>
78 /// Name字段
79 /// </summary>
80 public string NameField
81 {
82 set { _NameField = value; }
83 get { return _NameField; }
84 }
85
86 /**//// <summary>
87 /// 父级字段
88 /// </summary>
89 public string ParentField
90 {
91 set { _ParentField = value; }
92 get { return _ParentField; }
93 }
94
95 /**//// <summary>
96 /// 根节点号
97 /// </summary>
98 public string RootID
99 {
100 set { _RootID = value; }
101 get { return _RootID; }
102 }
103
104 /**//// <summary>
105 /// 超链接
106 /// </summary>
107 public string NavigateUrl
108 {
109 set { _NavigateUrl = value; }
110 get { return _NavigateUrl; }
111 }
112
113 /**//// <summary>
114 /// 超链接参数字段
115 /// </summary>
116 public string NavigateArg
117 {
118 set { _NavigateArg = value; }
119 get { return _NavigateArg; }
120 }
121
122 /**//// <summary>
123 /// 页面名的字段
124 /// </summary>
125 public string PageNameField
126 {
127 set { _PageNameField = value; }
128 get { return _PageNameField; }
129 }
130
131
132 /**//// <summary>
133 /// 超链接目标
134 /// </summary>
135 public int ExpandLevel
136 {
137 set { _ExpandLevel = value; }
138 get { return _ExpandLevel; }
139 }
140
141
142 /**//// <summary>
143 /// 超链接目标
144 /// </summary>
145 public string Target
146 {
147 set { _Target = value; }
148 get { return _Target; }
149 }
150
151 /**//// <summary>
152 /// 超链接目标
153 /// </summary>
154 public DataSet Dst
155 {
156 set { _Dst = value; }
157 get { return _Dst; }
158 }
159 #endregion
160
161 public void BindTree(System.Web.UI.WebControls.TreeView _myTree)
162 {
163 _myTree.Nodes.Clear();
164 _myTree.MaxDataBindDepth = ExpandLevel;
165
166 InitTree(_myTree.Nodes, RootID);
167 }
168
169 private void InitTree(TreeNodeCollection _Nds, string _parentID)
170 {
171 TreeNode tmpNd;
172 DataRow[] rows = Dst.Tables[0].Select(ParentField + "=" + _parentID);
173
174 foreach (DataRow row in rows)
175 {
176 tmpNd = new TreeNode();
177 tmpNd.Value = row[IDField].ToString();
178 tmpNd.Text = row[NameField].ToString();
179
180 if (PageNameField != "" && PageNameField != null)
181 {
182 if (row[PageNameField].ToString() != "")
183 tmpNd.NavigateUrl = ReturnPlus.GetAppPath() + row[PageNameField].ToString();
184
185 if (NavigateArg != "")
186 {
187 if (row[NavigateArg].ToString() != null)
188 tmpNd.NavigateUrl = String.Format(row[PageNameField].ToString(), row[NavigateArg]);
189 }
190 }
191 else
192 {
193 tmpNd.NavigateUrl = String.Format(NavigateUrl, row[NavigateArg]);
194 }
195
196 if (tmpNd.NavigateUrl == "" || tmpNd.NavigateUrl == null)
197 {
198 //tmpNd.SelectAction = TreeNodeSelectAction.SelectExpand;
199 //tmpNd.NavigateUrl = "javascript:TreeView_ToggleNode(myTree_Data," + tmpNd. +",myTreen3,'l',myTreen3Nodes)"
200 //tmpNd.NavigateUrl = "#";
201 }
202 else
203 {
204 tmpNd.Target = Target;
205 }
206
207 _Nds.Add(tmpNd);
208 InitTree(tmpNd.ChildNodes, tmpNd.Value);
209 }
210 }
211 }
212}
213