Here goes the sample implementation. I think you are kind of new to Asp.net and hence did a sample implementation that you can understand easily and develop your own implementation.
GridView in Aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="215px" OnRowCommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ImageField DataImageUrlField="ImageUrl" HeaderText="Image">
</asp:ImageField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Download" Text="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
And, the CodeBehind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public class Person
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string ImageUrl
{
get;
set;
}
public Person(int Id, string Name, string ImageUrl)
{
this.Id = Id;
this.Name = Name;
this.ImageUrl = ImageUrl;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList items = new ArrayList();
items.Add(new Person(1, "Peter", "~/images/ab.jpg"));
items.Add(new Person(2, "John", "~/images/cd.jpg"));
items.Add(new Person(3, "Shubho", "~/images/ef.jpg"));
GridView1.DataSource = items;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
string ImageUrl = (string)e.CommandArgument;
string physicalImagePath = Server.MapPath(ImageUrl);
if (File.Exists(physicalImagePath))
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(physicalImagePath));
Response.ContentType = "image/jpg";
Response.TransmitFile(physicalImagePath);
Response.Flush();
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
if (link != null)
{
link.CommandArgument = ((Person)e.Row.DataItem).ImageUrl.ToString();
}
}
}
}
This example basically binds some Person data to the gridview with images (Stored inside the web application folder's /images directory). Also, it binds a command argument to the download link in GridView's RowDataBound event (See CodeBehind). When user clicks the Download link, GridView's RowCommand event gathers the Image URL from the command argument of the LinkButton, reads the image from the physical location and writes in response for download.