protected void lbnDelete_Click(object sender, EventArgs e)
        {
            if (this.Request.QueryString["File"] != null)
            {
                _FileID = Int32.Parse(this.Request.QueryString["File"]);
            }

            FileConfigurationController ctrlConfig = new FileConfigurationController();
            var configs = ctrlConfig.GetItems(PortalId) as List<FileConfiguration>;
            if (_FileID == configs[0].RootSharedFolderID || _FileID == configs[0].RootGroupsFolderID || _FileID == configs[0].RootUsersFolderID)
            {
                DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, "Error: Can not delete a protected folder", DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
                lbnDelete.Visible = false;
                lbnOK.Visible = false;
                lbnCancel.Visible = true;
                return;
            }

            FileController fileCtrl = new FileController();
            File file = fileCtrl.Get(_FileID);
            _filesToDelete = new List<File>();

            DeleteChildren(file);

            foreach (File toDelete in _filesToDelete)
            {

                fileCtrl.Delete(toDelete);
            }

            string strMessage;
            if (_permanentlyDeletedFilesCount > 0)
            {
                strMessage = String.Format("Permanently deleted {0} item(s).", _permanentlyDeletedFilesCount);
            }
            else
            {
                strMessage = String.Format("Successfully deleted {0} item(s) and skipped {1} item(s).", _deletedFilesCount, _notDeletedFilesCount);
            }

            DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, strMessage, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);

            lbnDelete.Visible = false;
            lbnOK.Visible = true;
            lbnCancel.Visible = false;
        }
        private void DeleteChildren(File file)
        {
            //delete ALL children items where user has delete permission

            FileController fileCtrl = new FileController();
            List<File> files = fileCtrl.GetItems(file.ID) as List<File>;
            foreach (File childFile in files)
            {
                if (file.ItemType == 0)
                {
                    DeleteChildren(childFile);
                }
            }
                //send to trash if not folder
                if (file.ItemType != 0 && file.CanDelete(UserId, PortalId, PortalSettings.AdministratorRoleId, false))
                {
                    //send to trash if not there already
                    if (!file.IsInTrash)
                    {
                        file.IsInTrash = true;
                        fileCtrl.Update(file);
                        #region "Audit"
                        //Audit: Create Item
                        AuditController ctrlAudit = new AuditController();
                        Audit deleteAudit = new Audit() { EventDate = file.LastModifiedDate, EventDetails = "", EventName = "Deleted", FileID = file.ID, UserID = UserId };
                        ctrlAudit.Create(deleteAudit);
                        #endregion
                        _deletedFilesCount++;
                        return;
                    }

                    //if file is in trash, delete permissions and file
                    if (file.IsInTrash)
                    {
                        PermissionController permCtrl = new PermissionController();

                        //delete versions and version permissions
                        List<File> versions = fileCtrl.GetItems(file.ID) as List<File>;
                        foreach (File version in versions)
                        {
                            List<Permission> versionPermissions = permCtrl.GetItems(version.ID) as List<Permission>;
                            for (int i = 0; versionPermissions.Count - 1 > i; i++)
                            {
                                permCtrl.Delete(versionPermissions[i]);
                            }
                            fileCtrl.Delete(version);
                        }

                        //delete file permissions
                        List<Permission> folderPermissions = permCtrl.GetItems(file.ID) as List<Permission>;
                        for (int i = 0; folderPermissions.Count - 1 >= i; i++)
                        {
                            permCtrl.Delete(folderPermissions[i]);
                        }

                        //delete file instead of sending to trash
                        _filesToDelete.Add(file);
                        //fileCtrl.Delete(file);

                        _permanentlyDeletedFilesCount++;
                        return;
                    }
                }

                //check if any items exist under folder and delete permission before deleting
                if (file.ItemType == 0 && file.CanDelete(UserId, PortalId, PortalSettings.AdministratorRoleId, false))
                {
                    //get current child count
                    List<File> childFiles = fileCtrl.GetItems(file.ID) as List<File>;
                    int childFilesCount = 0;

                    //count child items not in trash
                    foreach (File childFileCount in childFiles)
                    {
                        if (!childFileCount.IsInTrash)
                        {
                            childFilesCount++;
                        }
                    }

                    //no child items, permanently delete folder
                    if (childFilesCount == 0)
                    {
                        //delete folder permissions
                        PermissionController permCtrl = new PermissionController();
                        List<Permission> folderPermissions = permCtrl.GetItems(file.ID) as List<Permission>;
                        for (int i = 0; folderPermissions.Count - 1 >=  i; i++)
                        {
                            permCtrl.Delete(folderPermissions[i]);
                        }

                        //delete folder instead of sending to trash
                        _filesToDelete.Add(file);
                        //fileCtrl.Delete(file);

                        _deletedFilesCount++;
                        return;
                    }
                    else
                    {
                        _notDeletedFilesCount++;
                        return;
                    }
                }
                _notDeletedFilesCount++;
        }