Click here to Skip to main content
13,668,247 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 18 Jan 2012
Licenced CPOL

File download from GridView rows in ASP.NET 4.0

Rate this:
Please Sign up or sign in to vote.
There will be one Link button in the GridView row. On clicking the Link button, the Image displayed in that row gets downloaded.
In .aspx page:
  • Inside columns tag, add one template field like below:
<asp:TemplateField HeaderText="Screen Shot">
         <asp:Image ID="screenShot" runat="server" ImageUrl='<%# Eval("screenshot") %>' CssClass="image" AlternateText="No Screenshots available"/>
         <asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("screenshot") %>' CommandName="cmd">Download</asp:LinkButton>
  • Add one OnRowCommand="GridView1_RowCommand" event inside
    <asp:GridView tag.
In .cs page:
  • Define the event "GridView1_RowCommand" as follows:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
   if (e.CommandName == "cmd")
      string filename = e.CommandArgument.ToString();
      if (filename != "")
         string path = MapPath(filename);
         byte[] bts = System.IO.File.ReadAllBytes(path);
         Response.AddHeader("Content-Type", "Application/octet-stream");
         Response.AddHeader("Content-Length", bts.Length.ToString());
         Response.AddHeader("Content-Disposition", "attachment;   filename=" + filename);

Important Points

  • Example is given for Image download, but you can download files of any type by just giving the path in "CommandArgument" attribute
  • Q: What is ImageUrl='' ???
  • A: It is written to specify/evaluate the path of image that we got from dataset(column name in dataset is "screenshot").
  • Q: What is CommandArgument='' ???
  • A: Again CommandArgument takes the ImageUrl which is obtained from the dataset.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Software Developer Mindfire Solutions
India India
Official Facebook Page - Tadit Dash

Proud Indian | Microsoft MVP | CodeProject MVP | Speaker | Author | DZone Most Valuable Blogger | Community Expert | Moderator | jsfiddler | Cook | Dancer

Currently working @Mindfire Solutions.
Love to code and help guys to Trap the Bug.

My Tech Blog

Programming Community Profiles

jsfiddle | Stack Overflow

Other Blog Profiles

Mindfire CRM Team Blog | Mindfire Blog

Social Profiles

Facebook | Twitter | LinkedIn | Google+


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013
  7. </

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Sibeesh KV13-Nov-14 3:14
professionalSibeesh KV13-Nov-14 3:14 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Nov-14 3:29
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Nov-14 3:29 
GeneralRe: My vote of 5 Pin
Sibeesh KV13-Nov-14 3:35
professionalSibeesh KV13-Nov-14 3:35 
GeneralMy vote of 5 Pin
Jas2429-Jan-14 19:19
professionalJas2429-Jan-14 19:19 
GeneralRe: My vote of 5 Pin
Tadit Dash29-Jan-14 19:21
mvpTadit Dash29-Jan-14 19:21 
GeneralMy vote of 5 Pin
Debopam Pal18-Nov-13 15:32
memberDebopam Pal18-Nov-13 15:32 
GeneralRe: My vote of 5 Pin
Tadit Dash18-Nov-13 18:58
professionalTadit Dash18-Nov-13 18:58 
GeneralMy vote of 5 Pin
Michael Haephrati23-Feb-13 1:11
mvpMichael Haephrati23-Feb-13 1:11 
GeneralRe: My vote of 5 Pin
Tadit Dash23-Feb-13 1:20
memberTadit Dash23-Feb-13 1:20 
QuestionNice one Pin
K.Tapan Kumar20-Sep-12 2:04
memberK.Tapan Kumar20-Sep-12 2:04 
AnswerRe: Nice one Pin
Tadit Dash20-Sep-12 2:07
memberTadit Dash20-Sep-12 2:07 
GeneralMy vote of 5 Pin
Deepak Jena6-Aug-12 1:52
memberDeepak Jena6-Aug-12 1:52 
GeneralRe: My vote of 5 Pin
Tadit Dash6-Aug-12 1:53
memberTadit Dash6-Aug-12 1:53 
Questionhow can i view file with a hyper link? Pin
praveen_t_s30-Jun-12 20:39
memberpraveen_t_s30-Jun-12 20:39 
GeneralRe: how can i view file with a hyper link? Pin
Tadit Dash3-Jul-12 21:54
memberTadit Dash3-Jul-12 21:54 
Questionhow do i add hyperlink to show a document from grid view? Pin
praveen_t_s30-Jun-12 19:59
memberpraveen_t_s30-Jun-12 19:59 
AnswerRe: how do i add hyperlink to show a document from grid view? Pin
Tadit Dash3-Jul-12 20:43
memberTadit Dash3-Jul-12 20:43 
GeneralMy vote of 5 Pin
Srinivasaprabhu.N19-Apr-12 4:47
memberSrinivasaprabhu.N19-Apr-12 4:47 
GeneralRe: My vote of 5 Pin
Tadit Dash9-Jun-12 3:08
memberTadit Dash9-Jun-12 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180820.1 | Last Updated 2 May 2013
Article Copyright 2012 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid