Hello, I have a gridview that shows an image as part of one of its columns. In Edit mode, I would like to let the user have the ability to upload a new image file, so I am using the FileUpload control in the edit portion of the template.
I have an event to capture this i believe:
protected void GridVew1_RowUpdated(object sender, GridViewUpdateEventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("images/hardware/" + FileUpload1.FileName));
}
}
I do not know how to call the control correctly though... How is this functionality coded?
-
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)); } }Kolten : Thanks the file is uploading perfectly. I see the Cells[0], but it seems to find the control no matter what I set this to... not sure why. -
If I understand what you are doing here you will have to find the control in the row
so in VB some thing like this
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim aRow As GridViewRow = Me.GridView1.Rows(e.RowIndex) dim xFileUpload as fileupload = CType(aRow.FindControl("FileUpload1"), FileUpload) xFileUpload. save file etc etc etc End SubCaveat - if this is wrong I would love to see a better way to do this!
0 comments:
Post a Comment