Hi, everyone.
I have a file upload control inside a listview and what I would like to do is have the user be able to click the edit button and upload a new file that would take the old one's place. I would also like to delete the file off the server when they press the delete button.
So far, I have been messing with the onItemUpdating and onItemDeleting events but I can't seem to get the values from the item properly.
Here is my code from the onItemUpdating section. I used the code from Imar's book to create a guid and store the file in the project on the server. I need to get the the id of the item and the filename from the control so I can update the new filename in the database and I would like to get the old filename so I can delete it off the server.
Code:
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
// How can I get the id of the item here??? I need it so I can retrieve the old filename.
// I also need to retrieve the new file so I can compare them to see if they uploaded
// a new file. In that case I would use the old filename to use "File.Delete(Server.MapPath(FileName))"
// and delete it off the server
// I would also like to somehow load the original file into the fileupload here
// this gave me an error saying object is not an object instance
FileUpload FileUpload1 = (FileUpload)ListView1.FindControl("FileUpload1");
if ( old filename != new filename)
{
// starting path
string virtualFolder = "~/UploadedFiles/";
// map a physical path on server, create guid, and get file extension from upload control
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
// save the file on server and grab the entire path to store in database
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
string FileName = virtualFolder + fileName + extension;
DataSet1TableAdapters.PAST_EVENTSTableAdapter myQuery = new DataSet1TableAdapters.PAST_EVENTSTableAdapter();
myQuery.UpdateFileName(FileName, id);
ListView1.DataBind();
}
}
If someone can show me how to get this working than I should have no trouble getting the id from the onItemDeleting event so that I can also delete the file from the server when the item gets automatically deleted from the listview.
Thanks.
***P.S -
I know this is a lot to ask, but would it also be possible that when they press the edit button that the file upload control will already have the original file loaded into into it? The reason why I ask is because when they click edit, the file upload control is blank so if they just wanted to edit some other fields they would have to re-upload the file again. I just want it so that they could upload a new file if they want but if not, the original file would still be uploaded.