TreeView.SelectedNode Propiedad

Definición

Obtiene un objeto TreeNode que representa el nodo seleccionado del control TreeView.

public:
 property System::Web::UI::WebControls::TreeNode ^ SelectedNode { System::Web::UI::WebControls::TreeNode ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.TreeNode SelectedNode { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedNode : System.Web.UI.WebControls.TreeNode
Public ReadOnly Property SelectedNode As TreeNode

Valor de propiedad

Objeto TreeNode que representa el nodo seleccionado en el control TreeView.

Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la SelectedNode propiedad para determinar el valor del nodo seleccionado en el TreeView control .


<%@ 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 Select_Change(Object sender, EventArgs e)
    {

        Message.Text = "You selected: " + LinksTreeView.SelectedNode.Text;

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">
    <title>TreeView SelectedNodeStyle Example</title>
</head>
<body>
        <form id="form1" runat="server">
        
            <h3>TreeView SelectedNodeStyle Example</h3>

            <asp:TreeView id="LinksTreeView"
                Font-Names= "Arial"
                ForeColor="Blue"
                SelectedNodeStyle-ForeColor="Green"
                SelectedNodeStyle-VerticalPadding="0"
                OnSelectedNodeChanged="Select_Change"   
                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: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 Select_Change(ByVal sender As Object, ByVal e As EventArgs)

        Message.Text = "You selected: " & LinksTreeView.SelectedNode.Text

    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">
    <title>TreeView SelectedNodeStyle Example</title>
</head>
<body>
        <form id="form1" runat="server">
        
            <h3>TreeView SelectedNodeStyle Example</h3>
            
            <asp:TreeView id="LinksTreeView"
                Font-Names= "Arial"
                ForeColor="Blue"
                SelectedNodeStyle-ForeColor="Green"
                SelectedNodeStyle-VerticalPadding="0"
                OnSelectedNodeChanged="Select_Change"   
                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:Label id="Message" runat="server"/>
            
        </form>
    </body>
</html>

Comentarios

El texto de un nodo del TreeView control puede estar en uno de los dos modos: modo de selección y modo de navegación. De forma predeterminada, un nodo está en modo de selección. Para colocar un nodo en modo de navegación, establezca la NavigateUrl propiedad del nodo en un valor distinto de una cadena vacía (""). Para colocar un nodo en modo de selección, establezca la NavigateUrl propiedad del nodo en una cadena vacía ("").

Cuando un nodo está en modo de selección, el usuario puede seleccionar un nodo haciendo clic en el texto del nodo. Utilice la SelectedNode propiedad para determinar qué nodo está seleccionado en el TreeView control .

Nota

No se puede seleccionar un nodo cuando el TreeView control muestra hipervínculos. Cuando se muestran hipervínculos, la SelectedNode propiedad siempre devuelve null.

Cuando el usuario selecciona un nodo diferente en el TreeView control haciendo clic en el texto del nuevo nodo, se genera el SelectedNodeChanged evento de forma predeterminada. Puede especificar un evento diferente estableciendo la SelectAction propiedad para la propiedad . En la siguiente tabla se enumeran las opciones disponibles.

Acción de selección Descripción
Expand Alterna el nodo entre expandido y contraído. Genera el TreeNodeExpanded evento o TreeNodeCollapsed , según corresponda.
None No provoca ningún evento cuando se selecciona un nodo.
Select Genera el evento SelectedNodeChanged cuando se selecciona un nodo.
SelectExpand Provoca los eventos SelectedNodeChanged y TreeNodeExpanded cuando se selecciona un nodo. Los nodos solo se expanden, nunca se contraen.

Nota

No HoverNodeStyle se representa para un nodo con su SelectAction propiedad establecida en TreeNodeSelectAction.None.

Se aplica a

Consulte también