title: Accessing Controls Inside ASP.NET View Controls (Event Handling)
body: <p>If I have the following <code>ListView</code>, how can I attach a <code>SelectedIndexChanged</code> event listener to the <code>DropDownList</code> so I can perform a command on the respective object? Imagine I have a list of new users and I want to add them to a usergroup by selecting the group from the <code>DropDownList</code>.</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;asp:ListView ID="NewUsers" runat="server" DataSourceID="NewUsersSDS" DataKeyNames="ID"&gt;
      &lt;LayoutTemplate&gt;
          &lt;asp:Table ID="groupPlaceholder" runat="server"&gt;&lt;asp:TableRow&gt;&lt;/asp:TableRow&gt;&lt;/asp:Table&gt;
      &lt;/LayoutTemplate&gt;
      &lt;GroupTemplate&gt;
          &lt;asp:TableCell ID="itemPlaceholder" runat="server"&gt;&lt;/asp:TableCell&gt;
      &lt;/GroupTemplate&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:Table ID="NewUsersTable" runat="server" Width="32%" CssClass="inlineTable"&gt;
              &lt;asp:TableRow&gt;
                  &lt;asp:TableCell Width="100px"&gt;&lt;%# Eval("FullName").ToString.Trim()%&gt;&lt;/asp:TableCell&gt;
                  &lt;asp:TableCell&gt;
                      &lt;asp:HiddenField ID="RowIndex" runat="server" Value="&lt;%# Container.DisplayIndex %&gt;" /&gt;
                      &lt;asp:DropDownList ID="UserGroupSelect" runat="server" DataSourceID="UserGroupSelectSDS" DataValueField="ID" DataTextField="UserGroup"
                          OnSelectedIndexChanged="UserGroupSelect_SelectedIndexChanged" AutoPostBack="True"&gt;
                      &lt;/asp:DropDownList&gt;
                  &lt;/asp:TableCell&gt;
              &lt;/asp:TableRow&gt;
          &lt;/asp:Table&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>I've been having issues accessing controls inside of __View controls. I read in a few places that you could access them by <code>NewUsers.FindControl([ControlID as String])</code> but this doesn't seem to be working for me. I guess this is what's called a dynamic control? Not really sure, feeling a bit lost.</p>
  
  <p>As always, your help is greatly appreciated. ;)</p>
  
  <p><strong>Additional Info / Code</strong></p>
  
  <pre class="lang-vb prettyprint-override"><code>'Now working code, thanks to James :)
  Protected Sub ItemBind(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles NewUsers.ItemDataBound
      Dim lv As ListView = DirectCast(sender, ListView)
      If e.Item.ItemType = ListViewItemType.DataItem Then
          lv.DataKeys(e.Item.DataItemIndex).Value.ToString() 'get the datakey
      End If
  End Sub
      Protected Sub UserGroupSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
      Dim RowIndex As Integer = CInt(DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("RowIndex"), HiddenField).Value)
      Dim pk As Integer = CInt(NewUsers.DataKeys(RowIndex)("ID"))
      Try
          MessageBox("Update key " + pk.ToString, "Update Key") 'Custom js "alert" box function
      Catch ex As Exception
          MessageBox("Something went wrong, is the update key empty?")
      End Try
  End Sub
  </code></pre>