I have a placeholder control where i have added the files from my folder. I have also binded a check box with each file How i can download the files if checkbox is clicked.

string strDir = Request.QueryString["d"] == null ? FileBrowerProperty.IntialPath : Request.QueryString["d"];

//read the directory
DirectoryInfo DirInfo = new DirectoryInfo(strDir);
//read the subdirectories inside the parent directory
DirectoryInfo[] subDirs = DirInfo.GetDirectories();
//read the files in the parent directory
FileInfo[] Files = DirInfo.GetFiles();

foreach (DirectoryInfo di in subDirs)
{
    ListItem listitem1 = new ListItem(di.FullName);
    // <a href='more.php' onclick='show_more_menu()'>More >>></a>
    //add the directory info to our table 
    LiteralControl lcFolders = new LiteralControl(@"<TR><TD style='WIDTH: 20px'><input type='checkbox' id='"
       + @di.FullName
       + "'>Directory</TD><TD style='WIDTH:350px'><a href='webform1.aspx?d=" 
       + @di.FullName + "'>" + di.Name + "</a></TD><TD style='WIDTH: 150px'>" 
       + di.CreationTime + "</TD><TD style='WIDTH: 150px'>" + di.LastWriteTime 
       + "</TD></TR>");

    PlaceHolder1.Controls.Add(lcFolders);  
}

How to download files from a placeholder control if checkbox is clicked if its not possible then plz tell me that how can i loop through the contents of the placeholder so that i can findout the checkbox and then supply the file name to download the file

Dani AI

Generated

Quick diagnosis and the core problem: the checkboxes you added via literal HTML are not server CheckBox controls, and if they were output without a proper name attribute they won't be posted back at all. Also using di.FullName as the checkbox id exposes server paths and often produces invalid HTML (backslashes, colons, spaces). Two reliable choices: 1) create real server-side checkboxes (or a CheckBoxList) and recreate them in Page_Init so their Checked state is preserved, or 2) emit plain HTML inputs but give each one a name="files" and a safe value (virtual path or encoded key) and read Request.Form.GetValues("files") on postback.

Minimal patterns you can apply immediately:

// create controls in Page_Init so viewstate works
foreach(var f in Directory.GetFiles(rootFolder)) {
    var chk = new CheckBox {
        ID = "chk_" + Path.GetFileNameWithoutExtension(f),
        Text = Path.GetFileName(f)
    };
    chk.Attributes["data-file"] = f; // or store a key/relative path
    PlaceHolder1.Controls.Add(chk);
}

Collect checked items and stream them (single file) or zip multiple files:

var selected = PlaceHolder1.Controls.OfType<CheckBox>()
               .Where(c=>c.Checked)
               .Select(c=>c.Attributes["data-file"]).ToList();
// if one file: Response.TransmitFile(path)
// if many: create zip with System.IO.Compression.ZipArchive and stream it

Quick tips and cautions: validate every selected path against an allowed root (prevent path-traversal), don’t expose raw server paths in markup, prefer streaming with TransmitFile for large files, and avoid wiring downloads to SelectedIndexChanged (auto-postback on each click) — use a single “Download selected” button. ’s CheckBoxList suggestion is valid for simple lists; use server controls when you want easy state handling and file metadata attached to each item.

use checkboxList at server side and use onSelectedIndexChanged or onTextChanged to download file...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.