title: Dropdown in .net usercontrol not maintaining state
body: <p>This is probably something fundamental and stupid that I've missed, but the various workarounds are causing me such huge headaches that really, I need to understand the basic problem here in order to find the best solution.</p>
  
  <p>I have a usercontrol which contains a drop-down list.</p>
  
  <pre><code>&lt;asp:DropDownList ID="ddlOrderStatus" AutoPostBack="true" runat="server" CssClass="textbox"&gt;
     &lt;asp:ListItem value="8"&gt;Pending&lt;/asp:ListItem&gt;
     &lt;asp:ListItem value="9"&gt;On Hold&lt;/asp:ListItem&gt;
     &lt;asp:ListItem Value="11"&gt;Complete&lt;/asp:ListItem&gt;
     &lt;asp:ListItem Value="12"&gt;Cancelled&lt;/asp:ListItem&gt;             
   &lt;/asp:DropDownList&gt;
  </code></pre>
  
  <p>It does nothing in its Page_Load event.</p>
  
  <p>This is in turn contained in a page which, in it's Page_Load event does some databinding on some repeater controls on the page but doesn't touch the control containing the ddl. There is no ajax on the page.</p>
  
  <p>The dropdownlist is - clearly - set to autopostback = true. The postback code looks like this:</p>
  
  <pre><code>Private Sub ddlOrderStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrderStatus.SelectedIndexChanged
      Dim ddl As DropDownList = DirectCast(sender, DropDownList)
      Dim currentsorting As String = Request.QueryString("sort")
      Dim currentpaging As String = Request.QueryString("page")
      Response.Redirect(String.Format("~/admin/orders/Manage/0?sort={0}&amp;page={1}&amp;status={2}", currentsorting, currentpaging, ddl.SelectedValue))
  End Sub
  </code></pre>
  
  <p>So when the ddl is changed, the page actually does a double postback. I see no way to avoid this as I need to do a postback to get the value in the ddl and then another postback to get some new data from  the server based on that value.</p>
  
  <p>Anyway, the long and the short of it is that when you do this the dropdownlist fails to maintain its state. So you select a value, the code does the job it's supposed to - gets the selected value then posts back again returning the new expected set of data - and then on loading again the dropdownlist is back in its default form.</p>
  
  <p>This is a pernicious problem for me because of that double-postback: if I try and set the value of the ddl to equal what's in the querystring I have, effecitvely, set it to that value permanently because Page_Load - where the value is set - occurs before events are processed and so the value stays unchanged. I tried moving the code which changed the selectedvalue of the ddl into Render, but it seemed that although the selected value had changed as far as the user could see, the selected value was still the default value during processing and was treated as such.</p>
  
  <p>I also tried setting the selectedValue in session and then clearing it as soon as it was populated. That worked for some scenarios, but unfortunately this page also contains some javascript that can cause it to post back - and you can't set session from client-side javascript. So that idea had to go out the window. </p>
  
  <p>I'm at a loss here - this apparently simple problem has eaten a whole day of my time. Can anyone either tell me why state isn't being maintained on this control and/or suggest a way I get it to show the right value after postback without turning the selection into a permanent one?</p>
  
  <p>Cheers,
  Matt</p>