title: Dropdownlist reset after postpack when bound to function
body: <p>I have been having problems with dropdownlists in an asp.net project. After exhaustive checking I have narrowed things down to two scenario's. Before you respond please try this for yourself as proof. VS2010 or VS2012 ASP.Net project. I guarantee you this will NOT work.</p>
  
  <p>I consider myself an experienced .net developer and code like this would normally be very trivial indeed for me but I am absolutely stumped with this one.</p>
  
  <p>Any help would be appreciated</p>
  
  <p>In my default.aspx <strong>Page_Load</strong> event page I have this code and nothing else</p>
  
  <pre><code>if (!IsPostBack)
  {
       cboDept1.DataSource = GetTeamGroups();
       cboDept1.DataValueField = "Value";
       cboDept1.DataTextField = "Text";
       cboDept1.DataBind();
  }
  </code></pre>
  
  <p>With this version of the <strong>GetTeamGroups</strong> function code the dropdownlist works as expected, the list is preserved and I can get the <strong>SelectedItem</strong> etc. with no problems after each postback.</p>
  
  <pre><code>private List&lt;ListItem&gt; GetTeamGroups()
  {
      List&lt;ListItem&gt; oList = new List&lt;ListItem&gt;();
  
      oList.Add(new ListItem("1", "1"));
      oList.Add(new ListItem("2", "2"));
      oList.Add(new ListItem("3", "3"));
      oList.Add(new ListItem("4", "4"));
  
      return oList;
  }
  </code></pre>
  
  <p>Thats not practical where I want to get my list from a function (database, or webservice). But this version of the <strong>GetTeamGroups</strong> function this the dropdownlist is reset with each postback. I CANNOT get the SelectedItem. (PS. It doesn't really matter what code you use to dynamically fill the list, the problem is the same. Seems like the list cannot be filled dynamically for this to work).</p>
  
  <pre><code>private List&lt;ListItem&gt; GetTeamGroups()
  {
      List&lt;ListItem&gt; oList = new List&lt;ListItem&gt;();
  
      string[] sGroups = ConfigurationManager.AppSettings["Groups"].ToString().Split(';');
  
      int iCount = 0;
  
      foreach (string sGroup in sGroups)
      {
          oList.Add(new ListItem(sGroup, iCount.ToString()));
      }
  
      return oList;
  }
  </code></pre>
  
  <p>I have read numerous articles suggesting that the viewstate may help. No combination of viewstate on the control or page resolves the problem. The list is ALWAYS reset.</p>