TreeView.SelectedNodeStyle Property

Definition

Gets the TreeNodeStyle object that controls the appearance of the selected node in the TreeView control.

public:
 property System::Web::UI::WebControls::TreeNodeStyle ^ SelectedNodeStyle { System::Web::UI::WebControls::TreeNodeStyle ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.TreeNodeStyle SelectedNodeStyle { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.SelectedNodeStyle : System.Web.UI.WebControls.TreeNodeStyle
Public ReadOnly Property SelectedNodeStyle As TreeNodeStyle

Property Value

A TreeNodeStyle that represents the style of the selected node in the TreeView control. The default is null, which indicates that the SelectedNodeStyle property is not set.

Attributes

Examples

The following code example demonstrates how to use the SelectedNodeStyle property to control the appearance of the selected node in the 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>

Remarks

Use the SelectedNodeStyle property to control the appearance of the selected node in the TreeView control. This property is read-only; however, you can set the properties of the TreeNodeStyle object it returns. The properties can be set declaratively in the form Property-Subproperty, where Subproperty is a property of the TreeNodeStyle object (for example, SelectedNodeStyle-ForeColor). The properties can also be set programmatically in the form Property.Subproperty (for example, SelectedNodeStyle.ForeColor). Common settings usually include a custom background color, foreground color, font properties, and node spacing. Style properties are applied in the following order of precedence:

  1. NodeStyle.

  2. RootNodeStyle, ParentNodeStyle, or LeafNodeStyle, depending on the node type. If the LevelStyles collection is defined, it is applied at this time, overriding the other node style properties.

  3. SelectedNodeStyle.

  4. HoverNodeStyle.

Applies to

See also