TreeView.CheckedNodes 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public:
property System::Web::UI::WebControls::TreeNodeCollection ^ CheckedNodes { System::Web::UI::WebControls::TreeNodeCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.TreeNodeCollection CheckedNodes { get; }
[<System.ComponentModel.Browsable(false)>]
member this.CheckedNodes : System.Web.UI.WebControls.TreeNodeCollection
Public ReadOnly Property CheckedNodes As TreeNodeCollection
屬性值
TreeNodeCollection,包含顯示已選取核取方塊之 TreeView 中的節點。
- 屬性
範例
下列程式碼範例示範如何使用 CheckedNodes 屬性來判斷控制項中 TreeView 哪些節點有選取的核取方塊。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Button_Click(Object sender, EventArgs e)
{
if(LinksTreeView.CheckedNodes.Count > 0)
{
// Clear the message label.
Message.Text = "You selected: <br /><br />";
// Iterate through the CheckedNodes collection and display the selected nodes.
foreach (TreeNode node in LinksTreeView.CheckedNodes)
{
Message.Text += node.Text + "<br />";
}
}
else
{
Message.Text = "No items selected.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView ShowCheckBoxes Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView ShowCheckBoxes Example</h3>
<!-- Set the ShowCheckBoxes property declaratively. -->
<!-- Because the ShowCheckBoxes property uses a flag -->
<!-- enumeration, you can combine multiple values by -->
<!-- using the bitwise OR operator. In declarative -->
<!-- syntax, this is done using a comma separated -->
<!-- list. -->
<asp:TreeView id="LinksTreeView"
Font-Names= "Arial"
ForeColor="Blue"
ExpandDepth="2"
ShowCheckBoxes="Parent,Leaf"
runat="server">
<LevelStyles>
<asp:TreeNodeStyle ChildNodesPadding="10"
Font-Bold="true"
Font-Size="12pt"
ForeColor="DarkGreen"/>
<asp:TreeNodeStyle ChildNodesPadding="5"
Font-Bold="true"
Font-Size="10pt"/>
<asp:TreeNodeStyle ChildNodesPadding="5"
Font-UnderLine="true"
Font-Size="10pt"/>
<asp:TreeNodeStyle ChildNodesPadding="10"
Font-Size="8pt"/>
</LevelStyles>
<Nodes>
<asp:TreeNode Text="Table of Contents"
SelectAction="None">
<asp:TreeNode Text="Chapter One">
<asp:TreeNode Text="Section 1.0">
<asp:TreeNode Text="Topic 1.0.1"/>
<asp:TreeNode Text="Topic 1.0.2"/>
<asp:TreeNode Text="Topic 1.0.3"/>
</asp:TreeNode>
<asp:TreeNode Text="Section 1.1">
<asp:TreeNode Text="Topic 1.1.1"/>
<asp:TreeNode Text="Topic 1.1.2"/>
<asp:TreeNode Text="Topic 1.1.3"/>
<asp:TreeNode Text="Topic 1.1.4"/>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Chapter Two">
<asp:TreeNode Text="Section 2.0">
<asp:TreeNode Text="Topic 2.0.1"/>
<asp:TreeNode Text="Topic 2.0.2"/>
</asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Appendix A" />
<asp:TreeNode Text="Appendix B" />
<asp:TreeNode Text="Appendix C" />
</Nodes>
</asp:TreeView>
<br /><br />
<asp:Button id="Submit"
Text="Select Items"
OnClick="Button_Click"
runat="server"/>
<br /><br />
<asp:Label id="Message"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
If LinksTreeView.CheckedNodes.Count > 0 Then
' Clear the message label.
Message.Text = "You selected: <br /><br />"
' Iterate through the CheckedNodes collection and display the selected nodes.
Dim node As TreeNode
For Each node In LinksTreeView.CheckedNodes
Message.Text &= node.Text & "<br />"
Next
Else
Message.Text = "No items selected."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView ShowCheckBoxes Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView ShowCheckBoxes Example</h3>
<!-- Set the ShowCheckBoxes property declaratively. -->
<!-- Because the ShowCheckBoxes property uses a flag -->
<!-- enumeration, you can combine multiple values by -->
<!-- using the bitwise OR operator. In declarative -->
<!-- syntax, this is done using a comma separated -->
<!-- list. -->
<asp:TreeView id="LinksTreeView"
Font-Names= "Arial"
ForeColor="Blue"
ExpandDepth="2"
ShowCheckBoxes="Parent,Leaf"
runat="server">
<LevelStyles>
<asp:TreeNodeStyle ChildNodesPadding="10"
Font-Bold="true"
Font-Size="12pt"
ForeColor="DarkGreen"/>
<asp:TreeNodeStyle ChildNodesPadding="5"
Font-Bold="true"
Font-Size="10pt"/>
<asp:TreeNodeStyle ChildNodesPadding="5"
Font-UnderLine="true"
Font-Size="10pt"/>
<asp:TreeNodeStyle ChildNodesPadding="10"
Font-Size="8pt"/>
</LevelStyles>
<Nodes>
<asp:TreeNode Text="Table of Contents"
SelectAction="None">
<asp:TreeNode Text="Chapter One">
<asp:TreeNode Text="Section 1.0">
<asp:TreeNode Text="Topic 1.0.1"/>
<asp:TreeNode Text="Topic 1.0.2"/>
<asp:TreeNode Text="Topic 1.0.3"/>
</asp:TreeNode>
<asp:TreeNode Text="Section 1.1">
<asp:TreeNode Text="Topic 1.1.1"/>
<asp:TreeNode Text="Topic 1.1.2"/>
<asp:TreeNode Text="Topic 1.1.3"/>
<asp:TreeNode Text="Topic 1.1.4"/>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Chapter Two">
<asp:TreeNode Text="Section 2.0">
<asp:TreeNode Text="Topic 2.0.1"/>
<asp:TreeNode Text="Topic 2.0.2"/>
</asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Appendix A" />
<asp:TreeNode Text="Appendix B" />
<asp:TreeNode Text="Appendix C" />
</Nodes>
</asp:TreeView>
<br /><br />
<asp:Button id="Submit"
Text="Select Items"
OnClick="Button_Click"
runat="server"/>
<br /><br />
<asp:Label id="Message"
runat="server"/>
</form>
</body>
</html>
備註
當核取方塊顯示在 TreeView 控制項 (時,將 屬性設定 ShowCheckBoxes 為) 以外的 TreeNodeType.None
值時,請使用 CheckedNodes 屬性來判斷哪些節點會顯示選取的核取方塊。 此集合通常用於逐一查看樹狀結構中已選取核取方塊的所有節點。
TreeNodeCheckChanged當控制項的 TreeView 核取方塊在張貼到伺服器之間變更狀態時,就會引發事件。 這可讓您提供事件處理方法,以在發生此事件時執行自訂常式,例如更新資料庫或顯示的內容。
注意
CheckedNodes集合會使用樹狀結構的深度優先周遊來填入:每個父節點會在填入下一個父節點之前,向下處理到其子節點。