title: Use an array to populate a listbox?
body: <p>The website I am working on will contain a drop-down list that shows a number of reason codes.</p>
  
  <p>Is it possible to populate a drop-down list with an array derived from a custom class?</p>
  
  <p>Something like this:</p>
  
  <p><strong>ASPX Page</strong></p>
  
  <pre><code>&lt;asp:DropDownList ID="ddlReasonsWhy" runat="server"&gt;&lt;/asp:DropDownList&gt;
  </code></pre>
  
  <p><strong>ASPX Code Behind</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      //instantiate custom class
      Class1 reasonsList = new Class1();
     //populate reasons list
     this.ddlReasonsWhy.Items.Add(reasonsList.getReasons);
  </code></pre>
  
  <p><strong>Custom Class</strong></p>
  
  <pre><code>public string[] getReasons()
      {
          string[] Reasons;
          Reasons[0] = "test";
          Reasons[1] = "test2";
          Reasons[2] = "test3";
  
          return Reasons;
      }
  </code></pre>
  
  <p><strong><em>UPDATE1:</em></strong></p>
  
  <p>Thanks to the answers and this stack <a href="https://stackoverflow.com/questions/132643/adding-listitems-to-a-dropdownlist-from-a-generic-list">link</a> my final solution is similar to Ahmet and silky answers.</p>
  
  <p><strong>Class</strong></p>
  
  <pre><code> public List&lt;ListItem&gt; getRejectReasons()
      {
          List&lt;ListItem&gt; reasons = new List&lt;ListItem&gt;();
          reasons.Add(new ListItem("Select Value", "0"));
          reasons.Add(new ListItem("Red", "1"));
          reasons.Add(new ListItem("Green", "2"));
          return reasons;
  
      }
  </code></pre>
  
  <p><strong>Code Behind</strong></p>
  
  <pre><code> //instantiate custom class
   Class1 reject = new Class1();
   this.ddlReasonsWhy.DataSource = reject.getRejectReasons();
   this.ddlReasonsWhy.DataBind();
  </code></pre>