I'm trying to upload an image in situe using a DetailsView but I need to store the filename when I commit to the DB. Here's what I've got so far:
protected void UploadProductImage(object sender, EventArgs args)
{
if (HttpContext.Current.Request.Files.Count > 0)
{
//Grab uploaded file
HttpPostedFile productImageFile = HttpContext.Current.Request.Files[0];
//TODO: Handle saving and resizing the file
//Here's my issue
TextBox fileNameTextBox = DetailsView1.Rows[4].Cells[1].Controls[0] as TextBox;
//display the preview image
Image img = DetailsView1.FindControl("UploadedImagePreview") as Image;
img.ImageUrl = string.Format("{0}{1}", ConfigurationManager.AppSettings["ProductImagePath"], fileNameTextBox.Text);
img.Visible = true;
}
}
My concern is that I don't really want the bound image fields to be editable by the user but the only way I've found to be able to edit them programatically is to have them editable and access them in the rather ugly DetailsView > Rows > Cells method which I'm sure will bite me at some later stage.
I'm wondering if there's a way to access the bound data away from the DetailsView, if that's even possible or a plausible solution...
I've done something similar to this before but have always known the ID of a pre-existing record so have just been able to run a SQL update against the record befre the OnUpdating methos is invoked.
Does anyone have sueestions or best practices for this?