Refresh treeview problems

10 posts, 0 answers
  1. Loy Chan
    Loy Chan avatar
    21 posts
    Member since:
    Mar 2008

    Posted 16 Feb 2011 Link to this post

    I used an example I found in these forums to show the # of files in a directory beside the name of the directory. The example seems to work fine but once I tried to change it around to suit my exact needs, I found some issues.

    I wanted the following changes
    1. All directories to show # of files within it including subdirectories (no problem, easily done)
    2. Setting the paths in the code behind rather than within the html
    3. Any change to the files (i.e. upload new file, delete file, move file) will automatically refresh the treeview

    Here are the problems I experienced:
    - if you set the path in the code behind, the refresh button will not update the # of files in the treeview (but will work if path are set within the html code)
    - I tried to use RadFileExplorer1.TreeView.Nodes.Clear(); in the page_load to force a refresh of the treeview (which works) but this had an adverse side effect. After a file delete or file move, the changed file still appears in the grid as if the file wasn't deleted or moved until I pressed the refresh button.

    Help?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    using Telerik.Web.UI.Widgets;
    using System.IO;
     
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RadFileExplorer1.TreeView.Nodes.Clear();
     
            RadFileExplorer1.Configuration.ViewPaths = new string[] {@"~/Thumbs/"};
            RadFileExplorer1.Configuration.UploadPaths = new string[] { @"~/Thumbs/" };
            RadFileExplorer1.Configuration.DeletePaths = new string[] { @"~/Thumbs/" };
     
            RadFileExplorer1.Configuration.ContentProviderTypeName = typeof(CustomProvider).AssemblyQualifiedName;
        }
     
        public class CustomProvider : FileSystemContentProvider
        {
            public CustomProvider(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl, string selectedItemTag)
                : base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)
            { }
     
     
            public override DirectoryItem ResolveRootDirectoryAsTree(string path)
            {
                DirectoryItem originalDir = base.ResolveRootDirectoryAsTree(path);
                string physicalPath = Context.Server.MapPath(path);
     
                List<DirectoryItem> childDirItems = new List<DirectoryItem>();
                foreach (DirectoryItem currentDir in originalDir.Directories)
                {
                    string physicalPathChildFile = Context.Server.MapPath(currentDir.FullPath);
                    DirectoryItem childDirItem = new DirectoryItem(currentDir.Name + "(" + Directory.GetFiles(physicalPathChildFile, "*.*", SearchOption.AllDirectories).Length + ")",
                                                                    currentDir.Location,
                                                                    currentDir.FullPath,
                                                                    currentDir.Tag,
                                                                    currentDir.Permissions,
                                                                    currentDir.Files,
                                                                    currentDir.Directories
                                                                      );
                    childDirItems.Add(childDirItem);
                }
     
     
                DirectoryItem dirItem = new DirectoryItem(originalDir.Name + "(" + Directory.GetFiles(physicalPath, "*.*", SearchOption.AllDirectories).Length + ")",
                                                          originalDir.Location,
                                                          originalDir.FullPath,
                                                          originalDir.Tag,
                                                          originalDir.Permissions,
                                                          originalDir.Files,
                                                         childDirItems.ToArray()
                                                          );
                return dirItem;
            }
     
            public override DirectoryItem ResolveDirectory(string path)
            {
                DirectoryItem originalDir = base.ResolveDirectory(path);
                string physicalPath = Context.Server.MapPath(path);
     
                DirectoryItem dirItem = new DirectoryItem(originalDir.Name + "(" + Directory.GetFiles(physicalPath, "*.*", SearchOption.AllDirectories).Length + ")",
                                                          originalDir.Location,
                                                          originalDir.FullPath,
                                                          originalDir.Tag,
                                                          originalDir.Permissions,
                                                          originalDir.Files,
                                                          originalDir.Directories
                                                          );
                return dirItem;
            }
        }
    }

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register TagPrefix="Telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="Form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <telerik:RadFileExplorer ID="RadFileExplorer1" runat="server" Height="300px" Width="804px"
            Skin="Forest">
            <Configuration SearchPatterns="*.*"></Configuration>
        </telerik:RadFileExplorer>
        </form>
    </body>
    </html>
  2. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 21 Feb 2011 Link to this post

    Hi Loy,

    The reason for the reported problem to occur is that when the fileepxlorer refreshes after a command it is not doing a complete reload of the files / folders. This is an expected behavior that a  result of the way that the control is designed to minimize the loading time and provide better performance - when a refresh occurs the ResolveRootDirectoryAsTree() method of the content provider is called only for the current folder and that is why the name of the current folder is not updated with the new number. The approach taken in the provided sample is not taking this into account.

    A possible approach to achieve the required functionality is to modify the FileItems' names during the RadFileExplorer's ExplorerPopulated server-side event., however, we will need a bit more time to investigate the exact case deeper. I will bookmark this thread and provide more-to-the-point answer till the end of the day.

    Greetings,
    Dobromir
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 28 Feb 2011 Link to this post

    Hi Loy,

    As I mentioned in my previous post the reason for this problem comes from the fact that ResolveRootDirectoryAsTree() is called for the currently selected folder only and currently this behavior cannot be modified.

    At present, the required functionality cannot be completely implemented to the RadFileExplorer.

    Best wishes,
    Dobromir
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  5. lordscarlet
    lordscarlet avatar
    19 posts
    Member since:
    Jan 2007

    Posted 23 Mar 2011 Link to this post

    I have a similar situation. I am using a custom ContentProvider. Through input outside of the fileexplorer I am modifying different filters for the data. As those filters are modified, I would like to refresh the Tree. I am able to refresh the grid through javascript and the grid's refresh() method, but I am unable to find a way to refresh the TreeView.
  6. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 28 Mar 2011 Link to this post

    Hi Doug,

    I am not quite sure I understand the exact scenario. Could you please describe in more details the exact scenario? How do you apply the filters?

    To be able to provide more to the point answer we might need to examine the custom content provider and debug it locally. Could you please open a support and provide a sample project reproducing the problem?

    Regards,
    Dobromir
    the Telerik team
  7. Ed
    Ed avatar
    14 posts
    Member since:
    Dec 2010

    Posted 05 Apr 2011 Link to this post

    I also am experiencing this - I've added an "Admin view" checkable button to the RadFileExplorer Toolbar which displays normally hidden folders when checked, but otherwise hides them. I can hide/show folders in the Grid successsfully with this, but cannot figure out how to get the treeview to change once it's set up.
  8. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 06 Apr 2011 Link to this post

    Hi Edward,

    By design, RadFileExplorer does not fully reload its TreeView in order to increase the performance of the control. In order to force full refresh of the TreeView you can clear its node collection, e.g.:
    RadFileExplorer1.TreeView.Nodes.Clear();

    This will force the ResolveRootDirectoryAsTree() method of the content provider to be called for the Root folder.

    Regards,
    Dobromir
    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
  9. Ed
    Ed avatar
    14 posts
    Member since:
    Dec 2010

    Posted 06 Apr 2011 Link to this post

    When I do that, the treeview is then blank - all of the items have been cleared out.

    aspx file:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DemoRFE.aspx.vb" Inherits="Standard_DemoRFE" %>
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager runat="server" ID="rsmMain"></telerik:RadScriptManager>
            <telerik:RadFileExplorer runat="server" ID="rfeFiles" Width="100%" DisplayUpFolderItem="true"
                EnableCopy="true" EnableCreateNewFolder="true" EnableOpenFile="true" EnableFilterTextBox="true">
                <Configuration ViewPaths="/" UploadPaths="/" SearchPatterns="*.aspx,*.gif,*.jpg,*.png,*.pdf" />            
            </telerik:RadFileExplorer>
        </div>
        </form>
    </body>
    </html>

    the codebehind .aspx.vb file:
    Imports Telerik.Web.UI.Widgets
      
    Partial Class Standard_DemoRFE
        Inherits System.Web.UI.Page
      
        Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
            rfeFiles.Configuration.ContentProviderTypeName = GetType(DemoSitePageFileSystemContentProvider).AssemblyQualifiedName
      
            SetupFilesToolbar()
        End Sub
      
        Private Sub SetupFilesToolbar()
            ' Add Master Admin buttons
            If IsUserAMasterAdmin() Then
                ' Add spacer
                Dim spacerButton As New RadToolBarButton("|")
                With spacerButton
                    .IsSeparator = True
                    rfeFiles.ToolBar.Items.Add(spacerButton)
                End With
      
                ' Add Show All Folders button
                Dim customButton As New RadToolBarButton("Show All Folders")
                With customButton
                    .Value = "showAllFolders"
                    .Checked = Not ShowAllFolders()
                    .CheckOnClick = True
                    .AllowSelfUnCheck = True
                    rfeFiles.ToolBar.Items.Add(customButton)
                    AddHandler rfeFiles.ToolBar.ButtonClick, AddressOf ChangeShowAllFolders
                End With
            End If
      
        End Sub
      
        Private Function IsUserAMasterAdmin() As Boolean
            ' This is normally handled elsewhere
            Return True
        End Function
      
        Protected Sub ChangeShowAllFolders(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarEventArgs)
            SetShowAllFolders(Not ShowAllFolders())
      
            rfeFiles.TreeView.Nodes.Clear()
      
            If HideFile(rfeFiles.CurrentFolder) Then
                rfeFiles.InitialPath = "/"
            Else
                rfeFiles.InitialPath = rfeFiles.CurrentFolder
            End If
      
        End Sub
      
        Public Function ShowAllFolders() As Boolean
            Return SafeGetBoolean(HttpContext.Current.Session("FileExplorer.ShowAllFolders"))
        End Function
      
        Public Sub SetShowAllFolders(ByVal ShowAll As Boolean)
            HttpContext.Current.Session("FileExplorer.ShowAllFolders") = ShowAll
        End Sub
      
        Private Function HideFile(ByRef File As Telerik.Web.UI.Widgets.FileBrowserItem) As Boolean
            Return HideFile(File.Path)
        End Function
      
        Private Function HideFile(ByVal FilePath As String) As Boolean
            ' Handle Admins Differently
            Dim boolIsAdmin As Boolean = IsUserAMasterAdmin()
            Dim boolShowAll As Boolean = ShowAllFolders()
      
            If FilePath.ToLower.Contains("_vti_") Then
                Return True
            ElseIf FilePath.StartsWith("/_vti_") Then
                Return True
            ElseIf FilePath.StartsWith("/aspnet_client") Then
                Return True
            ElseIf FilePath.StartsWith("/App_") And Not boolIsAdmin And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/editor") And Not boolIsAdmin And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/js") And Not boolIsAdmin And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/bin") And Not boolIsAdmin And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/_") And Not boolIsAdmin And Not boolShowAll Then
                Return True
            Else
                Return False
            End If
        End Function
      
    End Class
      
    Public Class DemoSitePageFileSystemContentProvider : Inherits Telerik.Web.UI.Widgets.FileSystemContentProvider
      
        Public Function ShowAllFolders() As Boolean
            Return SafeGetBoolean(HttpContext.Current.Session("FileExplorer.ShowAllFolders"))
        End Function
      
        Public Overloads Overrides Function ResolveDirectory(ByVal path As String) As DirectoryItem
            Dim originalFolder As DirectoryItem = MyBase.ResolveDirectory(path)
            Dim originalFiles As FileItem() = originalFolder.Files
            Dim filteredDirectories As New Generic.List(Of DirectoryItem)
            Dim filteredFiles As New Generic.List(Of FileItem)
      
            ' Filter the files   
            For Each originalFile As FileItem In originalFiles
                If Not Me.IsFiltered(originalFile.Path) Then
                    filteredFiles.Add(originalFile)
                End If
            Next
      
            ' Filter the folders    
            For Each originalDir As DirectoryItem In originalFolder.Directories
                If Not Me.IsFiltered(originalDir.FullPath) Then
                    filteredDirectories.Add(originalDir)
                End If
            Next
      
            Dim newFolder As New DirectoryItem(originalFolder.Name, originalFolder.Location, originalFolder.FullPath, originalFolder.Tag, originalFolder.Permissions, filteredFiles.ToArray(), filteredDirectories.ToArray())
      
            Return newFolder
        End Function
      
        Public Overloads Overrides Function ResolveRootDirectoryAsTree(ByVal path As String) As DirectoryItem
            Dim originalFolder As DirectoryItem = MyBase.ResolveRootDirectoryAsTree(path)
            Dim originalDirectories As DirectoryItem() = originalFolder.Directories
            Dim filteredDirectories As New Generic.List(Of DirectoryItem)
      
            ' Filter the folders    
            For Each originalDir As DirectoryItem In originalDirectories
                If Not Me.IsFiltered(originalDir.FullPath) Then
                    filteredDirectories.Add(originalDir)
                End If
            Next
            Dim newFolder As New DirectoryItem(originalFolder.Name, originalFolder.Location, originalFolder.FullPath, originalFolder.Tag, originalFolder.Permissions, originalFolder.Files, filteredDirectories.ToArray())
      
            Return newFolder
        End Function
      
        Private Function IsFiltered(ByVal FilePath As String) As Boolean
            ' Get rid of double slashes
            FilePath = FilePath.Replace("//", "/")
      
            ' Handle Admins Differently
            Dim boolShowAll As Boolean = Me.ShowAllFolders
            ' Not needed for demo
            'If Not SiteFunctions.IsUserAMasterAdmin Then boolShowAll = False
      
            If FilePath.ToLower.Contains("_vti_") Then
                Return True
            ElseIf FilePath.StartsWith("/_vti_") Then
                Return True
            ElseIf FilePath.StartsWith("/aspnet_client") Then
                Return True
            ElseIf FilePath.StartsWith("/App_") And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/editor") And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/js") And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/bin") And Not boolShowAll Then
                Return True
            ElseIf FilePath.StartsWith("/_") And Not boolShowAll Then
                Return True
            Else
                Return False
            End If
      
        End Function
      
        Public Sub New(ByVal context As System.Web.HttpContext, ByVal searchPatterns() As String, ByVal viewPaths() As String, ByVal uploadPaths() As String, ByVal deletePaths() As String, ByVal selectedUrl As String, ByVal selectedItemTag As String)
            MyBase.New(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)
        End Sub
    End Class
  10. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 11 Apr 2011 Link to this post

    Hi Edward,

    The problem occurs because the nodes are cleared after the RadFileExplorer have already retrieved the new items ( after calling content provider's ResolveRootDirectoryAsTree()). The latest moment in the page lifecycle where such modification can be applied is the Page_Load event.

    For this scenario, I would recommend to use a hidden field as a flag and modify its value with JavaScript on button click. Then in the Page_Load check the value of this hidden field and clear the nodes if needed, e.g.:
    ASPX
    <input type="hidden" name="shouldResetTree" id="shouldResetTree" runat="server" value="0" />
     
    <script type="text/javascript">
        function toolbarButtonClicked(sender, args)
        {
            if (args.get_item().get_value() == "showAllFolders")
            {
                $get("shouldResetTree").value = 1;
            }
        }
    </script>
    Code-Behind:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
        rfeFiles.ToolBar.OnClientButtonClicked = "toolbarButtonClicked"
     
        If (shouldResetTree.Value = "1") Then
            rfeFiles.TreeView.Nodes.Clear()
     
            shouldResetTree.Value = "0" 'reset the value
        End If
     
    End Sub

    Hope this helps.

    Greetings,
    Dobromir
    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
  11. Ed
    Ed avatar
    14 posts
    Member since:
    Dec 2010

    Posted 12 Apr 2011 Link to this post

    While this seems to work, I'd strongly suggest not requiring strange, non-intuitive workarounds like this. I understand sometimes there are tradeoffs that need to be made in development, but this seems like a bug, not a feature.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017