You can use this in the following order:
<asp:TemplateField HeaderText="UploadImage"> <ItemTemplate> <asp:Image ImageUrl="~/images/1.jpg" runat="server" ID="image" /> // shown only when not in edit mode </ItemTemplate> <EditItemTemplate> <asp:FileUpload ID="FileUpload1" runat="server" /> // shown only in edit mode </EditItemTemplate> </asp:TemplateField>
Finally, to enter edit mode, enable the following:
<asp:commandField showEditButton="true" showCancelButton="true">
Then add two events as follows:
protected void GridView1_RowEditing(object sender, GridViewUpdateEventArgs e) { gvwID.EditIndex=e.NewEditIndex; BindGrid(); } protected void GridView1_RowCancelEdit(object sender, GridViewUpdateEventArgs e) { gvwID.EditIndex=-1; BindGrid(); }
The FileUpload element will not automatically save the downloaded file. To save the file, you need to use the SaveA method for FileUpload. Before you can use the SaveAs method, you need to get an instance of the FileUpload control for the line you are editing. To get an instance of the control, you can hook into the RidUpdating GridViews event. The following code will receive an instance of the FileUpload control and save the downloaded file:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int RowID=Convert.ToInt32(gvwID.DataKeys[e.RowIndex].value); FileUpload fileUpload = GridView1.Rows[e.RowIndex].FindControl("FileUpload1") as FileUpload; if(fileUpload.HasFile) { fileUpload.SaveAs(System.IO.Path.Combine(Server.MapPath("Images"), fileUpload.FileName));
Hope this helps ...
Harun source share