locked
Selected Node Style is not highlighting in treeview. RRS feed

  • Question

  • User378096647 posted

    Hi all,

    I'm trying to high light the selected node and I find out that since the node I'm using are getting from a SQL server and I think that is maybe why is not showing correctly. But I'm not sure. Please review my previous post for more infomation.

    https://forums.asp.net/t/2163503.aspx?How+to+highlight+the+selected+treeview+node+

    Also, does this forum have a function to refresh unanswered post so will be on top again? Because a lot of unanswered post are at the last page and no one will read them, which means people will keep spam posting till they get their answers. 

    Tuesday, February 18, 2020 5:00 PM

All replies

  • User288213138 posted

    Hi noobcodervb,

    I'm trying to high light the selected node and I find out that since the node I'm using are getting from a SQL server and I think that is maybe why is not showing correctly. But I'm not sure. Please review my previous post for more infomation

    According to your description, i made demo for you as a reference. I also get the data from database.

    <asp:TreeView ID="TreeView1" runat="server">          
                <SelectedNodeStyle BackColor="Yellow"/>
            </asp:TreeView>
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not Me.IsPostBack Then
                Dim dt As DataTable = Me.GetData("SELECT Id, Name FROM VehicleTypes")
                Me.PopulateTreeView(dt, 0, Nothing)
            End If
        End Sub
    
        Private Sub PopulateTreeView(ByVal dtParent As DataTable, ByVal parentId As Integer, ByVal treeNode As TreeNode)
            For Each row As DataRow In dtParent.Rows
                Dim child As TreeNode = New TreeNode With {
                    .Text = row("Name").ToString(),
                    .Value = row("Id").ToString()
                }
    
                If parentId = 0 Then
                    TreeView1.Nodes.Add(child)
                    Dim dtChild As DataTable = Me.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " & child.Value)
                    PopulateTreeView(dtChild, Integer.Parse(child.Value), child)
                Else
                    treeNode.ChildNodes.Add(child)
                End If
            Next
        End Sub
    
        Private Function GetData(ByVal query As String) As DataTable
            Dim dt As DataTable = New DataTable()
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    
            Using con As SqlConnection = New SqlConnection(constr)
    
                Using cmd As SqlCommand = New SqlCommand(query)
    
                    Using sda As SqlDataAdapter = New SqlDataAdapter()
                        cmd.CommandType = CommandType.Text
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        sda.Fill(dt)
                    End Using
                End Using
    
                Return dt
            End Using
        End Function

    The result:

    Best regards,

    Sam

    Wednesday, February 19, 2020 4:53 AM
  • User378096647 posted

    Hi samwu,

    I don't see anything wrong with my code, can you help me take a quick look.

    ASPX code:

    <asp:TreeView ID="TreeView1" ExpandDepth="0" runat="server"
                    style="width:270px" ForeColor="Black"  HoverNodeStyle-BackColor ="LightBlue"  SelectedNodeStyle BackColor="LightBlue"
                    ShowExpandCollapse="true" PopulateOnDemand="true" NodeWrap="true"  
                   NodeStyle-CssClass="treeNode"
            RootNodeStyle-CssClass="rootNode"
            LeafNodeStyle-CssClass="leafNode" ViewStateMode="Enabled"
            OnTreeNodeCollapsed="TreeView1_TreeNodeExpandedCollapsed"
            OnTreeNodeExpanded="TreeView1_TreeNodeExpandedCollapsed"></asp:TreeView>

    Code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim Levels As String
            Levels = Request.QueryString("Level")
            If Not IsPostBack() Then GetTreeViewItems()
    
            If Session("TreeViewState") IsNot Nothing Then
                RestoreTreeViewState(TreeView1.Nodes, TryCast(Session("TreeViewState"), List(Of String)))
            End If
    
            Dim Search_Text As String
    
            If Session("Search_Text") <> "" Then
                Search_Text = Session("Search_Text")
    
                If Search_Text.Contains("Question:") Then
                    Search_Text = Search_Text.Substring(10, Search_Text.Length - 10)
                    SqlDataSource5.SelectCommand = "select distinct rr.Bset_Source, 'Home'+replace(rr.Bset_Location,'/','_')+'.PNG' Screenshot_FileName, rr.Bset_Location path, BA.AllDescriptions,  rr.Bset_Name name,rr.BSET_URL_Asset, BA.Question1, BA.Question2, BA.Question3 from [CBA_Control].[RR_Bsets_Categories_Plus_H] rr inner join [CBA_Control].[RR_Bsets_Plus] BA on rr.Bset_SourceID = BA.Bset_SourceID " & " where  Question1 like '" & Search_Text & "%' or Question2 like '" & Search_Text & "%' or  Question3 like '" & Search_Text & "%'"
                ElseIf Search_Text.Contains("Report:") Then
                    Search_Text = Search_Text.Substring(8, Search_Text.Length - 8)
                    SqlDataSource5.SelectCommand = "select distinct rr.Bset_Source, 'Home'+replace(rr.Bset_Location,'/','_')+'.PNG' Screenshot_FileName, rr.Bset_Location path, BA.AllDescriptions,  rr.Bset_Name name,rr.BSET_URL_Asset, BA.Question1, BA.Question2, BA.Question3 from [CBA_Control].[RR_Bsets_Categories_Plus_H] rr inner join [CBA_Control].[RR_Bsets_Plus] BA on rr.Bset_SourceID = BA.Bset_SourceID " & " where rr.Bset_Name like '%" & Search_Text & "%'"
                Else
                    SqlDataSource5.SelectCommand = "select distinct rr.Bset_Source, 'Home'+replace(rr.Bset_Location,'/','_')+'.PNG' Screenshot_FileName, rr.Bset_Location path, BA.AllDescriptions,  rr.Bset_Name name,rr.BSET_URL_Asset, BA.Question1, BA.Question2, BA.Question3 from [CBA_Control].[RR_Bsets_Categories_Plus_H] rr inner join [CBA_Control].[RR_Bsets_Plus] BA on rr.Bset_SourceID = BA.Bset_SourceID " & " where BA.AllDescriptions like '%" & Search_Text & "%'"
                End If
            End If
    
            If Levels <> "" Then
                SqlDataSource5.SelectCommand = "select distinct rr.Bset_Source, 'Home'+replace(rr.Bset_Location,'/','_')+'.PNG' Screenshot_FileName, rr.Bset_Location path, BA.AllDescriptions,  rr.Bset_Name name,rr.BSET_URL_Asset from [CBAControl].[RR_Bsets_Categories_Plus_H] rr inner join [CBA_Control].[RR_Bsets_Plus] BA on rr.Bset_SourceID = BA.Bset_SourceID " & "where rr.level_breadcrumb like '" & Levels & "%'"
                Dim Title_Header As String
                Title_Header = Dlookup("Category_Breadcrumb", "[CBA_Control].[RR_Categories_V2_H]", "Level_Breadcrumb = '" & Levels & "'")
                Lbl_Header.Text = Title_Header
            End If
        End Sub
    
        Private Sub RestoreTreeViewState(ByVal treeNodes As TreeNodeCollection, ByVal expandedList As List(Of String))
            For Each treeNode As TreeNode In treeNodes
                treeNode.Expanded = False
    
                If expandedList.Contains(treeNode.Text) Then
    
                    If treeNode.ChildNodes IsNot Nothing AndAlso treeNode.ChildNodes.Count > 0 Then
                        treeNode.Expanded = True
                        RestoreTreeViewState(treeNode.ChildNodes, expandedList)
                    End If
                End If
            Next
        End Sub
    
        Private Sub SaveTreeViewState(ByVal treeNodes As TreeNodeCollection, ByVal expandedList As List(Of String))
            For Each treeNode As TreeNode In treeNodes
    
                If treeNode.ChildNodes IsNot Nothing AndAlso treeNode.ChildNodes.Count > 0 Then
    
                    If treeNode.Expanded.HasValue AndAlso treeNode.Expanded = True Then
                        expandedList.Add(treeNode.Text)
                    End If
    
                    SaveTreeViewState(treeNode.ChildNodes, expandedList)
                End If
            Next
        End Sub
    
        Protected Sub TreeView1_TreeNodeExpandedCollapsed(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
            Dim expandedList As List(Of String) = New List(Of String)()
            SaveTreeViewState(TreeView1.Nodes, expandedList)
            Session("TreeViewState") = expandedList
        End Sub
    
        Public Shared Function Get_ODBC_Connection() As SqlConnection
            Dim MyCo As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DC_ConnectionString").ConnectionString)
            AddressOf Get_ODBC_Connection = New SqlConnection(MyCo.ConnectionString)
        End Function
    
        Public Shared Function Dlookup(ByVal strField As String, ByVal strDomain As String, ByVal strCondition As String) As String
            Dim objcommand As SqlCommand
            Dim ObjDataReader As SqlDataReader
            Dim sqlSTMT As String
            Dim conn As SqlConnection
    
            Try
                conn = Get_ODBC_Connection()
                conn.Open()
                objcommand = conn.CreateCommand
                sqlSTMT = "SELECT " & strField & " FROM " & strDomain & " WHERE " & strCondition
                objcommand.CommandText = sqlSTMT
                ObjDataReader = objcommand.ExecuteReader
                ObjDataReader.Read()
    
                If ObjDataReader.HasRows = True Then
                    AddressOf Dlookup = ObjDataReader.Item(0).ToString
                Else
                    AddressOf Dlookup = ""
                End If
    
                conn.Close()
                objcommand.Dispose()
            Catch
                AddressOf Dlookup = ""
            Finally
            End Try
        End Function
    
        Private Sub GetTreeViewItems()
            Dim cs As String = ConfigurationManager.ConnectionStrings("DC_ReportingConnectionString").ConnectionString
            Dim con As SqlConnection = New SqlConnection(cs)
            Dim da As SqlDataAdapter = New SqlDataAdapter("DCB_Control.spGetTreeViewItems", con)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            Dim ds As DataSet = New DataSet()
            da.Fill(ds)
            ds.Relations.Add("ChildRows", ds.Tables(0).Columns("ID"), ds.Tables(1).Columns("ParentId"))
            ds.Relations.Add("grandChildRows", ds.Tables(1).Columns("ID"), ds.Tables(2).Columns("ParentId"))
    
            For Each level1DataRow As DataRow In ds.Tables(0).Rows
                Dim treeNode As TreeNode = New TreeNode()
                treeNode.Text = level1DataRow("Category").ToString()
                treeNode.NavigateUrl = level1DataRow("NavigateURL").ToString()
                Dim level2DataRows As DataRow() = level1DataRow.GetChildRows("ChildRows")
    
                For Each level2DataRow As DataRow In level2DataRows
                    Dim childTreeNode As TreeNode = New TreeNode()
                    childTreeNode.Text = level2DataRow("Category").ToString()
                    childTreeNode.NavigateUrl = level2DataRow("NavigateURL").ToString()
                    Dim level3DataRows As DataRow() = level2DataRow.GetChildRows("grandChildRows")
    
                    For Each level3DataRow As DataRow In level3DataRows
                        Dim grandchildTreeNode As TreeNode = New TreeNode()
                        grandchildTreeNode.Text = level3DataRow("Category").ToString()
                        grandchildTreeNode.NavigateUrl = level3DataRow("NavigateURL").ToString()
                        childTreeNode.ChildNodes.Add(grandchildTreeNode)
                    Next
    
                    treeNode.ChildNodes.Add(childTreeNode)
                Next
    
                TreeView1.Nodes.Add(treeNode)
            Next
        End Sub
    
        Public Function FormatUrl_Preview(ByVal Screenshot_FileName As String) As String
            Dim File_path As String
            File_path = "C:\J\" & Screenshot_FileName
            Dim file_instance As FileInfo = New FileInfo(File_path)
    
            If file_instance.Exists Then
                AddressOf FormatUrl_Preview = "http://Screenshot/" & Screenshot_FileName
            Else
                AddressOf FormatUrl_Preview = ""
            End If
        End Function
    
        Public Function Format_Preview(ByVal Screenshot_FileName As String) As String
            Dim File_path As String
            File_path = "C:\J\" & Screenshot_FileName
            Dim file_instance As FileInfo = New FileInfo(File_path)
    
            If file_instance.Exists Then
                AddressOf Format_Preview = "Preview"
            Else
                AddressOf Format_Preview = ""
            End If
        End Function
    
        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim level1 As String
            level1 = DropDownList1.SelectedValue
    
            If level1 = "-1" Then
                SqlDataSource2.SelectCommand = "select null ID, '-1' Level_Breadcrumb, '--All Categories--' RRCategory union SELECT ID, Level_Breadcrumb, RRCategory  FROM [DC_Reporting].[DCB_Control].[RR_Categories_V2_H]  where  Hlevel = 2 order by id"
            Else
                SqlDataSource2.SelectCommand = "select null ID, '-1' Level_Breadcrumb, '--All Categories--' RRCategory union SELECT ID, Level_Breadcrumb, RRCategory  FROM [DC_Reporting].[DCB_Control].[RR_Categories_V2_H]  where  Hlevel = 2  and  level_breadcrumb like '" & level1 & ".%' and len(level_breadcrumb) < 6 order by id"
            End If
    
            DropDownList2.DataBind()
            lbl_Level.Text = level1
        End Sub
    
        Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim level2 As String
            level2 = DropDownList2.SelectedValue
    
            If level2 = "-1" Then
                lbl_Level.Text = DropDownList1.SelectedValue
            Else
                SqlDataSource3.SelectCommand = "select null ID, '-1' Level_Breadcrumb, '--All Categories--' RRCategory union SELECT ID, Level_Breadcrumb, RRCategory  FROM [DC_Reporting].[DCB_Control].[RR_Categories_V2_H]  where  Hlevel = 3  and  level_breadcrumb like '" & level2 & ".%'"
            End If
    
            DropDownList3.DataBind()
            lbl_Level.Text = level2
        End Sub
    
        Protected Sub DropDownList3_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim level3 As String
            level3 = DropDownList3.SelectedValue
    
            If level3 = "-1" Then
                lbl_Level.Text = DropDownList2.SelectedValue
            Else
                lbl_Level.Text = level3
            End If
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            SqlDataSource5.SelectCommand = "select distinct rr.Bset_Source, 'Home'+replace(rr.Bset_Location,'/','_')+'.PNG' Screenshot_FileName, rr.Bset_Location path, BA.AllDescriptions,  rr.Bset_Name name,rr.BSET_URL_Asset from [DCB_Control].[RR_Bsets_Categories_Plus_H] rr inner join [DCB_Control].[RR_Bsets_Plus] BA on rr.Bset_SourceID = BA.Bset_SourceID " & "where rr.level_breadcrumb like '" & lbl_Level.Text & "%'"
            GridView1.DataBind()
        End Sub

    Best regards,

    Wednesday, February 19, 2020 4:31 PM
  • User288213138 posted

    Hi noobcodervb,

    <asp:TreeView ID="TreeView1" ExpandDepth="0" runat="server"
                    style="width:270px" ForeColor="Black"  HoverNodeStyle-BackColor ="LightBlue"  SelectedNodeStyle BackColor="LightBlue"
                    ShowExpandCollapse="true" PopulateOnDemand="true" NodeWrap="true"  
                   NodeStyle-CssClass="treeNode"
            RootNodeStyle-CssClass="rootNode"
            LeafNodeStyle-CssClass="leafNode" ViewStateMode="Enabled"
            OnTreeNodeCollapsed="TreeView1_TreeNodeExpandedCollapsed"
            OnTreeNodeExpanded="TreeView1_TreeNodeExpandedCollapsed"></asp:TreeView>

    You didn't set the SelectedNodeStyle'BackColor in your code.

    <SelectedNodeStyle BackColor="Yellow"/>

    Best regards,

    Sam

    Thursday, February 20, 2020 6:27 AM
  • User378096647 posted

    Hi samwu,

    I do have the SelectednodeStyle backcolor="light blue" if you scroll to the right side of the code.

    Best regards,

    Vincent 

    Thursday, February 20, 2020 3:47 PM
  • User288213138 posted

    Hi noobcodervb,

    I do have the SelectednodeStyle backcolor="light blue" if you scroll to the right side of the code.

    Please try my code, I tried your code but it didn't work.

    Best regards,

    Sam

    Friday, February 21, 2020 9:29 AM