CurrentFolder is not update in codebehind

2 posts, 1 answers
  1. Patrick
    Patrick avatar
    20 posts
    Member since:
    Feb 2008

    Posted 22 Aug 2011 Link to this post

    Hello,

    I have build an uploadcontrol. The user can upload files and can view the uploaded file. For this I use the RadFileExplorer. Everything is working fine except deleting files. When I change a folder the addressbar is updated fine. But when I want to delete an image in the FileExplorer, the file is not deleted because the CurrentFolder-property in the codebehind is not updated to the path of the folder I selected. When I fire the event for the second time it works well because the CurrentFolder-property is updated this time. So, it looks I'm walking one postback behind.

    Can anyone tell me what I'm doing wrong. Thx

    <head runat="server">
        <title>UploadManager voor productafbeeldingen</title>
        <link href="CSI/css/custom/admin.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/Slider.UIN.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/ComboBox.UIN.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/Calendar.UIN.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/Input.UIN.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/TreeView.UIN.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/Upload.UIN.css" rel="stylesheet" type="text/css" />
        <link href="CSI/CSS/telerik/FileExplorer.UIN.css" rel="stylesheet" type="text/css" />
        <script src="CSI/JS/3rdparty/flash_detect.js" type="text/javascript"></script>
        <script src="CSI/JS/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script>
        <script src="CSI/JS/jQuery/jquery-1.2.6.min.js" type="text/javascript"></script>
        <script src="CSI/JS/telerik/IntegrationScripts.js" type="text/javascript"></script>
        <telerik:RadCodeBlock ID="codeBlock1" runat="server">
            <script type="text/javascript">
                //<![CDATA[           
     
                function OnClientDirectoryChange(sender, args) {
                    alert("Tree dir change: " + args.get_path());
                }
                
                function OnClientItemClick(sender, args) {
                    alert("Item click: " + args.get_path());
                }          
     
                //]]>
            </script>
        </telerik:RadCodeBlock>
        </head>
    <body>
        <form runat="server" id="mainForm" method="post">
        <telerik:RadScriptManager ID="ScriptManager1" runat="server" />
        <telerik:RadProgressManager ID="Radprogressmanager1" runat="server" />
        <div class="productimagemanager">
            <div class="qualitycenter">
                <div class="fileexplorer">
                    <table cellspacing="4">
                        <tr>
                            <td rowspan="2" style="vertical-align: top;">                          
                                <telerik:RadFileExplorer runat="server" ID="RadFileExplorer1" Width="700px" Height="500px" Skin="UIN" EnableOpenFile="true"
                                    OnClientLoad="OnFileExplorerLoad" OnClientFolderChange="OnClientDirectoryChange">
                                    <Configuration ViewPaths="~/media/QualityCenter/forapproval/retailer65" DeletePaths="~/media/QualityCenter/forapproval/retailer65" UploadPaths="~/media/QualityCenter/forapproval/retailer65" />
                                </telerik:RadFileExplorer>
                            </td>
                        </tr>
                    </table>
                </div>
                <asp:Panel ID="ListViewHolder" runat="server">
                    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel" runat="server">
                    </telerik:RadAjaxLoadingPanel>
                    <telerik:RadXmlHttpPanel ID="RadXmlHttpPanel1" runat="server" OnServiceRequest="RadXmlHttpPanel1_ServiceRequest" RenderMode="Block" EnableClientScriptEvaluation="true"
                        LoadingPanelID="RadAjaxLoadingPanel" OnClientResponseEnded="OnClientResponseEnded">
                        <telerik:RadContextMenu ID="RadListViewContextMenu" runat="server" OnClientItemClicked="RadListViewMenuItemClicked">
                        </telerik:RadContextMenu>
                        <telerik:RadListView ID="RadListView1" runat="server" ItemPlaceholderID="ItemPlaceHolder1" AllowPaging="true" AllowCustomPaging="true" PageSize="6"
                            OnItemDataBound="RadListView1_ItemDataBound" OnItemCommand="RadListView1_ItemCommand">
                            <LayoutTemplate>
                                <div class="imagelibrary">
                                    <div class="actiebuttons">
                                        <asp:ImageButton ID="btnSaveNewImageNames" runat="server" ImageUrl="~/App_Themes/UIN/Layout/buttons/opslaanensluitenbutton.png" Width="194px"
                                            Height="30px" ToolTip="Opslaan artikel en artikelwizard afsluiten" CommandName="updatefilenames" Visible="false" />
                                    </div>
                                    <asp:PlaceHolder ID="ItemPlaceHolder1" runat="server"></asp:PlaceHolder>
                                </div>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <asp:Panel ID="itemTemplateHolder" runat="server" CssClass="lvItemTemplate" ondblclick="openImageForPreview(this);" onclick="imageSelected(this);">
                                    <div class="imageitem">
                                        <div class="imagefile">
                                            <asp:Image ID="imgImageFile" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Path") %>' AlternateText="Image" CssClass="imagefile" />
                                            <asp:ImageButton ID="imgDeleteFile" runat="server" ImageUrl="~/App_Themes//UIN//Layout//buttonDeleteImage.png" CssClass="deleteimage" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Path") %>'
                                                OnClick="imgDeleteFile_Click" OnClientClick="return confirm('Weet u zeker dat u deze afbeelding wilt verwijderen?');" />
                                            <input type="hidden" value="<%# GetFileItemProperties(Container.DataItem) %>" />
                                        </div>
                                        <div class="imageoldname">
                                            <asp:Label ID="lblImageOldNameLabel" runat="server" Text="Huidige bestandsnaam" CssClass="labelheader"></asp:Label>
                                            <asp:Label ID="lblImageOldName" runat="server" CssClass="imagenamelabel" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label></div>
                                        <div class="imagenewname">
                                            <asp:Label ID="lblImageNewName" runat="server" Text="Nieuwe naam" CssClass="labelheader"></asp:Label>
                                            <asp:TextBox ID="txtImageNewName" runat="server">
                                            </asp:TextBox></div>
                                    </div>
                                </asp:Panel>
                            </ItemTemplate>
                        </telerik:RadListView>                   
                    </telerik:RadXmlHttpPanel>
                </asp:Panel>
            </div>
        </div>   
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
     
                function loadListViewContent(pathToFolder, sliderIndex) {
                    var pageIndexChangeArg = pathToFolder + "$|$" + sliderIndex;
                    var oXmlPanel = $find("<%= RadXmlHttpPanel1.ClientID %>");
                    selectedListViewItems = []; // Clear selection
                    oXmlPanel.set_value(pageIndexChangeArg);
                }
     
                function getRadFileExplorer1() {// returns RadFileExplorer1 client-side object
                    var oExplorer = $find("<%= RadFileExplorer1.ClientID %>");
                    return oExplorer;
                }
                
            </script>
        </telerik:RadCodeBlock>
        </form>
    </body>
    </html>
    protected void Page_Load(object sender, System.EventArgs e)
        {
            RadFileExplorer1.VisibleControls = Telerik.Web.UI.FileExplorer.FileExplorerControls.ContextMenus |
                                                    Telerik.Web.UI.FileExplorer.FileExplorerControls.TreeView|
                                                    Telerik.Web.UI.FileExplorer.FileExplorerControls.AddressBox;
     
            //set properties according to configuration panel
            RadFileExplorer1.Configuration.SearchPatterns = new string[] { "*.jpg", "*.jpeg" };      
            RadFileExplorer1.EnableOpenFile = false;
            RadFileExplorer1.DisplayUpFolderItem = true;
            RadFileExplorer1.AllowPaging = false;
            RadFileExplorer1.EnableCreateNewFolder = false;
             
            RadFileExplorer1.FindControl("paneTree").Controls.Add(ListViewHolder);
     
            if (!Page.IsPostBack)
            {
                BindCollections();
                txtArchiveName.Text = DateTime.Today.ToShortDateString();
     
                if (!string.IsNullOrEmpty(RadFileExplorer1.InitialPath))
                {
                    bool isDirectory = VirtualPathUtility.GetExtension(RadFileExplorer1.InitialPath) == string.Empty;
                    if (isDirectory)
                    {// The InitialPath points to a file
                        // Get the directory:
                        string parentDirectory = VirtualPathUtility.GetDirectory(RadFileExplorer1.InitialPath);
                        LoadListViewData(0, parentDirectory);
                    }
                    else
                    {// The InitialPath contains a directory path
                        string parentDirectory = RadFileExplorer1.InitialPath;
                        LoadListViewData(0, parentDirectory);
                    }
                }
                else
                {// The InitialPath property is not set ==> load the first folder in the ViewPaths property
                    string targetFolder = RadFileExplorer1.Configuration.ViewPaths[0] + "/" + cboCollection.Items[0].Text.Replace(" ", "_"); // +"/" + txtArchiveName.Text;
     
                    string parentDirectory = targetFolder; // RadFileExplorer1.Configuration.ViewPaths[0];               
                    LoadListViewData(0, parentDirectory);
                }                    
            }
            else
            {
                Control postbackControl = GetPostBackControl(this);// get the control that is coused the post back ;          
     
                if (postbackControl != null && postbackControl.ID == "buttonSubmit")
                {
                    RadFileExplorer1.TreeView.Nodes.Clear();
                    TargetFolder = "~/" + RETAILER_IMAGEPATH + FILIAAL_ID + "/" + cboCollection.Items[0].Text + "/" + txtArchiveName.Text;               
                    RadFileExplorer1.InitialPath = Page.ResolveUrl(TargetFolder);               
                }                       
            }
     
            //RemoveBackForwardRefreshButtons();
        }
     
    protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
     
            if (IsPostBack)
            {// This code ensures that the RadListView's content will be refreshed when a new file is uploaded
                string parentDirectory = RadFileExplorer1.CurrentFolder;
                LoadListViewData(0, parentDirectory);
            }
        }
     
    protected void imgDeleteFile_Click(object sender, ImageClickEventArgs e)
        {
            File.Delete(Server.MapPath(((ImageButton)sender).CommandArgument));
        }
     
        protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
        {// This event is used in order to associate the 'RadListViewContextMenu' with every itemTemplateHolder panes in the RadListView
            Telerik.Web.UI.RadListViewDataItem lvitem = (Telerik.Web.UI.RadListViewDataItem)e.Item;
            ContextMenuElementTarget elementTarget = new ContextMenuElementTarget();
            Control paneControl = e.Item.FindControl("itemTemplateHolder");
            RadBinaryImage oImage = e.Item.FindControl("imgImageFile") as RadBinaryImage;
            ImageButton oDeleteImage = e.Item.FindControl("imgDeleteFile") as ImageButton;
            elementTarget.ElementID = paneControl.ClientID;
            RadListViewContextMenu.Targets.Add(elementTarget);
        }
     
        protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.CommandName == "updatefilenames")
            {
                foreach (RadListViewDataItem item in RadListView1.Items)
                {
                    TextBox oNewImageName = item.FindControl("txtImageNewName") as TextBox;
                    Label oOldImageName = item.FindControl("lblImageOldName") as Label;
                    Image oImage = item.FindControl("imgImageFile") as Image;
                    string path = RadFileExplorer1.CurrentFolder;
     
                    if (oOldImageName != null && oNewImageName != null)
                    {
                        if (oNewImageName.Text != string.Empty)
                        {
                            string updatePath = oImage.ImageUrl.Replace(oOldImageName.Text, oNewImageName.Text + ".jpg");
                            File.Move(Server.MapPath(oImage.ImageUrl), Server.MapPath(updatePath));
                        }
                    }              
                }
     
                LoadListViewData(0, RadFileExplorer1.CurrentFolder);
            }
        }
     
        protected void RadXmlHttpPanel1_ServiceRequest(object sender, RadXmlHttpPanelEventArgs e)
        {// The value of e.Value looks like this: '/ListViewIntegration/FileExplorer/ExplorerSource/Images/Customers$|$0'
            string[] arguments = e.Value.Split(new string[] { "$|$" }, StringSplitOptions.RemoveEmptyEntries);
            string pathToFolder = arguments[0];
            string sliderValue = arguments[1];
            int sliderIndexAsInt = int.Parse(sliderValue);
            LoadListViewData(sliderIndexAsInt, pathToFolder);
        }
     
     private void LoadListViewData(int sliderIndex, string pathToFolderForLoad)
        {
            List<FileItem> allFilesInFolder = GetFilesSorted(pathToFolderForLoad);
     
            if (allFilesInFolder.Count.Equals(0))
            {// No files in directory
     
                //RadSlider1.Enabled = false;// Disable slider (or hide it (RadSlider1.Visible = false))
                RadListView1.DataSource = null;
                RadListView1.DataBind();// Clear content
                ((ImageButton)RadListView1.FindControl("btnSaveNewImageNames")).Visible = false;
            }
            else
            {
                //RadSlider1.Enabled = true;// Enable RadeSlider
     
                // Starting index
                int fromIndex = sliderIndex * RadListView1.PageSize;
     
                // Calculate the count of the images
                int count = (fromIndex + RadListView1.PageSize) < allFilesInFolder.Count ? RadListView1.PageSize : allFilesInFolder.Count - fromIndex;
     
                // Get maximum 4 files at a time
                List<FileItem> listViewSource = allFilesInFolder.GetRange(fromIndex, count);
                RadListView1.DataSource = listViewSource;
                RadListView1.DataBind();
                ((ImageButton)RadListView1.FindControl("btnSaveNewImageNames")).Visible = true;
     
                 
                long outLong = new long();// USed in order to be compliant with the VB.NET version
                // Refresh slire's values
                //RadSlider1.MaximumValue = (allFilesInFolder.Count % RadListView1.PageSize).Equals(0) ? Math.DivRem(allFilesInFolder.Count, RadListView1.PageSize, out outLong) - 1 : Math.DivRem(allFilesInFolder.Count, RadListView1.PageSize, out outLong);
                //RadSlider1.Value = sliderIndex;
     
                // Checks permissions and associates a context menu with all images in the ListView
                CreateListContextMenu(pathToFolderForLoad);
            }
        }
     
     private void CreateListContextMenu(string pathToOpenedFolder)
        {
            RadTreeViewContextMenu treeContextMenu = RadFileExplorer1.TreeView.ContextMenus[0];
            RadListViewContextMenu.Items.Clear();
            foreach (RadMenuItem treeMenuItem in treeContextMenu.Items)
            {
                RadMenuItem clonedTreeMenuItem = treeMenuItem.Clone();
                switch (clonedTreeMenuItem.Value)
                {
                    case "Delete":
                        {
                            bool enableCommand = ContentProvider.CheckDeletePermissions(pathToOpenedFolder);
                            if (enableCommand)
                            {// The path has Delete permisson ==> Delete is allowed
                                RadListViewContextMenu.Items.Add(clonedTreeMenuItem);
                            }
                        } break;
                    case "Upload":
                        {
                            bool enableCommand = ContentProvider.CheckWritePermissions(pathToOpenedFolder);
                            if (enableCommand)
                            {// The path has Write permisson ==> upload is allowed
                                RadListViewContextMenu.Items.Add(clonedTreeMenuItem);
                            }
                        } break;
                }
            }
     
            if (RadFileExplorer1.EnableOpenFile)
            {
                RadMenuItem openFileItem = new RadMenuItem();
                openFileItem.Value = "Open";
                openFileItem.Text = "Open";
                openFileItem.PostBack = false;
                openFileItem.EnableViewState = false;
                openFileItem.SelectedCssClass = "rfeNoClass";
                RadListViewContextMenu.Items.Add(openFileItem);
            }
     
            RadListViewContextMenu.Skin = RadFileExplorer1.Skin;
        }
     
        /// <summary>
        /// Returns an instance of FileBrowserContentProvider asociated with RadFileExplorer control. If a cusom provider is used, then this propperty should return
        /// an instance of that cusom provider.
        /// </summary>
        private FileBrowserContentProvider ContentProvider
        {
            get
            {
                if (object.Equals(this._contentProvider, null))
                {
     
                    this._contentProvider = new FileSystemContentProvider(this.Context,
                                                                                 RadFileExplorer1.Configuration.SearchPatterns,
                                                                                 RadFileExplorer1.Configuration.ViewPaths,
                                                                                 RadFileExplorer1.Configuration.UploadPaths,
                                                                                 RadFileExplorer1.Configuration.DeletePaths,
                                                                                 string.Empty,
                       
                }
     
                return this._contentProvider;
            }
        }
  2. Answer
    Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 25 Aug 2011 Link to this post

    Hi Patrick,

    Unfortunately, the provided code snippets are not runnable due to missing dependencies. I believe that you are using this live demo as a base but several methods are missing. Could you please open a formal support ticket and send a fully runnable sample project reproducing the problem so we can examine and debug it locally?

    In addition, responsible for the correct selection of the item inside the RadFileExplorer in the above mentioned example is the handler of the client-side click event of the panel wrapping the item, i.e:
    <asp:Panel ID="itemTemplateHolder" runat="server" CssClass="lvItemTemplate" ondblclick="openImageForPreview(this);" onclick="imageSelected(this);">
    If a JavaScript error is thrown on the page it may interrupt the correct execution of this handler and this may cause incorrect update of the CurrentFolder property.

    Regards,
    Dobromir
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top