I want to use a fileupload control in an editable WebUserControl in conjunction with a RadGrid.
I have put everything together, but when I try to upload the file it says.
Index was out of range. Must be non-negative and less than the size of the collection.
This is strange because when copy and paste the code in non-user control (regular aspx page), it works fine. I have tried the code in the WebUserControl which is called by the RadGrid Parent page, and I have also tried referencing the WebUserControl from within the Parent page, yet the message is the same either way.
Here is the code on my Parent RadGrid page which calls the UserControl with the FileUpload control in it:
I have put everything together, but when I try to upload the file it says.
Index was out of range. Must be non-negative and less than the size of the collection.
This is strange because when copy and paste the code in non-user control (regular aspx page), it works fine. I have tried the code in the WebUserControl which is called by the RadGrid Parent page, and I have also tried referencing the WebUserControl from within the Parent page, yet the message is the same either way.
Here is the code on my Parent RadGrid page which calls the UserControl with the FileUpload control in it:
protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID); |
RadUpload RadUpload1 = userControl.FindControl("RadUpload1") as RadUpload; |
Repeater repeaterResults = userControl.FindControl("repeaterResults") as Repeater; |
Label labelNoResults = userControl.FindControl("labelNoResults") as Label; |
string targetFolder = Server.MapPath(RadUpload1.TargetFolder); |
UploadedFile file = RadUpload1.UploadedFiles[0]; |
string uniqueFileName = System.Guid.NewGuid().ToString(); |
string targetFileName = Path.Combine(Server.MapPath("~/shared/videos/members"), uniqueFileName + file.GetExtension()); |
file.SaveAs(targetFileName); |
//delete unique filename if database insert fails |
if (RadUpload1.UploadedFiles.Count > 0) |
{ |
repeaterResults.DataSource = RadUpload1.UploadedFiles; |
repeaterResults.DataBind(); |
labelNoResults.Visible = false; |
repeaterResults.Visible = true; |
} |
else |
{ |
labelNoResults.Visible = true; |
repeaterResults.Visible = false; |
} |