0

I have a grid view showing the list of all the images from a folder with a checkboxes to select the images and download the selected ones.

But when I click on download. Nothing happens. I believe this problem have something to do with girdview being nested into the asp:content where the FindControl cannot find the image that is checked:

Note: When I take the GridView out of asp:content it starts working. But I need to have asp:content

list.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="cphFrontBody" runat="Server">
    <div class="inner-page-common">
        <div class="b-page">            
            <div class="fixed-wrapper">
                <h2>Images Found:</h2>
                <asp:GridView ID="GridView1" runat="server" 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="File Name" />
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="DownloadFiles" />
            </div>
        </div>
    </div>
</asp:Content>

list.aspx.cs

#region Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/upload/Products/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
                files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}
#endregion
public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
}

protected void DownloadFiles(object sender, EventArgs e)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
        zip.AddDirectoryByName("Products");
        foreach (GridViewRow row in GridView1.Rows)
        {
            if ((row.FindControl("chkSelect") as CheckBox).Checked)
            {
                string filePath = (row.FindControl("lblFilePath") as Label).Text;
                zip.AddFile(filePath, "Products");
            }
        }
        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();
    }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Evik Ghazarian
  • 1,803
  • 1
  • 9
  • 24

0 Answers0