RadUploadItem Validate Question

4 posts, 1 answers
  1. Michael Rogers
    Michael Rogers avatar
    22 posts
    Member since:
    Mar 2010

    Posted 22 Feb 2011 Link to this post

    I'm validating an additional text box on the uploaders upload item validate event by adding the following event handler.

    RadUpload1.AddHandler(RadUploadItem.ValidateEvent, New UploadValidateEventHandler(AddressOf OnValidate))

    I then check the text box and display a message box and cancel the upload if the text box is empty, i want to be able to do this without clearing the file while still cancel the upload.  I've tried manually adding the file back but i get an exception when calling the "PrepareSelectedFilesForUpload" if i don't call this method the files do not show up in the UI.  What is the best way to handle this?

    Private Sub OnValidate(ByVal sender As Object, ByVal e As UploadValidateEventArgs)
     
        If txtDesc.Text.Trim = String.Empty Then
     
            'create a new dialog paramater to display
            Dim dp As DialogParameters
            dp.Content = "A File Description is Required"
            dp.Header = "File Description"
     
            'save and readd selected file
            Dim file As RadUploadSelectedFile = RadUpload1.CurrentSession.SelectedFiles(0)
            RadUpload1.CancelUpload()
            RadUpload1.CurrentSession.SelectedFiles.Add(file)
            RadUpload1.PrepareSelectedFilesForUpload()
     
            'display allert
            RadWindow.Alert(dp)
     
        End If
     
    End Sub

    Message: Unhandled Error in Silverlight Application
    Code: 4004   
    Category: ManagedRuntimeError      
    Message: System.InvalidOperationException: Operation is not valid due to the current state of the object.
       at System.Windows.PresentationFrameworkCollection`1.CollectionEnum`1.MoveNext()
       at Telerik.Windows.Controls.RadUpload.ValidateItems()
       at Telerik.Windows.Controls.RadUpload.StartUpload()
       at Telerik.Windows.Controls.RadUpload.<.ctor>b__0(Object p)
       at Telerik.Windows.Controls.DelegateCommand.Execute(Object parameter)
       at Telerik.Windows.Controls.RadButton.ExecuteCommand()
       at Telerik.Windows.Controls.RadButton.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)    

    Thanks
    Michael
  2. Answer
    Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 25 Feb 2011 Link to this post

    Hi Michael Rogers,

    Below is a sample code snippet for this scenario. It is very close to what you already have. Please try this and let us know if that works in your scenario.

    List<FileInfo> files = new List<FileInfo>();
     void OnValidate(object sender, UploadValidateEventArgs e)
     {
         if (string.IsNullOrEmpty(tbxDesc.Text)) // validate
         {
             e.Cancel = true;
             SaveFiles();
             radUpload.CancelUpload();
             this.Dispatcher.BeginInvoke(() =>
                 {
                     ReUploadItems();
                     radUpload.PrepareSelectedFilesForUpload();
                 });
         }
     }
     private void ReUploadItems()
     {
         //// put the files back in the upload
         foreach (var item in files)
         {
             RadUploadSelectedFile f = new RadUploadSelectedFile(item);
             radUpload.CurrentSession.SelectedFiles.Add(f);
         }   
     }
     private void SaveFiles()
     {
         //// get the files from the upload.
         files.Clear();
         foreach (var item in radUpload.CurrentSession.SelectedFiles)
         {
             files.Add(item.File);
         }
     }


    Greetings,
    Alex Fidanov
    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. DevCraft banner
  4. Michael Rogers
    Michael Rogers avatar
    22 posts
    Member since:
    Mar 2010

    Posted 25 Feb 2011 Link to this post

    Hi Alex,

    That worked thanks for your assistance.

    Michael
  5. danparker276
    danparker276 avatar
    389 posts
    Member since:
    Aug 2010

    Posted 02 Apr 2014 Link to this post

    Thank you for this solution, there were bugs in everything else.
Back to Top