title: ASP.NET ListView SQLDataSource
body: <p>I use Visual Studio 2015 with a WebForm Webapplication.
  I have add a ListView and a SQLDataSource.
  The SQLDataSource is connected to a SQL-Server.</p>
  
  <p>Using the ListView operations like Edit, Insert and Delete are working.</p>
  
  <p>I have a textbox where the user can enter a City-Name and press a button that will update the SQLDataSource with a SQL-Query
  With this I can search/filter for City-Names.
  This works.</p>
  
  <p>But if I click the "Edit" button after a search the search result in the Listview gets removed and all records are displayed
  So I can not use "edit" after I have done a search.</p>
  
  <p>How can I run a search and edit one of the found records in the ListView?</p>
  
  <p><a href="https://i.stack.imgur.com/yTWVd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yTWVd.png" alt="enter image description here"></a></p>
  
  <p>.apsx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="frmMain.aspx.cs" Inherits="DB_mit_GridView.frmMain" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
  
          &lt;asp:Label ID="Label1" runat="server" Text="Stadt"&gt;&lt;/asp:Label&gt;
          &lt;asp:TextBox ID="txtCity" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Suche" /&gt;
  
      &lt;/div&gt;
          &lt;asp:GridView ID="GridViewDatenAnzeigen" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" PageSize="5"&gt;
              &lt;Columns&gt;
                  &lt;asp:CommandField ShowSelectButton="True" /&gt;
                  &lt;asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /&gt;
                  &lt;asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" /&gt;
                  &lt;asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /&gt;
                  &lt;asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" /&gt;
                  &lt;asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
          &lt;br /&gt;
          &lt;asp:ListView ID="ListView1" runat="server" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ListView1_SelectedIndexChanged" InsertItemPosition="LastItem"&gt;
              &lt;AlternatingItemTemplate&gt;
                  &lt;tr style=""&gt;
                      &lt;td&gt;
                          &lt;asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /&gt;
                          &lt;asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CustomerIDLabel" runat="server" Text='&lt;%# Eval("CustomerID") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CustomerNameLabel" runat="server" Text='&lt;%# Eval("CustomerName") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CityLabel" runat="server" Text='&lt;%# Eval("City") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="PostalCodeLabel" runat="server" Text='&lt;%# Eval("PostalCode") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CountryLabel" runat="server" Text='&lt;%# Eval("Country") %&gt;' /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/AlternatingItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;tr style=""&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="CustomerIDLabel1" runat="server" Text='&lt;%# Eval("CustomerID") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="CustomerNameTextBox" runat="server" Text='&lt;%# Bind("CustomerName") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="CityTextBox" runat="server" Text='&lt;%# Bind("City") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="PostalCodeTextBox" runat="server" Text='&lt;%# Bind("PostalCode") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="CountryTextBox" runat="server" Text='&lt;%# Bind("Country") %&gt;' /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/EditItemTemplate&gt;
              &lt;EmptyDataTemplate&gt;
                  &lt;table runat="server" style=""&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;
                          &lt;asp:TextBox ID="CustomerIDTextBox" runat="server" Text='&lt;%# Bind("CustomerID") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="CustomerNameTextBox" runat="server" Text='&lt;%# Bind("CustomerName") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="CityTextBox" runat="server" Text='&lt;%# Bind("City") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="PostalCodeTextBox" runat="server" Text='&lt;%# Bind("PostalCode") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="CountryTextBox" runat="server" Text='&lt;%# Bind("Country") %&gt;' /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/InsertItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;tr style=""&gt;
                      &lt;td&gt;
                          &lt;asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /&gt;
                          &lt;asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CustomerIDLabel" runat="server" Text='&lt;%# Eval("CustomerID") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CustomerNameLabel" runat="server" Text='&lt;%# Eval("CustomerName") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CityLabel" runat="server" Text='&lt;%# Eval("City") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="PostalCodeLabel" runat="server" Text='&lt;%# Eval("PostalCode") %&gt;' /&gt;
                      &lt;/td&gt;
  
                      &lt;td&gt;
                          &lt;asp:Label ID="CountryLabel" runat="server" Text='&lt;%# Eval("Country") %&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="0" style=""&gt;
                                  &lt;tr runat="server" style=""&gt;
                                      &lt;th runat="server"&gt;&lt;/th&gt;
                                      &lt;th runat="server"&gt;CustomerID&lt;/th&gt;
                                      &lt;th runat="server"&gt;CustomerName&lt;/th&gt;
                                      &lt;th runat="server"&gt;City&lt;/th&gt;
                                      &lt;th runat="server"&gt;PostalCode&lt;/th&gt;
                                      &lt;th runat="server"&gt;Country&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=""&gt;
                              &lt;asp:DataPager ID="DataPager1" runat="server" &gt;
                                  &lt;Fields&gt;
                                      &lt;asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"  /&gt;
                                      &lt;asp:NumericPagerField /&gt;
                                      &lt;asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" /&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=""&gt;
  
                      &lt;td&gt;
                          &lt;asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /&gt;
                          &lt;asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CustomerIDLabel" runat="server" Text='&lt;%# Eval("CustomerID") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CustomerNameLabel" runat="server" Text='&lt;%# Eval("CustomerName") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CityLabel" runat="server" Text='&lt;%# Eval("City") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="PostalCodeLabel" runat="server" Text='&lt;%# Eval("PostalCode") %&gt;' /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="CountryLabel" runat="server" Text='&lt;%# Eval("Country") %&gt;' /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/SelectedItemTemplate&gt;
          &lt;/asp:ListView&gt;
          &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:TestDBConnectionString %&gt;" SelectCommand="SELECT * FROM [Customers]" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @original_CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CustomerName], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CustomerName, @City, @PostalCode, @Country)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Customers] SET [CustomerName] = @CustomerName, [City] = @City, [PostalCode] = @PostalCode, [Country] = @Country WHERE [CustomerID] = @original_CustomerID"&gt;
              &lt;DeleteParameters&gt;
                  &lt;asp:Parameter Name="original_CustomerID" Type="String" /&gt;
              &lt;/DeleteParameters&gt;
              &lt;InsertParameters&gt;
                  &lt;asp:Parameter Name="CustomerID" Type="String" /&gt;
                  &lt;asp:Parameter Name="CustomerName" Type="String" /&gt;
                  &lt;asp:Parameter Name="City" Type="String" /&gt;
                  &lt;asp:Parameter Name="PostalCode" Type="String" /&gt;
                  &lt;asp:Parameter Name="Country" Type="String" /&gt;
              &lt;/InsertParameters&gt;
              &lt;UpdateParameters&gt;
                  &lt;asp:Parameter Name="CustomerName" Type="String" /&gt;
                  &lt;asp:Parameter Name="City" Type="String" /&gt;
                  &lt;asp:Parameter Name="PostalCode" Type="String" /&gt;
                  &lt;asp:Parameter Name="Country" Type="String" /&gt;
                  &lt;asp:Parameter Name="original_CustomerID" Type="String" /&gt;
              &lt;/UpdateParameters&gt;
          &lt;/asp:SqlDataSource&gt;
          &lt;br /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>.aspx.cs</p>
  
  <pre><code>public partial class frmMain : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
  
          protected void btnSearch_Click(object sender, EventArgs e)
          {
  
              //Search is done here
  
              SqlDataSource1.SelectCommand = "SELECT* FROM[Customers] where City = '" + txtCity.Text +"'";
  
  
          }
  
          protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
          {
  
          }
      }
  </code></pre>