ASP.NET - grid view file upload examples needed

Asked By Ashok Kumar on 21-Nov-10 06:39 AM
Hi friends!

I am new to ASP.NET
i am learning how to use file uploads in Gridview!
can any one plz send me any good examples for that...
Thanks in Adv

AShok
Mihir Soni replied to Ashok Kumar on 21-Nov-10 06:58 AM
Hello,

You can add fileupload control in Gridview Edit Template for upload the image you can use RowCommand event of gridview.

First add asp.net control in gridview edit template and then add following code in RowCommand event of gridview.


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(GridView1.EditIndex == -1) return;
FileUpload fileUpLoad = GridView1.Rows[GridView1.EditIndex].FindControl("FileUpload1") as FileUpload;
string fileName = fileUpLoad.FileName;
string fullPath = Path.GetFullPath(fileName);
fileUpLoad.SaveAs(fullPath); //Done
}


I hope this will help you.:)
sri sri replied to Ashok Kumar on 21-Nov-10 09:18 AM
hi,

check the below link for file upload control in gridview.

http://vinayakshrestha.wordpress.com/2008/02/19/aspnet-gridview-multiple-files-upload/

Anoop S replied to Ashok Kumar on 21-Nov-10 03:12 PM
First you need to handle the RowUpdating event instead of RowUpdated. Then you need to find a reference to the FileUpload control on that row.

IMPORTANT: You need to know the ordinal position of the column where the control is located. In my example, I set it to 0, assuming it is the first column. Otherwise, you would need to through the Cells collection to find it.

protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  GridViewRow row = gridView.Rows[e.RowIndex];
  FileUpload fileUpload = row.Cells[0].FindControl("fileUpload1") as FileUpload;
  if (fileUpload != null && fileUpload.HasFiles)
  {
    fileUpload.SaveAs(Server.MapPath("images/hardware/" + fileUpload.FileName));
  }
}
Manasi replied to Anoop S on 15-Dec-10 11:12 PM
YOUR POST WAS REALLY HELPFUL.

THANKS