TreeView.TreeNodeCheckChanged 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当 TreeView 控件中的复选框在向服务器的两次发送过程之间状态有所更改时发生。
public:
event System::Web::UI::WebControls::TreeNodeEventHandler ^ TreeNodeCheckChanged;
public event System.Web.UI.WebControls.TreeNodeEventHandler TreeNodeCheckChanged;
member this.TreeNodeCheckChanged : System.Web.UI.WebControls.TreeNodeEventHandler
Public Custom Event TreeNodeCheckChanged As TreeNodeEventHandler
事件类型
示例
下面的代码示例演示了当控件的检查框TreeView在发帖到服务器之间更改状态时,如何使用 TreeNodeCheckChanged 事件更新Label控件。
<%@ 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 LinksTreeView_CheckChanged(Object sender, TreeNodeEventArgs 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 CheckChanged Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView CheckChanged 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"
ForeColor="Blue"
ExpandDepth="2"
ShowCheckBoxes="Parent,Leaf"
OnTreeNodeCheckChanged="LinksTreeView_CheckChanged"
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"
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 LinksTreeView_CheckChanged(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
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 CheckChanged Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView CheckChanged 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"
ForeColor="Blue"
ExpandDepth="2"
ShowCheckBoxes="Parent,Leaf"
OnTreeNodeCheckChanged="LinksTreeView_CheckChanged"
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"
runat="server"/>
<br /><br />
<asp:Label id="Message"
runat="server"/>
</form>
</body>
</html>
注解
当TreeNodeCheckChanged控件中的TreeView检查框在发帖到服务器之间更改状态时,将引发 该事件。 这允许你提供一个事件处理方法,该方法在发生此事件时执行自定义例程,例如更新数据库或显示的内容。
即使事件TreeNodeCheckChanged是在回发时触发的,更改检查框不会导致回发。
有关如何处理事件的详细信息,请参阅 处理和引发事件。