This is a migrated thread and some comments may be shown as answers.

RadUploadItem Validate Question

3 Answers 93 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Michael Rogers
Top achievements
Rank 2
Michael Rogers asked on 22 Feb 2011, 08:43 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Alex Fidanov
Telerik team
answered on 25 Feb 2011, 01:53 PM
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!
0
Michael Rogers
Top achievements
Rank 2
answered on 25 Feb 2011, 03:21 PM
Hi Alex,

That worked thanks for your assistance.

Michael
0
danparker276
Top achievements
Rank 2
answered on 02 Apr 2014, 10:53 PM
Thank you for this solution, there were bugs in everything else.
Tags
Upload
Asked by
Michael Rogers
Top achievements
Rank 2
Answers by
Alex Fidanov
Telerik team
Michael Rogers
Top achievements
Rank 2
danparker276
Top achievements
Rank 2
Share this question
or