title: ASP.NET: Wrong event is fired when I click a LinkButton
body: <p>I have a "cart" <code>Repeater</code>, where every item has a couple of controls in it. The ones we're focusing on are two <code>DropDownLists</code>, a <code>LinkButton</code> and a <code>TextBox</code>. The lists are on one validation group and the button &amp; textbox are on another.</p>
  
  <p>Both lists have an <code>OnSelectedIndexChanged</code> event handler which does some calculations based on the selections in both <code>DropDownLists</code>. Both lists also have <code>AutoPostBack="True"</code>.</p>
  
  <p>The button has an <code>OnClick</code> handler which also does some calculations according to the number in the textbox.</p>
  
  <p>I need the calculations to be updated immediately - so I added another data binding for the <code>Repeater</code> - on each event handler</p>
  
  <p><strong>The problem is</strong> - I can't get the value from the TextBox after I click on one of the <code>DropDownLists</code>. It always comes off as 1. In the background, the <code>OnSelectedIndexChanged</code> event handler fires <strong>first</strong>, and the buttons' <code>OnClick</code> will fires after it. That <strong>only happens</strong> after I change one of the lists - and it happens every time I click that button from that moment on. Before that - everything is normal on all controls.</p>
  
  <p>What do you think? Below is some code (I hope it's not too much, I included everything that's taking part) - <strong>Thank you very much!</strong></p>
  
  <p>Here's the repeater template:</p>
  
  <pre><code>                &lt;ItemTemplate&gt;
                      &lt;tr&gt;
                          &lt;td class="size"&gt;&lt;div&gt;&lt;asp:DropDownList runat="server" ID="_selectSize" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="&lt;%#Container.ItemIndex%&gt;" ValidationGroup="doNotValidate"&gt;&lt;/asp:DropDownList&gt;&lt;/div&gt;&lt;/td&gt;
                          &lt;td class="material"&gt;&lt;div&gt;&lt;asp:DropDownList runat="server" ID="_selectMaterial" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="&lt;%#Container.ItemIndex%&gt;" ValidationGroup="doNotValidate"&gt;&lt;/asp:DropDownList&gt;&lt;/div&gt;&lt;/td&gt;
                          &lt;td class="quantity"&gt;
                              &lt;div&gt;
                                  &lt;div class="quantity_container"&gt;&lt;asp:TextBox runat="server" ID="_txtAmount" name="quantity_&lt;%#Container.ItemIndex%&gt;" ValidationGroup="vCart"&gt;&lt;/asp:TextBox&gt;&lt;/div&gt;
                                  &lt;div&gt;&lt;asp:LinkButton runat="server" CssClass="refresh" id="_refreshCart" ValidationGroup="vCart"&gt;&lt;/asp:LinkButton&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p>The <code>Repeater.DataBound()</code>:</p>
  
  <pre><code>Protected Sub rptCart_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCart.ItemDataBound
      If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
          Dim OrderItem As ProxyMarket.Item = CType(e.Item.DataItem, ProxyMarket.Item)
          Dim btnRemoveProduct As LinkButton = CType(e.Item.FindControl("_removeProduct"), LinkButton)
          Dim btnRefreshCart As LinkButton = CType(e.Item.FindControl("_refreshCart"), LinkButton)
          Dim txtAmount As TextBox = CType(e.Item.FindControl("_txtAmount"), TextBox)
          Dim sizeSelect As DropDownList = CType(e.Item.FindControl("_selectSize"), DropDownList)
          Dim materialSelect As DropDownList = CType(e.Item.FindControl("_selectMaterial"), DropDownList)
  
          btnRemoveProduct.CommandName = OrderItem.ID
          btnRefreshCart.CommandName = OrderItem.ID
          txtAmount.Text = OrderItem.Units
  
          AddHandler btnRemoveProduct.Click, AddressOf removeProduct
          AddHandler btnRefreshCart.Click, AddressOf refreshCart
  
          sizeSelect.DataSource = sizeList
          sizeSelect.DataBind()
          sizeSelect.SelectedIndex = s
          materialSelect.DataSource = materialList
          materialSelect.DataBind()
          materialSelect.SelectedIndex = m
      End If
  End Sub
  </code></pre>
  
  <p>Here is the <code>DropDownLists</code> event handler:</p>
  
  <pre><code>Protected Sub selectChange(ByVal sender As DropDownList, ByVal e As System.EventArgs)
      Dim listing As New PriceListing
      Dim ddl As DropDownList
      Dim selectedIndex As Integer
  
      If sender.ID = "_selectSize" Then
          For Each rptrItem As RepeaterItem In rptCart.Items
              ddl = CType(rptrItem.FindControl("_selectMaterial"), DropDownList)
              If ddl.TabIndex = sender.TabIndex Then Exit For
          Next
  
          For Each listing In artDecoPricing
              If listing.Size = sender.SelectedValue Then Exit For
          Next
  
          selectedIndex = ddl.SelectedIndex
          s = sender.SelectedIndex
      ElseIf sender.ID = "_selectMaterial" Then
          For Each rptrItem As RepeaterItem In rptCart.Items
              ddl = CType(rptrItem.FindControl("_selectSize"), DropDownList)
              If ddl.TabIndex = sender.TabIndex Then Exit For
          Next
  
          For Each listing In artDecoPricing
              If listing.Size = ddl.SelectedValue Then Exit For
          Next
  
          selectedIndex = sender.SelectedIndex
          s = ddl.SelectedIndex
      End If
  
      Select Case selectedIndex
          Case 0
              Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Canvas
          Case 1
              Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Acrylic
          Case 2
              Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Framed
          Case 3
              Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Alucobond
      End Select
  
      Cart.SaveOrder()
  
      s = sender.SelectedIndex
      m = selectedIndex
  
      rptCart.DataSource = Cart.Order.Items
      rptCart.DataBind()
  End Sub
  </code></pre>
  
  <p>And finally, the <code>LinkButton</code> <code>OnClick</code> handler:</p>
  
  <pre><code>Protected Sub refreshCart(ByVal sender As Object, ByVal e As System.EventArgs)
      Dim btnRefreshCart As LinkButton = CType(sender, LinkButton)
      Dim amountStr As String
      Dim amount As Integer
  
      amountStr = CType(btnRefreshCart.Parent.FindControl("_txtAmount"), TextBox).Text
      If IsNumeric(amountStr) Then
          amount = CDbl(amountStr)
      Else
          amount = -1
      End If
      amount = IIf(amount &gt; 0, amount, -30)
  
      For Each Item As ProxyMarket.Item In Cart.Order.Items
          If Item.ID = btnRefreshCart.CommandName Then
              Item.Units = amount
              Cart.Edit_Product(btnRefreshCart.CommandName, amount)
              Exit For
          End If
      Next
  
      rptCart.DataSource = Cart.Order.Items
      rptCart.DataBind()
  End Sub
  </code></pre>
  
  <p>Thank you :)</p>