TreeNode 构造函数 (String)的C#例子

本文介绍了一种使用C#在ASP.NET中动态创建并填充TreeView控件的方法。通过示例代码展示了如何创建根节点和子节点,并利用不同构造函数设置节点的文本、值、图片链接及导航地址等属性。

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


 下面的代码示例演示如何使用此构造函数将节点动态添加到 TreeView 控件。

 1None.gif<%@ Page Language="C#" %>   
 2None.gif  
 3None.gif<SCRIPT runat="server">  
 4None.gif  
 5None.gif  void Page_Init(Object sender, EventArgs e)  
 6ExpandedBlockStart.gifContractedBlock.gif  {  
 7InBlock.gif    
 8InBlock.gif    if(!IsPostBack)  
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    {  
10InBlock.gif  
11InBlock.gif      // Add the first tree to the TreeView control.  
12InBlock.gif      CreateTree("Section 1");  
13InBlock.gif  
14InBlock.gif      // Add the second tree to the TreeView control.  
15InBlock.gif      CreateTree("Section 2");  
16InBlock.gif      
17ExpandedSubBlockEnd.gif    }
  
18InBlock.gif  
19ExpandedBlockEnd.gif  }
  
20None.gif  
21None.gif  void CreateTree(String NodeText)  
22ExpandedBlockStart.gifContractedBlock.gif  dot.gif{  
23InBlock.gif  
24InBlock.gif    // Create the root node using the default constructor.  
25InBlock.gif    TreeNode root = new TreeNode();  
26InBlock.gif    root.Text = NodeText;  
27InBlock.gif  
28InBlock.gif    // Use the ChildNodes property of the root TreeNode to add child nodes.  
29InBlock.gif    // Create the node using the constructor that takes the text parameter.  
30InBlock.gif    root.ChildNodes.Add(new TreeNode("Topic 1"));  
31InBlock.gif  
32InBlock.gif    // Create the node using the constructor that takes the text and value parameters.  
33InBlock.gif    root.ChildNodes.Add(new TreeNode("Topic 2""Value 2"));  
34InBlock.gif  
35InBlock.gif    // Create the node using the constructor that takes the text, value,   
36InBlock.gif    // and imageUrl parameters.  
37InBlock.gif    root.ChildNodes.Add(new TreeNode("Topic 3""Value 3""Image1.jpg"));  
38InBlock.gif  
39InBlock.gif    // Create the node using the constructor that takes the text, value,   
40InBlock.gif    // imageUrl, navigateUrl, and target parameters.  
41InBlock.gif    root.ChildNodes.Add(new TreeNode("Topic 4""Value 4""Image1.jpg""http://www.microsoft.com""_blank"));  
42InBlock.gif  
43InBlock.gif    // Add the root node to the Nodes collection of the TreeView control.  
44InBlock.gif    DynamicTreeView.Nodes.Add(root);  
45InBlock.gif  
46ExpandedBlockEnd.gif  }
  
47None.gif  
48None.gif</SCRIPT>   
49None.gif  
50None.gif<HTML>   
51None.gif     
52None.gif    <FORM runat="server">   
53None.gif       
54None.gif      <H3>TreeNode Constructor Example</H3>   
55None.gif         
56None.gif      <ASP:TREEVIEW id=DynamicTreeView runat="server" InitialExpandDepth="2" EnableClientScript="false">   
57None.gif           
58None.gif      </ASP:TREEVIEW>   
59None.gif  
60None.gif    </FORM>   
61None.gif    
62None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值