title: How do I download the PDF file generated given the file path in a Gridview?
body: <p>I want to write code to allow user to download a PDF by clicking the "Download" linkbutton in a Gridview when the filepath is given in the Gridview.</p>
  
  <p>Here's my code :</p>
  
  <p>aspx</p>
  
  <pre><code>&lt;asp:GridView ID="Gridview1" runat="server" ShowHeaderWhenEmpty="true" 
                              EmptyDataText="No Records Found" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="true" Font-Size="Small" Width="95%" onselectedindexchanged="Gridview1_SelectedIndexChanged"&gt;
      &lt;AlternatingRowStyle BackColor="White" /&gt;
  
      &lt;Columns&gt;
          &lt;asp:BoundField HeaderText="ID" DataField="id" DataFormatString="BSc/{0}" /&gt;
          &lt;asp:BoundField HeaderText="Company Name" DataField="ComName" /&gt;
          &lt;asp:BoundField HeaderText="Country Incorperated" DataField="Country" /&gt;
          &lt;asp:BoundField HeaderText="Date Created" DataField="Date" /&gt;
          &lt;asp:BoundField HeaderText="Path" DataField="Path"  /&gt;
  
          &lt;asp:TemplateField&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '&lt;%# Eval("Path") %&gt;' runat="server" OnClick = "DownloadFile"&gt;&lt;/asp:LinkButton&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  
      &lt;EditRowStyle BackColor="#2461BF" /&gt;
      &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
      &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
      &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
      &lt;RowStyle BackColor="#EFF3FB" /&gt;
      &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#F5F7FB" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#6D95E1" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#E9EBEF" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#4870BE" /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>aspx.cs</p>
  
  <pre><code>protected void DownloadFile(object sender, EventArgs e)
  {
      string filePath = (sender as LinkButton).CommandArgument;
      Response.ContentType = ContentType;
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
      Response.WriteFile(filePath);
      Response.End();
  }
  </code></pre>
  
  <p>I looked up the download code from this site : <a href="http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx" rel="nofollow">http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx</a></p>
  
  <p>I also get the file path like I'm supposed to, but thereafter nothing happens. There is no error either.</p>
  
  <p>Please help. Thanks in advance.</p>