ArunPratap
ArunPratap

Reputation: 5030

How to populate Dropdownlist with server Folder Filenames

I have added a folder in My project With Name (ReportFile) and I am saving xml Files in that Folder.

i want to Show all Xml File Name in a DropDownList (that are stored in that folder)

My DropDown:

    <asp:DropDownList ID="ddlReportTemplate" runat="server" AutoPostBack="True">
 </asp:DropDownList>

Upvotes: 0

Views: 1522

Answers (1)

Evan Huang
Evan Huang

Reputation: 1275

In the Page_Load of the code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        var reportFolderPath = Server.MapPath("~/Reports"); //change the "~/Reports" to your report folder name

        IEnumerable<string> xmlFiles = Directory.GetFiles(reportFolderPath, "*.xml");

        //As a common practice server file path should not be shown to the client, use file name instead
        xmlFiles = xmlFiles.Select(o => Path.GetFileNameWithoutExtension(o)).Where(o => o.Contains("Arun"));;

        ddlReportTemplate.DataSource = xmlFiles;
        ddlReportTemplate.DataBind();
    }

Upvotes: 2

Related Questions