title: Get values from selected item in listview using asp.net c#
body: <p>I've tried many many different ways of displaying values from listview to either textbox or just store it in session and use it later, but none of it works.</p>
  
  <p>I was able to make it work for GridView, but i just can't make it work for ListView.</p>
  
  <p>The idea is to get id of selected user (in gridview) and id of selected club (in listview)</p>
  
  <p>After getting and storing both id's from selected rows, i can add club to another user.</p>
  
  <p>Let's say i have table <strong>Clubs</strong> which has item: (4 columns, 1 row)</p>
  
  <p>ID: 1
  ClubName: Los Angeles Lakers
  City: Los Angeles
  State: California</p>
  
  <p>When i press on the first row, it should highlight in a different color, and then store ID either to session or just textbox. Doesn't really matter as i'll decide that later. But can you guys please help me get ID ?</p>
  
  <p><strong>This is source code</strong></p>
  
  <pre><code>&lt;asp:ListView ID="ListView1" runat="server" DataKeyNames="KlubID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ListView1_SelectedIndexChanged" style="margin-right: 324px" OnRowCreated="GridView1_RowCreated"&gt;
          &lt;AlternatingItemTemplate&gt;
              &lt;tr style="background-color:#FFF8DC;"&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KlubIDLabel" runat="server" Text='&lt;%# Eval("KlubID") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="ImeKlubaLabel" runat="server" Text='&lt;%# Eval("ImeKluba") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KrajLabel" runat="server" Text='&lt;%# Eval("Kraj") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="DrzavaLabel" runat="server" Text='&lt;%# Eval("Drzava") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="MenedzerLabel" runat="server" Text='&lt;%# Eval("Menedzer") %&gt;' /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/AlternatingItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;tr style="background-color:#008A8C;color: #FFFFFF;"&gt;
                  &lt;td&gt;
                      &lt;asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /&gt;
                      &lt;asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KlubIDLabel1" runat="server" Text='&lt;%# Eval("KlubID") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="ImeKlubaTextBox" runat="server" Text='&lt;%# Bind("ImeKluba") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="KrajTextBox" runat="server" Text='&lt;%# Bind("Kraj") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="DrzavaTextBox" runat="server" Text='&lt;%# Bind("Drzava") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="MenedzerTextBox" runat="server" Text='&lt;%# Bind("Menedzer") %&gt;' /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/EditItemTemplate&gt;
          &lt;EmptyDataTemplate&gt;
              &lt;table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;No data was returned.&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/EmptyDataTemplate&gt;
          &lt;InsertItemTemplate&gt;
              &lt;tr style=""&gt;
                  &lt;td&gt;
                      &lt;asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /&gt;
                      &lt;asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="ImeKlubaTextBox" runat="server" Text='&lt;%# Bind("ImeKluba") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="KrajTextBox" runat="server" Text='&lt;%# Bind("Kraj") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="DrzavaTextBox" runat="server" Text='&lt;%# Bind("Drzava") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:TextBox ID="MenedzerTextBox" runat="server" Text='&lt;%# Bind("Menedzer") %&gt;' /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/InsertItemTemplate&gt;
          &lt;ItemTemplate&gt;
              &lt;tr style="background-color:#DCDCDC;color: #000000;"&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KlubIDLabel" runat="server" Text='&lt;%# Eval("KlubID") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="ImeKlubaLabel" runat="server" Text='&lt;%# Eval("ImeKluba") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KrajLabel" runat="server" Text='&lt;%# Eval("Kraj") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="DrzavaLabel" runat="server" Text='&lt;%# Eval("Drzava") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="MenedzerLabel" runat="server" Text='&lt;%# Eval("Menedzer") %&gt;' /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/ItemTemplate&gt;
          &lt;LayoutTemplate&gt;
              &lt;table runat="server"&gt;
                  &lt;tr runat="server"&gt;
                      &lt;td runat="server"&gt;
                          &lt;table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;"&gt;
                              &lt;tr runat="server" style="background-color:#DCDCDC;color: #000000;"&gt;
                                  &lt;th runat="server"&gt;KlubID&lt;/th&gt;
                                  &lt;th runat="server"&gt;ImeKluba&lt;/th&gt;
                                  &lt;th runat="server"&gt;Kraj&lt;/th&gt;
                                  &lt;th runat="server"&gt;Drzava&lt;/th&gt;
                                  &lt;th runat="server"&gt;Menedzer&lt;/th&gt;
                              &lt;/tr&gt;
                              &lt;tr id="itemPlaceholder" runat="server"&gt;
                              &lt;/tr&gt;
                          &lt;/table&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr runat="server"&gt;
                      &lt;td runat="server" style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;"&gt;
                          &lt;asp:DataPager ID="DataPager1" runat="server"&gt;
                              &lt;Fields&gt;
                                  &lt;asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" /&gt;
                              &lt;/Fields&gt;
                          &lt;/asp:DataPager&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/LayoutTemplate&gt;
          &lt;SelectedItemTemplate&gt;
              &lt;tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;"&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KlubIDLabel" runat="server" Text='&lt;%# Eval("KlubID") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="ImeKlubaLabel" runat="server" Text='&lt;%# Eval("ImeKluba") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="KrajLabel" runat="server" Text='&lt;%# Eval("Kraj") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="DrzavaLabel" runat="server" Text='&lt;%# Eval("Drzava") %&gt;' /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="MenedzerLabel" runat="server" Text='&lt;%# Eval("Menedzer") %&gt;' /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/SelectedItemTemplate&gt;
      &lt;/asp:ListView&gt;
  </code></pre>
  
  <p><strong>This is code - my latest, doesnt work</strong></p>
  
  <pre><code>protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.ListViewCommandEventArgs e)
  {
      ListViewDataItem item1 = e.Item as ListViewDataItem;
      int findMe = item1.DisplayIndex;
  
      var route = (Label)item1.FindControl("ID");
      TextBox1.Text = route.Text;
  } 
  </code></pre>
  
  <p><strong>Any input would be appreciated, thank you!</strong></p>
  
  <p><strong>PS, IS IT POSSIBLE TO HAVE TWO GRIDVIEW WHICH WOULD STORE DIFFERENT DATA?</strong></p>
  
  <p>I tried this before, but everytime i selected item in 2nd gridview, the item from 1st gridview was getting selected.</p>