title: Dropdown in listview SelectedIndexChanged doesn't work first time but does after
body: <p>I've searched high and low, I can't seem to find out what's happening with this. I've simplified the code, but I really have taken it back to as basic as this and still have the same problem.</p>
  
  <p>I have a drop down list in a a repeater (in a Web Form with master page):</p>
  
  <pre><code>&lt;asp:DropDownList ID="TicketQuantityDDL" runat="server" CssClass="qtyddl" AutoPostBack="true" OnSelectedIndexChanged="TicketQuantityDDL_SelectedIndexChanged" CausesValidation="false" SelectedIndex='&lt;%# CInt(Eval("Quantity")) - 1%&gt;'&gt;
                                      &lt;asp:ListItem Value="1"&gt;1&lt;/asp:ListItem&gt;
                                      &lt;asp:ListItem Value="2"&gt;2&lt;/asp:ListItem&gt;
                                      &lt;asp:ListItem Value="3"&gt;3&lt;/asp:ListItem&gt;
                                      &lt;asp:ListItem Value="4"&gt;4&lt;/asp:ListItem&gt;
                                      &lt;asp:ListItem Value="5"&gt;5&lt;/asp:ListItem&gt;
                                      &lt;asp:ListItem Value="6"&gt;6&lt;/asp:ListItem&gt;
                                  &lt;/asp:DropDownList&gt;
  </code></pre>
  
  <p>Handler</p>
  
  <pre><code>  Protected Sub TicketQuantityDDL_SelectedIndexChanged(sender As Object, e As EventArgs)
      myLiteral.text = "Selected Index Changed handled..."
    End Sub
  </code></pre>
  
  <p>The first time the page is loaded if I change the DDL the the page is posted back - the selected index change handler is NOT fired (I've stepped through the code, page.ispostback is true). Every time after the handler works unless the page is full reloaded.</p>
  
  <p><strong>Things I've tried:</strong></p>
  
  <ul>
  <li>Manually adding a handler <code>OnItemCreated</code></li>
  <li>Manually adding a handler <code>OnItemDataBound</code></li>
  <li>Manually registering the control for async postback with scriptmanager</li>
  <li>Using <code>OnClientSelectedIndexChanged</code> to trigger postback from the client</li>
  <li>Removing <code>AutoPostBack</code> and all of the above again...</li>
  <li>I've used <code>Page.Request.Params("__EVENTTARGET")</code> to verify that when the partial postback is fired that the control is the drop down.</li>
  <li>Even though viewstate is enabled I've tried specifying this for the control and the page directly.</li>
  <li>Disabling validation.</li>
  <li>I've tried not binding the value of the drop down and just leaving it
  as is with no selected value and then manually setting the initial
  selected value - no dice.</li>
  <li>Tried removing update panel, same issue.</li>
  </ul>
  
  <p><strong>Things that are DEFINITELY not happening here.</strong></p>
  
  <ul>
  <li>I'm not rebinding on post back <code>if not page.ispostback... databind...</code></li>
  <li>I'm not selecting the same value/first item in the drop down</li>
  <li>This isn't an auto ID problem, the controls ID stay the same through postbacks.</li>
  <li>I'm not doing anything funky other than binding the repeater to a list of objects.</li>
  </ul>
  
  <p>Why isn't the handler firing the first time? After the first time everything works exactly as intended.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I've replicated the exact same behaviour in a list view. Due to time constraints I've used another approach but I'd really like to know how to fix this or at least know why it doesn't work. </p>
  
  <p><strong>Update 2</strong></p>
  
  <p>I've tested the functionality with a bog standard web form and it functions correctly. Something is up with this being in a contentplaceholder from a masterpage, the script manager or update panel. It's as if the event handler for the dropdown is only registered after the first post back, I've tried registering the handler in <code>DataBound</code> and also in the page <code>LoadComplete</code> events, the same thing still happens.</p>
  
  <p><strong>Update 3</strong></p>
  
  <p>I've since changed it to a list view, I'm having the exact same issue though.</p>
  
  <p>This is on a web form with master page, the master page contains the script manager, the list view is in an update panel, although I've tried removing this and I still have the same issue. I've not included the onselectedindexchanged code, I've made it as simple as changing the text of a literal - doesn't work first post back, does the second.</p>
  
  <p>I had originally specified the list items manually but have changed this to programatically at itemDataBound, still no difference.</p>
  
  <p>As I stated above when I check which control caused the postback it's definitely the ddl, it just doesn't fire selectindexchanged the first time. I've also tried specifying the OnSelectedIndexChange in the control itself, still no dice.</p>
  
  <p>Page load ,bind, list view and on item created code.</p>
  
  <p><em>Page Load</em></p>
  
  <pre><code> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
  
          If Not Page.IsPostBack Then
  
              Dim _Basket = SessionHandler.getSessionObject(SessionHandler.SessionObjects.Basket)
  
              If _Basket Is Nothing OrElse DirectCast(_Basket, BasketContainer).BasketItemList.Count = 0 Then
                  BasketSectionContainer.Visible = False
                  alertLiteral.Text = AlertGenerator.GetAlertHTML("No Items in Basket", "There are no items in your basket, please use the menu above to navigate the site.", AlertGenerator.AlertType.warning)
                  If _Basket IsNot Nothing Then SessionHandler.removeSessionObject(SessionHandler.SessionObjects.Basket)
                  Exit Sub
              Else
                  Dim lBasket = DirectCast(_Basket, BasketContainer)
  
  
                  BindBasket(lBasket)
              End If
  
          End If
  
      End Sub
  </code></pre>
  
  <p><em>Bind</em></p>
  
  <pre><code>  Private Sub BindBasket(lBasket As BasketContainer)
  
          basketListView.DataSource = lBasket.BasketItems
          basketListView.DataBind()
  
          bindTotals(lBasket) 'This just sets text of literals on the page outside of the listview
          If lBasket.Postage Then
            PostageDDL.visible = True 'This is outside of the list view also
          End If
      End Sub
  </code></pre>
  
  <p><em>Item Created</em></p>
  
  <pre><code>Private Sub basketListView_ItemCreated(sender As Object, e As ListViewItemEventArgs) Handles basketListView.ItemCreated
  
      Dim QtyDDL As DropDownList = DirectCast(e.Item.FindControl("TicketQuantityDDL"), DropDownList)
      AddHandler QtyDDL.SelectedIndexChanged, AddressOf TicketQuantityDDL_SelectedIndexChanged
  
  End Sub
  </code></pre>
  
  <p>_Item Data Bound _</p>
  
  <pre><code>Private Sub basketListView_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles basketListView.ItemDataBound
  
      Dim data As BasketItem = DirectCast(e.Item.DataItem, BasketItem)
      Dim QtyDDL As DropDownList = DirectCast(e.Item.FindControl("TicketQuantityDDL"), DropDownList)
  
      For i As Integer = 1 To 6
          QtyDDL.Items.Add(New ListItem(i.ToString, i.ToString))
      Next
  
  
      QtyDDL.DataTextField = data.BasketItemID.ToString 'no command arg for DDL so using this, I've tested without, doesn't make a difference.
  
      Select Case data.BasketType
          Case BasketInfo.BasketItemType.DiscountedTickets, BasketInfo.BasketItemType.Tickets, BasketInfo.BasketItemType.Goods
              'tickets and goods...
              QtyDDL.Items.FindByValue(data.Quantity.ToString).Selected = True
  
          Case Else
              'non ticket or goods type, disable quantity selection
  
              QtyDDL.Items.FindByValue("1").Selected = True
              QtyDDL.Enabled = False
      End Select
  
  
  End Sub
  </code></pre>
  
  <p>_List View _</p>
  
  <pre><code>&lt;asp:ListView ID="basketListView" runat="server"&gt;
  
                      &lt;LayoutTemplate&gt;
  
                            &lt;table class="cart-table responsive-table"&gt;
  
                                  &lt;tr&gt;
                                      &lt;th&gt;Item&lt;/th&gt;
                                      &lt;th&gt;Description&lt;/th&gt;
                                      &lt;th&gt;Price&lt;/th&gt;
                                      &lt;th&gt;Quantity&lt;/th&gt;
                                      &lt;th&gt;Total&lt;/th&gt;
                                      &lt;th&gt;&lt;/th&gt;
                                  &lt;/tr&gt;
  
                                    &lt;asp:PlaceHolder ID="itemPlaceholder" runat="server" /&gt;
  
                                &lt;/table&gt;
                          &lt;table class="cart-table bottom"&gt;
                          &lt;tr&gt;
                              &lt;th&gt;
  
                                            &lt;asp:Button ID="ApplyDiscountCodeButton" runat="server" CssClass="button color pull-right" Text="Apply Code" /&gt;
                                  &lt;asp:TextBox ID="DiscountCodeTextBox" runat="server" CssClass="discount-tb pull-right" /&gt;
  
                              &lt;/th&gt;
  
                          &lt;/tr&gt;
  
                      &lt;/table&gt;
                      &lt;div class="clearfix"&gt;&lt;/div&gt;
                      &lt;/LayoutTemplate&gt;
  
                      &lt;ItemTemplate&gt;
                          &lt;tr&gt;
                           &lt;td&gt;
                                      &lt;img src="/images/shows/&lt;%# Eval("imageURL")%&gt;.jpg" alt="&lt;%#Eval("BasketItemTitle")%&gt;" class="basketimg" /&gt;&lt;/td&gt;
                                  &lt;td class="cart-title"&gt;
                                      &lt;a href="#"&gt;&lt;%#Eval("BasketItemTitle")%&gt;&lt;/a&gt;
                                      &lt;br /&gt;
                                      &lt;%# String.Format("{0:dddd} {1} {0:MMMM yyyy}      |     {0:HH:mm}", Eval("PerformanceStarts"), Eval("OrdinalDay"))%&gt;
                                      &lt;br /&gt;
                                      &lt;%# Eval("VenueTitle")%&gt;
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;%#Eval("PriceBandType")%&gt;
                                      &lt;br /&gt;
                                      @ &lt;%# String.Format("{0:c}", Eval("PriceBandValue"))%&gt;
                                  &lt;/td&gt;
                                  &lt;td&gt;
  
                                  &lt;asp:DropDownList ID="TicketQuantityDDL" runat="server" CssClass="qtyddl" AutoPostBack="true" ClientIDMode="Static" /&gt;
  
                                  &lt;/td&gt;
                                  &lt;td class="cart-total"&gt;&lt;%#String.Format("{0:c}", Eval("BasketItemTotalValue"))%&gt;&lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;asp:LinkButton ID="RemoveLinkBtn" runat="server" CssClass="cart-remove" CommandName="RemoveBasketItem" CommandArgument='&lt;%#Eval("BasketItemID")%&gt;' /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                      &lt;/ItemTemplate&gt;
  
                  &lt;/asp:ListView&gt;
  </code></pre>