关于TreeNode.Expanded 属性

来源:互联网 发布:卖家怎么在淘宝客推广 编辑:程序博客网 时间:2024/05/01 06:06

获取或设置一个值,该值指示是否展开节点。

命名空间:  System.Web.UI.WebControls
程序集:  System.Web(在 System.Web.dll 中)

属性值

类型:System.Nullable<(Of<(Boolean>)>)

如果已展开节点,则为 true;如果尚未展开节点,则为 falsenullNothingnullptrnull 引用(在 Visual Basic 中为Nothing

备注

使用 Expanded 属性指定或确定节点的展开状态。

通过分别调用 Expand 和 Collapse 方法,可以展开和折叠节点。还可以通过分别调用 ExpandAll 和 CollapseAll 方法,展开和折叠节点及其所有子节点。

由于 Expanded 属性为三态属性,下面的 C# 代码段会导致编译错误:

 “复制”图像复制代码
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){if (TreeView1.Nodes[0].Expanded){// some work here }}

由于 VB.Net 隐式将 Boolean 值强制转换为 NullableBoolean,C# 并不这样,因此,最佳做法是显式检查该属性的状态。例如,Visual Basic 和 C# 中的如下代码示例显式测试Expanded 属性的值。

下面的 Visual Basic 代码示例显式测试 Expanded 属性的值。此示例测试 Expanded 属性是否已设置为 TrueNothingFalse 会贯穿 If 语句。

 “复制”图像复制代码
If TreeView1.Nodes(0).Expanded = True Then 'some work hereEnd IF

此 C# 代码示例显式测试 Expanded 属性的值。此示例测试 Expanded 属性是否已设置为 TrueNullFalse 会贯穿 If 语句。

 “复制”图像复制代码
if( TreeView1.Nodes[0].Expanded == true ) { //some work here}
TopicLocation如何:添加或删除 TreeView 节点元素在 Visual Studio 中生成 ASP .NET Web 应用程序

“折叠”图像示例

下面的代码示例演示如何使用 Expanded 属性以编程方式展开节点。它将深度为 1 的所有节点初始化为展开状态。请注意,根节点展开时,其子节点已经展开。若要此示例正确运行,您必须将下面的示例 XML 数据复制到名为 Book.xml 的文件中。

Visual Basic “复制”图像复制代码
<%@ 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 Data_Bound(ByVal sender As Object, ByVal e As TreeNodeEventArgs)        ' Determine the depth of a node as it is bound to data.        ' If the depth is 1, expand the node.        If e.Node.Depth = 1 Then            e.Node.Expanded = True        Else            e.Node.Expanded = False        End If    End Sub</script><html xmlns="http://www.w3.org/1999/xhtml" >  <head runat="server">    <title>TreeNode Expanded Example</title></head><body>    <form id="form1" runat="server">      <h3>TreeNode Expanded Example</h3>      <asp:TreeView id="BookTreeView"         DataSourceID="BookXmlDataSource"        OnTreeNodeDataBound="Data_Bound"        runat="server">        <DataBindings>          <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>          <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>          <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>        </DataBindings>      </asp:TreeView>      <asp:XmlDataSource id="BookXmlDataSource"          DataFile="Book.xml"        runat="server">      </asp:XmlDataSource>    </form>  </body></html>
C# “复制”图像复制代码
<%@ 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 Data_Bound(Object sender, TreeNodeEventArgs e)  {    // Determine the depth of a node as it is bound to data.    // If the depth is 1, expand the node.    if(e.Node.Depth == 1)    {      e.Node.Expanded = true;    }    else    {      e.Node.Expanded = false;    }  }</script><html xmlns="http://www.w3.org/1999/xhtml" >  <head runat="server">    <title>TreeNode Expanded Example</title></head><body>    <form id="form1" runat="server">      <h3>TreeNode Expanded Example</h3>      <asp:TreeView id="BookTreeView"         DataSourceID="BookXmlDataSource"        OnTreeNodeDataBound="Data_Bound"        runat="server">        <DataBindings>          <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>          <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>          <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>        </DataBindings>      </asp:TreeView>      <asp:XmlDataSource id="BookXmlDataSource"          DataFile="Book.xml"        runat="server">      </asp:XmlDataSource>    </form>  </body></html>

下面的代码是前一示例使用的示例 XML 数据。

 “复制”图像复制代码
<Book Title="Book Title">    <Chapter Heading="Chapter 1">        <Section Heading="Section 1">        </Section>        <Section Heading="Section 2">        </Section>    </Chapter>    <Chapter Heading="Chapter 2">        <Section Heading="Section 1">        </Section>    </Chapter></Book>

 

                                                                                                                                                                                  摘自:MSDN
原创粉丝点击