Click here to Skip to main content
14,243,903 members

File download from GridView rows in ASP.NET 4.0

Rate this:
4.91 (16 votes)
Please Sign up or sign in to vote.
4.91 (16 votes)
2 May 2013CPOL
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. </

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
Tom Marvolo Riddle29-Jan-14 19:19
professionalTom Marvolo Riddle29-Jan-14 19:19 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jan-14 19:21
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jan-14 19:21 
GeneralMy vote of 5 Pin
Debopam Pal18-Nov-13 15:32
professionalDebopam Pal18-Nov-13 15:32 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)18-Nov-13 18:58
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)18-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 Dash (ତଡିତ୍ କୁମାର ଦାଶ)23-Feb-13 1:20
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)23-Feb-13 1:20 
QuestionNice one Pin
Mr. Tapan Kumar20-Sep-12 2:04
memberMr. Tapan Kumar20-Sep-12 2:04 
AnswerRe: Nice one Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)20-Sep-12 2:07
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)20-Sep-12 2:07 
GeneralMy vote of 5 Pin
Pro Idiot6-Aug-12 1:52
memberPro Idiot6-Aug-12 1:52 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)6-Aug-12 1:53
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)6-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 Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-Jul-12 21:54
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-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 Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-Jul-12 20:43
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-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 Dash (ତଡିତ୍ କୁମାର ଦାଶ)9-Jun-12 3:08
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)9-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.

Posted 18 Jan 2012


17 bookmarked