如何使用服务器文件夹文件名填充Dropdownlist

时间:2017-12-07 08:32:48

标签: c# asp.net .net xml

我在My project With Name(ReportFile)中添加了一个文件夹,我正在该文件夹中保存xml文件。

我想在Xml中显示所有DropDownList文件名 (存储在该文件夹中)

我的掉落:

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

1 个答案:

答案 0 :(得分:2)

背后的代码的Page_Load中
    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();
    }