TreeNode 构造函数 (String)的C#例子
下面的代码示例演示如何使用此构造函数将节点动态添加到 TreeView 控件。
1
<%@ Page Language="C#" %>
2
3
<SCRIPT runat="server">
4
5
void Page_Init(Object sender, EventArgs e)
6
{
7
8
if(!IsPostBack)
9
{
10
11
// Add the first tree to the TreeView control.
12
CreateTree("Section 1");
13
14
// Add the second tree to the TreeView control.
15
CreateTree("Section 2");
16
17
}
18
19
}
20
21
void CreateTree(String NodeText)
22
{
23
24
// Create the root node using the default constructor.
25
TreeNode root = new TreeNode();
26
root.Text = NodeText;
27
28
// Use the ChildNodes property of the root TreeNode to add child nodes.
29
// Create the node using the constructor that takes the text parameter.
30
root.ChildNodes.Add(new TreeNode("Topic 1"));
31
32
// Create the node using the constructor that takes the text and value parameters.
33
root.ChildNodes.Add(new TreeNode("Topic 2", "Value 2"));
34
35
// Create the node using the constructor that takes the text, value,
36
// and imageUrl parameters.
37
root.ChildNodes.Add(new TreeNode("Topic 3", "Value 3", "Image1.jpg"));
38
39
// Create the node using the constructor that takes the text, value,
40
// imageUrl, navigateUrl, and target parameters.
41
root.ChildNodes.Add(new TreeNode("Topic 4", "Value 4", "Image1.jpg", "http://www.microsoft.com", "_blank"));
42
43
// Add the root node to the Nodes collection of the TreeView control.
44
DynamicTreeView.Nodes.Add(root);
45
46
}
47
48
</SCRIPT>
49
50
<HTML>
51
52
<FORM runat="server">
53
54
<H3>TreeNode Constructor Example</H3>
55
56
<ASP:TREEVIEW id=DynamicTreeView runat="server" InitialExpandDepth="2" EnableClientScript="false">
57
58
</ASP:TREEVIEW>
59
60
</FORM>
61
62
<%@ Page Language="C#" %> 2
3
<SCRIPT runat="server"> 4
5
void Page_Init(Object sender, EventArgs e) 6
{ 7
8
if(!IsPostBack) 9
{ 10
11
// Add the first tree to the TreeView control. 12
CreateTree("Section 1"); 13
14
// Add the second tree to the TreeView control. 15
CreateTree("Section 2"); 16
17
} 18
19
} 20
21
void CreateTree(String NodeText) 22
{ 23
24
// Create the root node using the default constructor. 25
TreeNode root = new TreeNode(); 26
root.Text = NodeText; 27
28
// Use the ChildNodes property of the root TreeNode to add child nodes. 29
// Create the node using the constructor that takes the text parameter. 30
root.ChildNodes.Add(new TreeNode("Topic 1")); 31
32
// Create the node using the constructor that takes the text and value parameters. 33
root.ChildNodes.Add(new TreeNode("Topic 2", "Value 2")); 34
35
// Create the node using the constructor that takes the text, value, 36
// and imageUrl parameters. 37
root.ChildNodes.Add(new TreeNode("Topic 3", "Value 3", "Image1.jpg")); 38
39
// Create the node using the constructor that takes the text, value, 40
// imageUrl, navigateUrl, and target parameters. 41
root.ChildNodes.Add(new TreeNode("Topic 4", "Value 4", "Image1.jpg", "http://www.microsoft.com", "_blank")); 42
43
// Add the root node to the Nodes collection of the TreeView control. 44
DynamicTreeView.Nodes.Add(root); 45
46
} 47
48
</SCRIPT> 49
50
<HTML> 51
52
<FORM runat="server"> 53
54
<H3>TreeNode Constructor Example</H3> 55
56
<ASP:TREEVIEW id=DynamicTreeView runat="server" InitialExpandDepth="2" EnableClientScript="false"> 57
58
</ASP:TREEVIEW> 59
60
</FORM> 61
62



浙公网安备 33010602011771号