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

Get the full path

8 Answers 260 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Kjell
Top achievements
Rank 1
Iron
Kjell asked on 07 Oct 2012, 08:30 PM
I use exemple TreeView / Directory Structure.
When I click the button i get the selected file name (default.aspx) with "RadTreeView1.SelectedNode.Text"
How get the full path/address of the selected file, like "/organisation/klubbar/default.aspx"

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Oct 2012, 04:13 AM
Hi Kjell,

Following is the sample code that I tried to get the path of the selectednode on an external button click.

ASPX:
<asp:Button ID="Button1" runat="server" OnClientClick="OnClientClick(); return false;"  />

JS:
<script type="text/javascript">
    function OnClientClick() {
        debugger;
        var tree = $find("<%= RadTreeView1.ClientID %>");
        var node = tree.get_selectedNode();
        var s = node.get_text();
        var currentObject = node.get_parent();
        while (currentObject != null) {
            if (currentObject.get_parent() != null) {
                s = currentObject.get_text() + " / " + s;
            }
            currentObject = currentObject.get_parent();
        }
        alert(s);
    }
</script>
Please take a look into this documentation for more information.

Hope this helps.

Regards,
Princy.
0
Kjell
Top achievements
Rank 1
Iron
answered on 09 Oct 2012, 09:33 AM
I do not know, but I get only the filename not the full path. Do you have a working example in vb?
http://test.draghundsport.se/test/default.aspx
0
Princy
Top achievements
Rank 2
answered on 10 Oct 2012, 04:13 AM
Hi Kjell,

Unfortunately I couldn't replicate your issue. Here is the full code that I tried to achieve your scenario.

ASPX:
<telerik:RadTreeView ID="RadTreeView1" runat="server" OnNodeExpand="RadTreeView1_NodeExpand">
</telerik:RadTreeView>
<asp:Button ID="Button1" runat="server" Text="Login" OnClientClick="OnClientClick(); return false;" />

JS:
<script type="text/javascript">
    function OnClientClick() {
        debugger;
        var tree = $find("<%= RadTreeView1.ClientID %>");
        var node = tree.get_selectedNode();
        var s = node.get_text();
        var currentObject = node.get_parent();
        while (currentObject != null) {
            if (currentObject.get_parent() != null) {
                s = currentObject.get_text() + " / " + s;
            }
            currentObject = currentObject.get_parent();
        }
        alert(s);  //s is the path of the selectednode
    }
</script>

VB:
Private ReadOnly KnownExtensions As String() = New String() {"aspx", "asmx", "cs", "vb", "xml", "ascx", _
    "jpg", "png", "gif", "html"}
 
Private Sub BindTreeToDirectory(virtualPath As String, parentNode As RadTreeNode)
    Dim physicalPath As String = Server.MapPath(virtualPath)
    Dim directories As String() = Directory.GetDirectories(physicalPath)
    For Each directory__1 As String In directories
        Dim node As New RadTreeNode(Path.GetFileName(directory__1))
        node.Value = virtualPath + "/" + Path.GetFileName(directory__1)
        node.ImageUrl = "~/TreeView/Img/Vista/folder.png"
        node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack
        parentNode.Nodes.Add(node)
    Next
    Dim files As String() = Directory.GetFiles(physicalPath)
    For Each file As String In files
        Dim node As New RadTreeNode(Path.GetFileName(file))
        Dim extension As String = Path.GetExtension(file).ToLower().TrimStart("."C)
        If Array.IndexOf(KnownExtensions, extension) > -1 Then
            node.ImageUrl = "~/TreeView/Img/Vista/" + extension + ".png"
        Else
            node.ImageUrl = "~/TreeView/Img/Vista/unknown.png"
        End If
        parentNode.Nodes.Add(node)
    Next
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not Page.IsPostBack Then
        Dim rootNode As New RadTreeNode("Examples")
        rootNode.Value = "~/TreeView/Examples"
        rootNode.ImageUrl = "~/TreeView/Img/Vista/folder.png"
        rootNode.ExpandMode = TreeNodeExpandMode.ServerSideCallBack
        RadTreeView1.Nodes.Add(rootNode)
    End If
End Sub
Protected Sub RadTreeView1_NodeExpand(sender As Object, e As RadTreeNodeEventArgs)
    BindTreeToDirectory(e.Node.Value, e.Node)
End Sub

Hope this helps.

Regards,
Princy.
0
Kjell
Top achievements
Rank 1
Iron
answered on 11 Oct 2012, 12:29 PM
Sorry I was not clear, I want to save the full path in a database. So I want to pick it out in Code Behind


Protected Sub btnKlick_Click(sender As Object, e As EventArgs) Handles btnKLick.Click
    lblMsg.Text = RadTreeView1.SelectedNode.Text 'get the full path
       save to database..........

End Sub
0
Princy
Top achievements
Rank 2
answered on 12 Oct 2012, 04:34 AM
Hi Kjell,

Try the following code snippet to achieve your scenario.

VB:
Protected Sub Button1_Click1(sender As Object, e As EventArgs)
    Dim node As RadTreeNode = RadTreeView1.SelectedNode
    Dim s As String = node.Text
    Dim currentObject As RadTreeNode = node.ParentNode
    While currentObject IsNot Nothing
        If currentObject.Parent IsNot Nothing Then
            s = currentObject.Text + "/" + s
        End If
        currentObject = currentObject.ParentNode
    End While
    Response.Write(s)
End Sub

Hope this helps.

Regards,
Princy.
0
Kjell
Top achievements
Rank 1
Iron
answered on 12 Oct 2012, 12:33 PM
thanks, works perfectly.....
0
Kjell
Top achievements
Rank 1
Iron
answered on 13 Oct 2012, 02:35 PM
One more question, is it possible to set the selected node ("/organisation/klubbar/default.aspx") by default in Page Load

I have save the full path in a database.

    If Not Page.IsPostBack Then......
        expanded and selected, for example "/organisation/klubbar/default.aspx"
0
Plamen
Telerik team
answered on 18 Oct 2012, 06:36 AM
Hello Kjell,

You can refer to this Code Library where similar behavior have been explained. 

Hope this will be helpful.

Kind regards,
Plamen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
TreeView
Asked by
Kjell
Top achievements
Rank 1
Iron
Answers by
Princy
Top achievements
Rank 2
Kjell
Top achievements
Rank 1
Iron
Plamen
Telerik team
Share this question
or