0

I have a grid view showing product images:

<form id="form1" runat="server">
<asp:Button ID="btnDownload" runat="server" CssClass="dist-down-button" Text="Download Selected" OnClick="DownloadFiles" />
<asp:Button ID="Button1" runat="server" CssClass="dist-down-button" Text="Download All" OnClick="DownloadAll" />
<asp:GridView ID="GridView1" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="True" OnPageIndexChanging="datagrid_PageIndexChanging" AutoGenerateColumns="false" EmptyDataText="No files available">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:Label ID="lblFilePath" runat="server" Text='<%# Eval("Value") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Text" HeaderText="Image Name" />
</Columns>
</asp:GridView>
</form>

Binding function: (matches all the files in the products folder with the product images list from database and show matching products)

protected void BindData()
{
    images = GetProductImages();
    string[] filePaths = Directory.GetFiles(Server.MapPath("~/upload/Products/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in filePaths)
    {
        if (images.IndexOf(Path.GetFileName(filePath)) > -1)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
    }
    GridView1.DataSource = files;
    GridView1.DataBind();
}
protected void datagrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindData();
}

Download All function: (look for the gridview list and download all the files listed)

protected void DownloadAll(object sender, EventArgs e)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
        zip.AddDirectoryByName(ProductUrl);
        foreach (GridViewRow row in GridView1.Rows)
        {
            string filePath = (row.FindControl("lblFilePath") as Label).Text;
            zip.AddFile(filePath, ProductUrl);
        }
        Response.Clear();
        Response.BufferOutput = false;
        string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
        zip.Save(Response.OutputStream);
        Response.End();
    }
}

When I click on Download All button it was downloading all the files correctly. BUT after I added the pagination the download all is now only downloading the current page.

Note: The checkbox is for selected download function. As you see I am not looking for checkboxes checked in the DownloadAll function.

Does anyone know why is this happening eventhough I am not looking for any checkbox in my function?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Evik Ghazarian
  • 1,803
  • 1
  • 9
  • 24

2 Answers2

1

You could turn off paging and re-binding the GridView before exporting:

protected void DownloadAll(object sender, EventArgs e)
{
    GridView1.AllowPaging = false;
    BindData();

    // ... your code

    GridView1.AllowPaging = true;
    BindData();
}
haldo
  • 14,512
  • 5
  • 46
  • 52
  • as simple as that. Thank you. One more question; Isn't this gonna slow down the application since the BindData() is being called multiple times? bind data is pulling out a large list of data. – Evik Ghazarian Sep 20 '19 at 15:38
  • It's Depend's on your data...For e.g grid view has 100 records ....Then it will take more...If grid view has 50 records it will take less time as compare to 100 record s – THE LIFE-TIME LEARNER Sep 20 '19 at 16:20
  • Yes, there will be some performance loss having to re-bind every time the grid is exported. Notice that the gridview is re-bound each time the page is updated. You could use Linq `Skip(page * pageSize)` and `Take(pageSize)` if the order the files load in is the same each time. @EvikGhazarian – haldo Sep 25 '19 at 10:24
1

In Download All function, you have clearly writing that...take file path only from gridview.. After the pagination...so it only take Current Page Gridview data...not pickup all the data..

foreach (GridViewRow row in GridView1.Rows)
        {
            string filePath = (row.FindControl("lblFilePath") as Label).Text;
            zip.AddFile(filePath, ProductUrl);
        }

Solution:-

So, Whenever you binding the Gridview that time hold all data in ViewState..

gridView.DataSource = dt;
gridView.DataBind();

ViewState["DownLoadGridData"]=dt as DataTable;

DataTable dt =  ViewState["DownLoadGridData"] as DataTable;

foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn column in dt.Columns)
    {
        if (row[FilePath] != null) // This will check the null values also (if you want to check).
        {
               // then add filepath 
           string filePath = row[FilePath].ToString();
           zip.AddFile(filePath, ProductUrl);

        }
     }
}

Note:- you can develop your own logic also

THE LIFE-TIME LEARNER
  • 1,476
  • 1
  • 8
  • 18