You must Sign In to post a response.
  • Category: ASP.NET

    Dynamic CheckBox list from File System


    I've been put in charge of a project at the company I work for and I'm new to .NET. I just came across dotnetspider and thought it'd be a good place to get some help.

    I'm looking to dynamically create a check box list from a file system using Web Forms. Users will see this list once they hit the page and they will be able to select which files they want and then hit a button that begins a download of the selected files to their machine.

    I'm not quite sure how to get this list to populate. Which method to use?

    If anyone can point me in the direction of where to begin I would be very thankful.

  • #762763
    Assuming that the files are present in the Data directory in your website. Please use the below code to list all the files in the Data directory and display it as a Checkbox in a checkboxlist control.
    <asp:CheckBoxList runat="server" ID="chkFiles"></asp:CheckBoxList>

    FileInfo fi = new FileInfo(Server.MapPath("~/Data/"));
    FileInfo[] files = fi.Directory.GetFiles();

    foreach(var f in files)
    chkFiles.Items.Add(new ListItem(f.Name, f.FullName));

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #762764


    You can add the files to the listbox from a particular path or folder of files.

    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\sri\Desktop");
    FileInfo[] Files = dinfo.GetFiles("*.*");
    //string[] files = Directory.GetFiles(@"C:\Users\sri\Desktop");
    foreach (FileInfo f in Files)

    Implement the checkbox selected index changed event to download the selected file.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762793
    Thanks for the help @sridhar thota

    I'm now trying to cycle through the selected items in the CheckBoxList and have them zipped and downloaded to the users machine.

    I've got this so far, but I'm unsure if it's correct:

    foreach (ListItem listItem in CheckBoxList1.Items)
    if (listItem.Selected)
    //download these files

    Am I on the right path? How do you bundle selected files to a zip folder and begin a download to the user's machine?

    Thank you.

  • Sign In to post your comments