Click here to Skip to main content
Click here to Skip to main content

File download from GridView rows in ASP.NET 4.0

, 2 May 2013
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
Proud Indian | Microsoft MVP | CodeProject MVP | Author | Blogger | Community Expert | Moderator | jsfiddler | Cricketer | 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. Microsoft MVP 2014
  2. Code Project MVP 2014
  3. Star Achiever of the Month December 2013
  4. Mindfire Techno Idea Contest 2013 Winner
  5. Star of the Month July 2013
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

GeneralMy vote of 5 PinprofessionalJas2429-Jan-14 19:19 
GeneralRe: My vote of 5 PinmvpTadit Dash29-Jan-14 19:21 
GeneralMy vote of 5 PinmemberDebopam Pal18-Nov-13 15:32 
GeneralRe: My vote of 5 PinprofessionalTadit Dash18-Nov-13 18:58 
GeneralMy vote of 5 PinmvpMichael Haephrati23-Feb-13 1:11 
GeneralRe: My vote of 5 PinmemberTadit Dash23-Feb-13 1:20 
QuestionNice one PinmemberK.Tapan Kumar20-Sep-12 2:04 
AnswerRe: Nice one PinmemberTadit Dash20-Sep-12 2:07 
GeneralMy vote of 5 PinmemberDeepak Jena6-Aug-12 1:52 
GeneralRe: My vote of 5 PinmemberTadit Dash6-Aug-12 1:53 
Questionhow can i view file with a hyper link? Pinmemberpraveen_t_s30-Jun-12 20:39 
GeneralRe: how can i view file with a hyper link? PinmemberTadit Dash3-Jul-12 21:54 
Questionhow do i add hyperlink to show a document from grid view? Pinmemberpraveen_t_s30-Jun-12 19:59 
AnswerRe: how do i add hyperlink to show a document from grid view? PinmemberTadit Dash3-Jul-12 20:43 
GeneralMy vote of 5 PinmemberSrinivasaprabhu.N19-Apr-12 4:47 
GeneralRe: My vote of 5 PinmemberTadit Dash9-Jun-12 3:08 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 2 May 2013
Article Copyright 2012 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid