This is a migrated thread and some comments may be shown as answers.
Refresh treeview problems
9 Answers 156 Views
This is a migrated thread and some comments may be shown as answers.
Loy Chan
Top achievements
Rank 1
Loy Chan asked on 16 Feb 2011, 08:22 AM
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>

9 Answers, 1 is accepted

Sort by
0
Dobromir
Telerik team
answered on 22 Feb 2011, 01:54 AM
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!
0
Dobromir
Telerik team
answered on 28 Feb 2011, 03:40 PM
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!
0
lordscarlet
Top achievements
Rank 1
answered on 24 Mar 2011, 12:10 AM
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.
0
Dobromir
Telerik team
answered on 28 Mar 2011, 04:17 PM
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
0
Ed
Top achievements
Rank 1
answered on 05 Apr 2011, 08:08 PM
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.
0
Dobromir
Telerik team
answered on 06 Apr 2011, 12:08 PM
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
0
Ed
Top achievements
Rank 1
answered on 06 Apr 2011, 03:36 PM
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
0
Dobromir
Telerik team
answered on 11 Apr 2011, 11:40 AM
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
0
Ed
Top achievements
Rank 1
answered on 12 Apr 2011, 06:34 PM
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.
Asked by
Loy Chan
Top achievements
Rank 1
Answers by
Dobromir
Telerik team
lordscarlet
Top achievements
Rank 1
Ed
Top achievements
Rank 1
Share this question
or