[原创]asp.net 2.0下的自定义树(myTreeView)

本文介绍了一个自定义的TreeView组件myTreeView,该组件用于ASP.NET Web应用程序中,通过传入数据集和其他配置项来生成带有超链接的树形导航菜单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己在项目里用的类,大家看一下~~
ContractedBlock.gifExpandedBlockStart.gifCode
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Data;
  5using System.Web.UI.WebControls;
  6
  7ExpandedBlockStart.gifContractedBlock.gif/**//*-----------------------------------------------------------
  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
 26ExpandedBlockStart.gifContractedBlock.gif{
 27ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 28    /// myTreeView 的摘要说明。
 29    /// </summary>

 30    public class myTreeView
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 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
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 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()
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 65        }

 66
 67ContractedSubBlock.gifExpandedSubBlockStart.gif        参数#region 参数
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 69        /// ID字段
 70        /// </summary>

 71        public string IDField
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _IDField = value; }
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _IDField; }
 75        }

 76
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 78        /// Name字段
 79        /// </summary>

 80        public string NameField
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _NameField = value; }
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _NameField; }
 84        }

 85
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 87        /// 父级字段
 88        /// </summary>

 89        public string ParentField
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _ParentField = value; }
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _ParentField; }
 93        }

 94
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 96        /// 根节点号
 97        /// </summary>

 98        public string RootID
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        {
100ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _RootID = value; }
101ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _RootID; }
102        }

103
104ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
105        /// 超链接
106        /// </summary>

107        public string NavigateUrl
108ExpandedSubBlockStart.gifContractedSubBlock.gif        {
109ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _NavigateUrl = value; }
110ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _NavigateUrl; }
111        }

112
113ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
114        /// 超链接参数字段
115        /// </summary>

116        public string NavigateArg
117ExpandedSubBlockStart.gifContractedSubBlock.gif        {
118ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _NavigateArg = value; }
119ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _NavigateArg; }
120        }

121
122ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
123        /// 页面名的字段
124        /// </summary>

125        public string PageNameField
126ExpandedSubBlockStart.gifContractedSubBlock.gif        {
127ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _PageNameField = value; }
128ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _PageNameField; }
129        }

130
131
132ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
133        /// 超链接目标
134        /// </summary>

135        public int ExpandLevel
136ExpandedSubBlockStart.gifContractedSubBlock.gif        {
137ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _ExpandLevel = value; }
138ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _ExpandLevel; }
139        }

140
141
142ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
143        /// 超链接目标
144        /// </summary>

145        public string Target
146ExpandedSubBlockStart.gifContractedSubBlock.gif        {
147ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _Target = value; }
148ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _Target; }
149        }

150
151ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
152        /// 超链接目标
153        /// </summary>

154        public DataSet Dst
155ExpandedSubBlockStart.gifContractedSubBlock.gif        {
156ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _Dst = value; }
157ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _Dst; }
158        }

159        #endregion

160
161        public void BindTree(System.Web.UI.WebControls.TreeView _myTree)
162ExpandedSubBlockStart.gifContractedSubBlock.gif        {
163            _myTree.Nodes.Clear();
164            _myTree.MaxDataBindDepth = ExpandLevel;
165
166            InitTree(_myTree.Nodes, RootID);
167        }

168
169        private void InitTree(TreeNodeCollection _Nds, string _parentID)
170ExpandedSubBlockStart.gifContractedSubBlock.gif        {
171            TreeNode tmpNd;
172            DataRow[] rows = Dst.Tables[0].Select(ParentField + "=" + _parentID);
173
174            foreach (DataRow row in rows)
175ExpandedSubBlockStart.gifContractedSubBlock.gif            {
176                tmpNd = new TreeNode();
177                tmpNd.Value = row[IDField].ToString();
178                tmpNd.Text = row[NameField].ToString();
179
180                if (PageNameField != "" && PageNameField != null)
181ExpandedSubBlockStart.gifContractedSubBlock.gif                {
182                    if (row[PageNameField].ToString() != "")
183                        tmpNd.NavigateUrl =  ReturnPlus.GetAppPath() + row[PageNameField].ToString();
184
185                    if (NavigateArg != "")
186ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
187                        if (row[NavigateArg].ToString() != null)
188                            tmpNd.NavigateUrl = String.Format(row[PageNameField].ToString(), row[NavigateArg]);
189                    }

190                }

191                else
192ExpandedSubBlockStart.gifContractedSubBlock.gif                {
193                    tmpNd.NavigateUrl = String.Format(NavigateUrl, row[NavigateArg]);
194                }

195
196                if (tmpNd.NavigateUrl == "" || tmpNd.NavigateUrl == null)
197ExpandedSubBlockStart.gifContractedSubBlock.gif                {
198                    //tmpNd.SelectAction = TreeNodeSelectAction.SelectExpand;                
199                    //tmpNd.NavigateUrl = "javascript:TreeView_ToggleNode(myTree_Data," + tmpNd. +",myTreen3,'l',myTreen3Nodes)"
200                    //tmpNd.NavigateUrl = "#";
201                }

202                else
203ExpandedSubBlockStart.gifContractedSubBlock.gif                {
204                    tmpNd.Target = Target;
205                }

206
207                _Nds.Add(tmpNd);
208                InitTree(tmpNd.ChildNodes, tmpNd.Value);
209            }

210        }

211    }

212}

213

转载于:https://www.cnblogs.com/pcsky/archive/2009/10/15/1584124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值