在更新事件期间使用entitydatasource从表中获取旧值(C#ASP.NET)

时间:2016-07-20 15:45:32

标签: c# asp.net

我正在尝试将新图像上传到服务器,然后从服务器上删除旧图像,但是,当使用下面的代码时,用于删除文件的字符串为空,我将如何实现此目的?

代码:

protected void BandEditorEntityDataSource_Updating(object sender, EntityDataSourceChangingEventArgs e)
{
    Band myBand = (Band)e.Entity;
    FileUpload bandlogo = (FileUpload)BandDetailsView.FindControl("BandLogoEditFileUpload");
    if (bandlogo.HasFile)
    {
        string oldbandlogopath = myBand.BandLogoUrl; //<- Returns null value
        myBand.BandLogoUrl = fileupload(bandlogo, "~/Images/Uploaded/Band/Logo/");
        string oldbandlogodelfile = Server.MapPath(oldbandlogopath);
        System.IO.File.Delete(oldbandlogodelfile);
    }
}

文件上传方法(它似乎有效,因为它将文件上传到服务器但我不确定数据库是否实际接收更新,因为它会抛出错误):

private string fileupload(FileUpload fileUploadControl, string virtualFolders)
{
    string virtualFolder = virtualFolders;
    string physicalFolder = Server.MapPath(virtualFolder);
    string fileName = Guid.NewGuid().ToString();
    string extension = System.IO.Path.GetExtension(fileUploadControl.FileName);
    fileUploadControl.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
    string returnUrl = virtualFolder + fileName + extension;
    return returnUrl;
}

1 个答案:

答案 0 :(得分:0)

我相信在更新开始时你应该有两个方法_Updating,在更新完成时你应该有_Updated。希望您能在_Updated方法

中看到您的网址