Hi,
i am reading folder files on basis of date, how to download and delete on click of gridview.
as mentiond in url
http://www.aspsnippets.com/Articles/Display-list-of-files-from-Server-folder-in-ASPNet-GridView.aspx
<asp:GridView ID="GridView1" CssClass="grid" GridLines="None" ShowFooter="true"
AllowPaging="true" PageSize="5"
AutoGenerateColumns="false"
OnPageIndexChanging="GridView1_PageIndexChanging" runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Length">
<ItemTemplate>
<asp:Label ID="lblLen" runat="server" Text='<%#Eval("Length")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Extention">
<ItemTemplate>
<asp:Label ID="lblFileType" runat="server" Text='<%#Eval("Extension")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Creation Date & Time">
<ItemTemplate>
<asp:Label ID="lblDateTime" runat="server" Text='<%#Eval("CreationTime")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label Text="" ID="lblMsg" runat="server"></asp:Label>
<asp:ListBox ID="drop1" Rows="3" runat="server">
<asp:ListItem Selected="true">All</asp:ListItem>
<asp:ListItem>pdf</asp:ListItem>
<asp:ListItem>jpg</asp:ListItem>
<asp:ListItem>png</asp:ListItem>
<asp:ListItem>txt</asp:ListItem>
<asp:ListItem>doc</asp:ListItem>
</asp:ListBox>
<input type="button" id="btShowFiles" onserverclick="btShowFiles_Click" value="Show Files" runat="server" />
How to enable delete and download in below code, as all code works fine
protected void btShowFiles_Click(object sender, EventArgs e)
{
ViewState["FileType"] = drop1.SelectedValue; // GET THE FILE TYPE.
GetFilesFromFolder();
}
// GRIDVIEW PAGING.
protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GetFilesFromFolder();
}
private void GetFilesFromFolder()
{
// GET A LIST OF FILES FROM A SPECIFILED FOLDER.
DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("media\\"));
DateTime from_date = DateTime.Now.AddYears(-35);
DateTime to_date = DateTime.Now.AddDays(1);
FileInfo[] listfiles = objDir.GetFiles("*." + ((string)ViewState["FileType"] != "All" ? ViewState["FileType"] : "*")).Where(file => file.CreationTime >= from_date && file.CreationTime <= to_date).ToArray();
if (listfiles.Length > 0)
{
// BIND THE LIST OF FILES (IF ANY) WITH GRIDVIEW.
GridView1.Visible = true;
GridView1.DataSource = listfiles;
GridView1.DataBind();
lblMsg.Text = listfiles.Length + " files found";
}
else
{
GridView1.Visible = false;
lblMsg.Text = "No files found";
}
}