Get the full path

9 posts, 0 answers
  1. Kjell
    Kjell avatar
    203 posts
    Member since:
    May 2011

    Posted 07 Oct 2012 Link to this post

    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"
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Oct 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kjell
    Kjell avatar
    203 posts
    Member since:
    May 2011

    Posted 09 Oct 2012 Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Oct 2012 Link to this post

    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.
  6. Kjell
    Kjell avatar
    203 posts
    Member since:
    May 2011

    Posted 11 Oct 2012 Link to this post

    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
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Oct 2012 Link to this post

    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.
  8. Kjell
    Kjell avatar
    203 posts
    Member since:
    May 2011

    Posted 12 Oct 2012 Link to this post

    thanks, works perfectly.....
  9. Kjell
    Kjell avatar
    203 posts
    Member since:
    May 2011

    Posted 13 Oct 2012 Link to this post

    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"
  10. Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 18 Oct 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017