Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 24th, 2012, 02:27 PM
Authorized User
 
Join Date: Dec 2011
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
Default I am having trouble with the file upload control inside a listview.

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.

Last edited by IceThatJaw; March 24th, 2012 at 03:06 PM..
 
Old March 27th, 2012, 12:16 AM
Authorized User
 
Join Date: Dec 2011
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Ok, so I solved my problem by removing the automatic update button and adding my own update button in it's place that called a method that did all the code behind I needed to accomplish the task.

Let this thread die gracefully! lol





Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload file in listview diegoorjuela BOOK: Beginning ASP.NET 4 : in C# and VB 3 September 1st, 2011 01:03 PM
Clear the filepath details of File Upload Control after saving a file DineshGirij008 C# 2005 0 March 7th, 2009 03:52 AM
file upload control iamsunil2006 .NET Framework 2.0 1 January 21st, 2008 05:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.