This is a migrated thread and some comments may be shown as answers.

dynamically select child nodes in Load-on-demand treeview

3 Answers 291 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 08 Oct 2010, 04:48 PM
I have a treeview that is set to load on demand.

When the user clicks a node, the page is reloaded with updated info and the treeview is reset back to showing just the parent node.

I need to have it auto-expand back to the previously selected node.

I've tried the FindbyValue method but since the trees not populated, there's no value to be found.

I've tried to navigate thru expanding as I go (I have the path to the end node saved) but I can't force the treeview to expand to populate the child.

Anyway to do this???

thanks.

3 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 14 Oct 2010, 09:19 AM
Hi John,

If the treeview is subscribed to NodeClick event, it should preserve its state after the postback, but if NavigateUrl property of the nodes is set, the page navigates and a new instance of the treeview is shown, so it's collapsed. What exactly is your case? Can you send us some sample code demonstrating your scenario? Thanks

Sincerely yours,
Yana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 14 Oct 2010, 03:26 PM

I found a way to do what I needed.

If there's a better way, please feel free to let me know.

TreeView is load on demand and nested in several containers.
Clicking on a node redirects back to same page with new parameters that displays correct info.
TreeView is dynamically expanded back to the selected node.

'*** CODE IN PAGE.ASPX
'treeview nested in panel inside pane inside splitter
<telerik:RadPanelItem Text="Navigation" PostBack="False" Expanded="True">
  <Items>
    <telerik:RadPanelItem Value="TreeTemplate">
      <ItemTemplate>
        <telerik:RadTreeView runat="Server" ID="trvDocs" Skin="Web20" OnNodeExpand="trvDocs_NodeExpand" OnNodeClick="trvDocs_NodeClick" ></telerik:RadTreeView>
      </ItemTemplate>
    </telerik:RadPanelItem>
  </Items>
</telerik:RadPanelItem>
  
  
'*** (partial) CODE IN PAGE.ASPX.VB
  
Partial Class WebForms_Page
  
    Private trvNavigate As RadTreeView
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        trvNavigate = CType(pnlMenuItems.FindItemByValue("TreeTemplate").FindControl("trvDocs"), RadTreeView)
        If Not IsPostBack Then
           LoadRootNodes(trvNavigate, TreeNodeExpandMode.ServerSideCallBack)
  
        'IF PAGE WAS CALLED BY TREEVIEW, THEN EXPAND TREE TO SELECTED ITEM
                Dim sVP As String
                If Request.QueryString("VPTH") Is Nothing Then
                    sVP = ""
                Else
                    sVP = Request.QueryString("VPTH")
                End If
                If sVP <> "" Then
                    Dim vNodeTrail As String() = Split(sVP, "/")
                    Dim Trail As String
                    Dim sTrailSteps As String = ""
                    Dim istp As Integer = 0
                    For Each Trail In vNodeTrail
                        sTrailSteps &= Trail
                        Dim n As RadTreeNode
                        n = trvNavigate.FindNodeByValue(sTrailSteps)
                        If n Is Nothing Then
                            lblErr.Text = "not found: " & sTrailSteps
                        Else
                            Call PopulateNodeDynamically(n)
                        End If
                        sTrailSteps &= "/"
                    Next
                End If
        End If
    End Sub
  
  
    Protected Sub trvDocs_NodeExpand(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs)
      PopulateNodeDynamically(e.Node)
    End Sub
  
    Protected Sub trvDocs_NodeClick(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs)
       LoadNewDoc(trvNavigate.SelectedNode.Value.ToString)
    End Sub
  
    Protected Sub LoadRootNodes(ByVal treeView As RadTreeView, ByVal expandMode As TreeNodeExpandMode)
        Dim cn As New SqlConnection
        Try
            OpenConnection(cn)
            Dim cmd As New SqlCommand("GetRootNodes")
            cmd.Connection = cn
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@CID", SqlDbType.Int)).Value = sPageCompanyID
            Dim adapter As New SqlDataAdapter(cmd)
            Dim data As New DataTable
            adapter.Fill(data)
            For Each row As DataRow In data.Rows
                Dim node As New RadTreeNode()
                node.Text = row("ParentName").ToString()
                node.Value = "V" & row("ParentNo").ToString()
                node.ExpandMode = expandMode
                treeView.Nodes.Add(node)
            Next
  
        Catch ex As Exception
            lblErr.Text = "Err: " & ex.Message & " [LoadRootNodes]"
        Finally
            cn.Close()
        End Try
  
   End Sub
  
   Protected Sub PopulateNodeDynamically(ByVal e As RadTreeNode)
        Dim cn As New SqlConnection
        Try
            Dim DocNo As String = ""
            DocNo = Right(e.Value, e.Value.Trim.Length - InStrRev(e.Value, "/"))
            OpenConnection(cn)
            Dim cmd As New SqlCommand("GetChildren")
            cmd.Connection = cn
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@ParentNo", SqlDbType.VarChar, 50)).Value = DocNo.Substring(1)
            cmd.Parameters.Add(New SqlParameter("@CID", SqlDbType.Int)).Value = sPageCompanyID
            Dim adapter As New SqlDataAdapter(cmd)
            Dim data As New DataTable
            adapter.Fill(data)
  
            For Each row As DataRow In data.Rows
                Dim node As New RadTreeNode()
                node.Text = row("ChildName").ToString()
                node.Value = e.Value.Trim & "/" & row("ChildType").ToString.Trim & row("ChildNo").ToString.Trim
                node.PostBack = True
                If Convert.ToInt32(row("ChildQty")) > 0 Then
                    node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack
                End If
                e.Nodes.Add(node)
            Next
  
            e.Expanded = True
  
        Catch ex As Exception
        Finally
            cn.Close()
        End Try
    End Sub
  
    Protected Sub LoadNewDoc(ByVal vPath As String)
        Dim DocNo As String = ""
        DocNo = Right(vPath, vPath.Trim.Length - InStrRev(vPath, "/"))
        OpenConnection(cn)
        Dim cmd As New SqlCommand("GetDocInfo")
        cmd.Connection = cn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@DocNo", SqlDbType.VarChar, 50)).Value = DocNo.Substring(1)
        cmd.Parameters.Add(New SqlParameter("@UserID", SqlDbType.Int)).Value = sUserID
             
        Dim drDoc As SqlDataReader
        drDoc = cmd.ExecuteReader()
        If drDoc.Read() Then
            Dim sFN As String = drDoc.Item("FileName")
            Dim sID As Integer = drDoc.Item("FileID")
            drDoc.Close()
            Response.Redirect("page.aspx?file=" & sFN & "&ID=" & sID & "&VPTH=" & vPath)
         End If
    End Sub
  
End Class
0
Yana
Telerik team
answered on 15 Oct 2010, 03:34 PM
Hello John,

I'm glad that you managed to find a solution, it seems correct to me.

Best wishes,
Yana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
TreeView
Asked by
John
Top achievements
Rank 1
Answers by
Yana
Telerik team
John
Top achievements
Rank 1
Share this question
or