学习使用TreeView 控件的TreeNode 对象来获取选中了CheckBox的节点!
虽说TreeView 有个CheckBox无法回发和自动关联的BUG,但相比ASP.NET1.0里的树形控件,多出了个TreeNode 对象的集合,此集合可以使我们循环访问树中所有选中了复选框的节点,很简单方便的办法!
如果复选框显示在 TreeView 控件中(通过将 ShowCheckBoxes 属性设置为 TreeNodeType.None 以外的值),可使用 CheckedNodes 属性确定哪些节点显示选中了的复选框。
需要注意的是,只有当 TreeView 控件的复选框在两次向服务器发送之间更改状态时,才会引发 TreeNodeCheckChanged 事件。
1 <%@ Page Language="C#" %>
2
3 <script runat="server">
4
5 void Button_Click(Object sender, EventArgs e)
6 {
7
8 if(LinksTreeView.CheckedNodes.Count > 0)
9 {
10 Message.Text = "You selected: <br><br>";
11 foreach (TreeNode node in LinksTreeView.CheckedNodes)
12 {
13 Message.Text += node.Text + "<br>";
14 }
15 }
16 else
17 {
18 Message.Text = "No items selected.";
19 }
20 }
21
22 </script>
2
3 <script runat="server">
4
5 void Button_Click(Object sender, EventArgs e)
6 {
7
8 if(LinksTreeView.CheckedNodes.Count > 0)
9 {
10 Message.Text = "You selected: <br><br>";
11 foreach (TreeNode node in LinksTreeView.CheckedNodes)
12 {
13 Message.Text += node.Text + "<br>";
14 }
15 }
16 else
17 {
18 Message.Text = "No items selected.";
19 }
20 }
21
22 </script>
1 <html>
2 <body>
3 <form runat="server">
4
5 <h3>TreeView ShowCheckBoxes Example</h3>
6
7 <asp:TreeView id="LinksTreeView"
8 Font-Name= "Arial"
9 ForeColor="Blue"
10 InitialExpandDepth="2"
11 ShowCheckBoxes="Parent,Leaf"
12 runat="server">
13
14 <LevelStyles>
15
16 <asp:TreeNodeStyle ChildNodesPadding="10"
17 Font-Bold="true"
18 Font-Size="12pt"
19 ForeColor="DarkGreen"/>
20 <asp:TreeNodeStyle ChildNodesPadding="5"
21 Font-Bold="true"
22 Font-Size="10pt"/>
23 <asp:TreeNodeStyle ChildNodesPadding="5"
24 Font-UnderLine="true"
25 Font-Size="10pt"/>
26 <asp:TreeNodeStyle ChildNodesPadding="10"
27 Font-Size="8pt"/>
28
29 </LevelStyles>
30
31 <Nodes>
32
33 <asp:TreeNode Text="Table of Contents"
34 SelectAction="None">
35
36 <asp:TreeNode Text="Chapter One">
37
38 <asp:TreeNode Text="Section 1.0">
39
40 <asp:TreeNode Text="Topic 1.0.1"/>
41 <asp:TreeNode Text="Topic 1.0.2"/>
42 <asp:TreeNode Text="Topic 1.0.3"/>
43
44 </asp:TreeNode>
45
46 <asp:TreeNode Text="Section 1.1">
47
48 <asp:TreeNode Text="Topic 1.1.1"/>
49 <asp:TreeNode Text="Topic 1.1.2"/>
50 <asp:TreeNode Text="Topic 1.1.3"/>
51 <asp:TreeNode Text="Topic 1.1.4"/>
52
53 </asp:TreeNode>
54
55 </asp:TreeNode>
56
57 <asp:TreeNode Text="Chapter Two">
58
59 <asp:TreeNode Text="Section 2.0">
60
61 <asp:TreeNode Text="Topic 2.0.1"/>
62 <asp:TreeNode Text="Topic 2.0.2"/>
63
64 </asp:TreeNode>
65
66 </asp:TreeNode>
67
68 </asp:TreeNode>
69 <asp:TreeNode Text="Appendix A" />
70 <asp:TreeNode Text="Appendix B" />
71 <asp:TreeNode Text="Appendix C" />
72
73 </Nodes>
74
75 </asp:TreeView>
76
77 <br><br>
78
79 <asp:Button id="Submit"
80 Text="Select Items"
81 OnClick="Button_Click"
82 runat="server"/>
83
84 <br><br>
85
86 <asp:Label id="Message"
87 runat="server"/>
88
89 </form>
90 </body>
91 </html>
2 <body>
3 <form runat="server">
4
5 <h3>TreeView ShowCheckBoxes Example</h3>
6
7 <asp:TreeView id="LinksTreeView"
8 Font-Name= "Arial"
9 ForeColor="Blue"
10 InitialExpandDepth="2"
11 ShowCheckBoxes="Parent,Leaf"
12 runat="server">
13
14 <LevelStyles>
15
16 <asp:TreeNodeStyle ChildNodesPadding="10"
17 Font-Bold="true"
18 Font-Size="12pt"
19 ForeColor="DarkGreen"/>
20 <asp:TreeNodeStyle ChildNodesPadding="5"
21 Font-Bold="true"
22 Font-Size="10pt"/>
23 <asp:TreeNodeStyle ChildNodesPadding="5"
24 Font-UnderLine="true"
25 Font-Size="10pt"/>
26 <asp:TreeNodeStyle ChildNodesPadding="10"
27 Font-Size="8pt"/>
28
29 </LevelStyles>
30
31 <Nodes>
32
33 <asp:TreeNode Text="Table of Contents"
34 SelectAction="None">
35
36 <asp:TreeNode Text="Chapter One">
37
38 <asp:TreeNode Text="Section 1.0">
39
40 <asp:TreeNode Text="Topic 1.0.1"/>
41 <asp:TreeNode Text="Topic 1.0.2"/>
42 <asp:TreeNode Text="Topic 1.0.3"/>
43
44 </asp:TreeNode>
45
46 <asp:TreeNode Text="Section 1.1">
47
48 <asp:TreeNode Text="Topic 1.1.1"/>
49 <asp:TreeNode Text="Topic 1.1.2"/>
50 <asp:TreeNode Text="Topic 1.1.3"/>
51 <asp:TreeNode Text="Topic 1.1.4"/>
52
53 </asp:TreeNode>
54
55 </asp:TreeNode>
56
57 <asp:TreeNode Text="Chapter Two">
58
59 <asp:TreeNode Text="Section 2.0">
60
61 <asp:TreeNode Text="Topic 2.0.1"/>
62 <asp:TreeNode Text="Topic 2.0.2"/>
63
64 </asp:TreeNode>
65
66 </asp:TreeNode>
67
68 </asp:TreeNode>
69 <asp:TreeNode Text="Appendix A" />
70 <asp:TreeNode Text="Appendix B" />
71 <asp:TreeNode Text="Appendix C" />
72
73 </Nodes>
74
75 </asp:TreeView>
76
77 <br><br>
78
79 <asp:Button id="Submit"
80 Text="Select Items"
81 OnClick="Button_Click"
82 runat="server"/>
83
84 <br><br>
85
86 <asp:Label id="Message"
87 runat="server"/>
88
89 </form>
90 </body>
91 </html>