Load file content to DropDownList (C#) : File « File Directory « ASP.NET Tutorial






<%@ Page Language="C#" Debug="true" %>
<script runat="server">

private void Page_Load(object sender, System.EventArgs e) {

  string MyFileName;
  System.IO.StreamReader ObjectStreamReader = null;
  ArrayList FileLines = new ArrayList();

  MyFileName = Page.MapPath("Data.txt");

  try {
    ObjectStreamReader = new System.IO.StreamReader (MyFileName);
    while (ObjectStreamReader.Peek() > -1) {
      FileLines.Add(ObjectStreamReader.ReadLine());
    }
    FileLines.Sort();
    Label1.Text = "Select a title:";    
    DropDownList1.DataSource = FileLines;
    DropDownList1.DataBind(); 

  } catch (Exception ObjectError) {
    Label1.Text = ObjectError.Message;
    DropDownList1.Visible = false;
  } finally {
    if (ObjectStreamReader != null) {
                  ObjectStreamReader.Close();
    }
  }
      
}
</script>
<html>
  <head>
  </head>
  <body>
    <form runat="server">
      <asp:Label id="Label1" runat="server"></asp:Label>
      <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
    </form>
  </body>
</html>








10.1.File
10.1.1.Get file information (VB.net)
10.1.2.Opening a File (C#)
10.1.3.Opening a File (VB)
10.1.4.Delete a file (C#)
10.1.5.Delete a file (VB)
10.1.6.Creating a File (C#)
10.1.7.Creating a File (VB)
10.1.8.Load file content to DropDownList (C#)
10.1.9.Load file content to DropDownList (VB)
10.1.10.Rename File