asp.net TreeView控件的用法

本文介绍了ASP.NET中的TreeView控件,包括其基本属性如ImageSet、NodeIndent和ShowLines的设置,以及事件处理如OnSelectedNodeChanged。示例代码展示了如何在页面中创建并配置TreeView控件,同时提供了选中节点改变时的事件响应方法。

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

html

 

  1.   <asp:Panel id="panel1" runat="server" Height="300" ScrollBars="Auto">
  2.                                     <asp:TreeView ID="PART_VIEW" runat="server" ImageSet="Simple" NodeIndent="10" ShowLines="True" AutoGenerateDataBindings="False" ExpandDepth="1" OnSelectedNodeChanged="PART_VIEW_SelectedNodeChanged">
  3.                                         <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
  4.                                         <SelectedNodeStyle Font-Underline="True" ForeColor="#DD5555" HorizontalPadding="0px" VerticalPadding="0px" />
  5.                                         <HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" />
  6.                                         <ParentNodeStyle Font-Bold="False" />
  7.                                     </asp:TreeView>
  8.                                 </asp:Panel>

C# Code

 

  1. public class TreeView_Class :TreeView
  2. {
  3.        private DataSet ds = null;
  4.         /// <summary>
  5.         /// 
  6.         /// </summary>
  7.         /// 
  8.         public TreeView_Class(): base()
  9.         {
  10.         }
  11.         /// <summary>
  12.         /// 
  13.         /// </summary>
  14.         /// <param name="ds"></param>
  15.         public TreeView_Class(DataSet ds)
  16.         {
  17.             this.ds = ds;
  18.         }
  19.         public void SetDataSet(DataSet ds)
  20.         {
  21.             this.ds = ds;
  22.         }
  23.         /// <summary>
  24.         /// 建樹不帶參數
  25.         /// </summary>
  26.         public TreeNode BuildTree()
  27.         {
  28.             //if (ds == null)
  29.             //{
  30.             //    return "";
  31.             //}
  32.             this.Nodes.Clear();
  33.             TreeNode root = new TreeNode("物料類別");
  34.             CreatTreeNodes newNode = null;
  35.             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  36.             {
  37.                 newNode = new CreatTreeNodes(ds.Tables[0].Rows[i][1].ToString());
  38.                 newNode.Value = ds.Tables[0].Rows[i][0].ToString();
  39.                 // newNode.FunctionCode = ds.Tables[0].Rows[i][3].ToString();
  40.                 if (ds.Tables[0].Rows[i]["cata_pid"].ToString() == "0")
  41.                 {
  42.                     root.ChildNodes.Add(newNode);
  43.                 }
  44.                 else
  45.                 {
  46.                     AddChildNodes(root, newNode, ds.Tables[0].Rows[i]["cata_pid"].ToString());
  47.                 }
  48.             }
  49.             root.Expand();
  50.             return root;
  51.         }
  52.         /// <summary>
  53.         /// 
  54.         /// </summary>
  55.         /// <param name="root">父節點</param>
  56.         /// <param name="newnode">節點</param>
  57.         /// <param name="prentID">節點的序列號</param>
  58.         public void AddChildNodes(TreeNode root, TreeNode newnode, string prentID)
  59.         {
  60.             if (root.Value!= null && prentID.Equals(root.Value.ToString()))
  61.             {
  62.                 root.ChildNodes.Add(newnode);
  63.             }
  64.             else
  65.             {
  66.                 for (int i = 0; i < root.ChildNodes.Count; i++)
  67.                 {
  68.                     if (prentID == root.ChildNodes[i].Value.ToString())
  69.                     {
  70.                         root.ChildNodes[i].ChildNodes.Add(newnode);
  71.                     }
  72.                     else
  73.                     {
  74.                         AddChildNodes(root.ChildNodes[i], newnode, prentID);
  75.                     }
  76.                     //root.Expand();
  77.                 }
  78.             }
  79.         }
  80.         /// <summary>
  81.         /// 建樹(包含權限的字段)
  82.         /// </summary>
  83.         /// <param name="ch">字符要顯示的標題</param>
  84.         /// <param name="one">要顯示給客戶看的字段(指數據庫的字段)</param>
  85.         /// <param name="two">權限的字段(指數據庫的字段)</param>
  86.         public void BuildTree(string ch, string one, string two)
  87.         {
  88.             this.Nodes.Clear();
  89.             TreeNode root = new TreeNode(ch);
  90.             CreatTreeNodes newnode = null;
  91.             this.Nodes.Add(root);
  92.             int j = ds.Tables[0].Rows.Count;
  93.             for (int i = 0; i < j; i++)
  94.             {
  95.                 newnode = new CreatTreeNodes(ds.Tables[0].Rows[i][one].ToString());
  96.                 newnode.Value = ds.Tables[0].Rows[i][0].ToString();
  97.                 if (newnode.FunctionCode != null)
  98.                 {
  99.                     newnode.FunctionCode = ds.Tables[0].Rows[i][two].ToString();
  100.                 }
  101.                 if (ds.Tables[0].Rows[i][two].ToString() == null || ds.Tables[0].Rows[i][two].ToString() == "")
  102.                 {
  103.                     root.ChildNodes.Add(newnode);
  104.                 }
  105.                 else
  106.                 {
  107.                     AddChildNodes(root, newnode, ds.Tables[0].Rows[i][two].ToString());
  108.                 }
  109.                 root.Expand();
  110.             }
  111.         }
  112.         //
  113.         public class CreatTreeNodes : TreeNode
  114.         {
  115.             private object functionCode;
  116.             //凳婖滲杅參趼睫珆尨堤懂
  117.             //彆羶衄參趼睫蛌徹饒繫攷憩珆尨諾
  118.             public CreatTreeNodes(string text)
  119.                 : base(text)
  120.             {
  121.             }
  122.             //權限 
  123.             public object FunctionCode
  124.             {
  125.                 get { return functionCode; }
  126.                 set { functionCode = value; }
  127.             }
  128.             //蘇凳婖源楊
  129.             //彆狟醱羶衄赽誹萸憩珆尨諾
  130.             public CreatTreeNodes()
  131.                 : base()
  132.             {
  133.                 this.functionCode = null;
  134.             }
  135.         }
  136. }

 

aspx.cs

  1.  private void bind()
  2.     {
  3.         DataSet ds=new DataSet();
  4.         sql ="SELECT CATA_ID,CATA_NAME,CATA_PID FROM BASE_CATA";
  5.         ds = return_PartMater(sql); --方法返回一个DataSet
  6.         TreeNode node=new TreeNode();
  7.         TreeView_Class treeview = new TreeView_Class(ds);
  8.         this.PART_VIEW.Nodes.Add(treeview.BuildTree());
  9.         --调用上面写的一个TreeView类
  10.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值