Using Ionic.Zip - Download Code from GridView in ASP.NET using c#

We bind a PDF in a GridView and then write download code using the Ionic.Zip library.

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (gridview_demo).

Step 2

In Solution Explorer you get your empty website, then add a web form and a SQL Server database as in the following.

For Web Form:

gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it gridview_demo.aspx.

For SQL Server Database

gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder).

DATABASE CHAMBER

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.

Table tbl_data (Don't Forget to make ID as IS Identity -- True)

table design

Design Chamber

Step 4

Now open your gridview_demo.aspx file, where we create our design so that our application works.

Gridview_demo.aspx.cs:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             font-size: medium;  
  12.             text-decoration: underline;  
  13.             color: #FF6600;  
  14.         }  
  15.         .style2  
  16.         {  
  17.             color: #FF6600;  
  18.         }  
  19.     </style>  
  20. </head>  
  21. <body>  
  22.     <form id="form1" runat="server">  
  23.     <div>  
  24.       
  25.         <br />  
  26.         <table style="width:100%;">  
  27.             <tr>  
  28.                 <td class="style1">  
  29.                     <strong>Download Task in Gridview in asp.net</strong></td>  
  30.                 <td class="style2">  
  31.                      </td>  
  32.                 <td class="style2">  
  33.                      </td>  
  34.             </tr>  
  35.             <tr>  
  36.                 <td>  
  37.       
  38.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   
  39.             DataKeyNames="id" onrowcommand="GridView1_RowCommand" BackColor="White"   
  40.                         BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"   
  41.                         GridLines="Vertical">  
  42.             <AlternatingRowStyle BackColor="#DCDCDC" />  
  43.             <Columns>  
  44.                 <asp:TemplateField HeaderText="Book No.">  
  45.                     <EditItemTemplate>  
  46.                         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>  
  47.                     </EditItemTemplate>  
  48.                     <ItemTemplate>  
  49.                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>  
  50.                     </ItemTemplate>  
  51.                 </asp:TemplateField>  
  52.                 <asp:TemplateField HeaderText="Download">  
  53.                     <EditItemTemplate>  
  54.                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  55.                     </EditItemTemplate>  
  56.                     <ItemTemplate>  
  57.                         <asp:LinkButton ID="lnkbtn" runat="server" CommandName="Download" Text='<%#Bind("book")%>'></asp:LinkButton>  
  58.                     </ItemTemplate>  
  59.                 </asp:TemplateField>  
  60.             </Columns>  
  61.             <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  62.             <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
  63.             <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  64.             <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
  65.             <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  66.             <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  67.             <SortedAscendingHeaderStyle BackColor="#0000A9" />  
  68.             <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  69.             <SortedDescendingHeaderStyle BackColor="#000065" />  
  70.         </asp:GridView>  
  71.       
  72.                 </td>  
  73.                 <td>  
  74.                      </td>  
  75.                 <td>  
  76.                      </td>  
  77.             </tr>  
  78.             <tr>  
  79.                 <td>  
  80.                      </td>  
  81.                 <td>  
  82.                      </td>  
  83.                 <td>  
  84.                      </td>  
  85.             </tr>  
  86.         </table>  
  87.         <br />  
  88.         <br />  
  89.         <br />  
  90.         <br />  
  91.         <br />  
  92.       
  93.     </div>  
  94.     </form>  
  95. </body>  
  96. </html>  
Your design will look like this:

design

Code Chamber

Step 5

Open your gridview_demo.aspx.cs and write some code so that our application works.

Gridview_demo.aspx.cs:

First you need to download the Ionic.Zip.dll file from the codeplex, but here in my source you will get your DLL file and in my account also there is this file you can download it from there too. Then add the namespaces as in the following image.

Namespace

The following is the code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using Ionic.Zip;  
  10. using System.IO;  
  11.   
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         if (!Page.IsPostBack)  
  17.         {  
  18.             refreshdata();  
  19.   
  20.         }  
  21.     }  
  22.   
  23.     public void refreshdata()  
  24.     {  
  25.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  26.         SqlCommand cmd = new SqlCommand("select * from tbl_data",con);  
  27.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  28.         DataTable dt = new DataTable();  
  29.         sda.Fill(dt);  
  30.   
  31.         GridView1.DataSource=dt;  
  32.         GridView1.DataBind();  
  33.           
  34.       
  35.     }  
  36.   
  37.     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  38.     {  
  39.         if (e.CommandName=="Download")  
  40.         {  
  41.             using( ZipFile zpf = new ZipFile())  
  42.             {  
  43.                 LinkButton lnk = (e.CommandSource) as LinkButton;  
  44.   
  45.                 string fileName1 = lnk.Text;  
  46.                 string fileName = e.CommandSource.ToString();  
  47.                 string filePath = Server.MapPath("~/Files/" + fileName1);  
  48.                 zpf.AddFile(filePath, "Files");  
  49.                   
  50.                 Response.Clear();  
  51.                 Response.AddHeader("Content-Disposition""attachment; filename=DownloadedFile.zip");  
  52.                 Response.ContentType = "application/zip";  
  53.                 zpf.Save(Response.OutputStream);  
  54.                 Response.End();  
  55.               
  56.               
  57.             }  
  58.         }  
  59.     }  
  60. }  
Output Chamber

Output

I hope you like this. Have a good day. Thank you for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all