title: Listview with Search and DataPager controls
body: <p>As this is my first project, I really do not have much idea to debug this. I have a listview. I am writing code manually to populate listview. As the listview is very large, I have a search button where user enters a search string. I am trying to build a sql string from here and populate the listview. Everything works fine but when I click on next page, it does not include the search string. I think it is just populating the 2nd page without the search criteria. If I remove the data pager control, it works fine. I am not able to put proper code datapger_PagePropertiesChanging. I appreciate if  someone can help me with this.</p>
  
  <p>Here is my list view markup:</p>
  
   
                  
                  
                  
                  
               
  
  <pre><code>         &lt;asp:ListView ID="lvContractors" runat="server" OnPagePropertiesChanging="lvContractors_PagePropertiesChanging"&gt;
             &lt;LayoutTemplate&gt;
              &lt;div class="ListViewGrid"&gt;
                  &lt;table class="tblpadding"&gt;
                    &lt;tr&gt;
                        &lt;th class="HeaderStyle"&gt;
                             Contractor Number
                        &lt;/th&gt;
                        &lt;th class="HeaderStyle"&gt;
                             First Name
                        &lt;/th&gt;
                        &lt;th class="HeaderStyle"&gt;
                             Last Name
                        &lt;/th&gt;
                        &lt;th class="HeaderStyle"&gt;
                             View
                        &lt;/th&gt;
                        &lt;th class="HeaderStyle"&gt;
                             Edit
                        &lt;/th&gt;
                     &lt;/tr&gt;
                     &lt;tbody&gt;&lt;asp:PlaceHolder runat="server" ID="itemPlaceholder" /&gt;&lt;/tbody&gt;
  
                   &lt;/table&gt;
               &lt;/div&gt; &lt;!-- End of ListViewGrid --&gt;
  
               &lt;/LayoutTemplate&gt;
               &lt;ItemSeparatorTemplate&gt; &lt;td&gt;   &lt;hr /&gt; &lt;/td&gt;
  
               &lt;/ItemSeparatorTemplate&gt;
  
                &lt;ItemTemplate&gt;
                  &lt;div &gt;
                      &lt;tr&gt;
                         &lt;td class="RowStyle"&gt;&lt;%#Eval("ContractorNumber")%&gt;&lt;/td&gt;
                         &lt;td class="RowStyle"&gt;&lt;%#Eval("FirstName")%&gt;&lt;/td&gt;
                         &lt;td class="RowStyle"&gt;&lt;%#Eval("LastName")%&gt;&lt;/td&gt;
  
                         &lt;td class="RowStyle"&gt;
                             &lt;asp:HyperLink ID="lnkView" runat="server" CssClass="link"
                                  NavigateUrl='&lt;%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&amp;Mode=" + "CV" %&gt;'
                                  Target="_blank" Text="View" /&gt;
                         &lt;/td&gt;
                         &lt;td class="RowStyle"&gt;
                            &lt;asp:HyperLink ID="lnkEdit" runat="server" CssClass="link"
                                  NavigateUrl= '&lt;%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&amp;Mode=" + "CE" %&gt;'
                                  Text="Edit"&gt; &lt;/asp:HyperLink&gt;
  
                       &lt;/tr&gt;
                    &lt;/div&gt;
                 &lt;/ItemTemplate&gt;
                 &lt;AlternatingItemTemplate&gt;
                   &lt;div&gt;
                      &lt;tr&gt;
                         &lt;td class="AlternatingRowStyle"&gt;&lt;%#Eval("ContractorNumber")%&gt;&lt;/td&gt;
                         &lt;td class="AlternatingRowStyle"&gt;&lt;%#Eval("FirstName")%&gt;&lt;/td&gt;
                         &lt;td class="AlternatingRowStyle"&gt;&lt;%#Eval("LastName")%&gt;&lt;/td&gt;
  
                         &lt;td class="AlternatingRowStyle"&gt;
                             &lt;asp:HyperLink ID="lnkView" runat="server" CssClass="link"
                                  NavigateUrl='&lt;%# "Contractor.aspx?ID=" + Eval("ContractorNumber")%&gt;'
                                  Target="_blank" Text="View" /&gt;
                         &lt;/td&gt;
                         &lt;td class="AlternatingRowStyle"&gt;
                            &lt;asp:HyperLink ID="lnkEdit" runat="server" CssClass="link"
                                  NavigateUrl= '&lt;%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&amp;Mode=" + "CE" %&gt;'
                                  Text="Edit"&gt; &lt;/asp:HyperLink&gt;
  
                       &lt;/tr&gt;
                    &lt;/div&gt;
  
                 &lt;/AlternatingItemTemplate&gt;
  
                 &lt;ItemSeparatorTemplate&gt;
                     &lt;tr&gt;
                        &lt;td colspan="5" class="itemseparator"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/ItemSeparatorTemplate&gt;
  
       &lt;/asp:ListView&gt;
  
       &lt;div class="pager"&gt;
          &lt;asp:DataPager PageSize="20" ID="DataPagerContractor" runat="server" PagedControlID="lvContractors"
                          NextPreviousButtonCssClass="PrevNext"
                          CurrentPageLabelCssClass="CurrentPage"
                          NumericButtonCssClass="PageNumber"&gt;
              &lt;fields&gt;
                   &lt;asp:NumericPagerField
                        PreviousPageText="&amp;lt; Prev"
                        NextPageText="Next &amp;gt;"
                        ButtonCount="10"
                        NextPreviousButtonCssClass="PrevNext"
                        CurrentPageLabelCssClass="CurrentPage"
                        NumericButtonCssClass="PageNumber" /&gt;
              &lt;/fields&gt;
          &lt;/asp:DataPager&gt;
        &lt;/div&gt;
  &lt;/div&gt; Code behind this: Partial Class Contractor
  Inherits System.Web.UI.Page
  
  Private txtKeywordSearchWhereClause As String = ""
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
  
      If Not Page.IsPostBack Then
          Session("Clicked") = "Contractor"
      End If
  
      If Not DBNull.Value.Equals(txtKeywordSearchWhereClause) Then
          LoadContractors(txtKeywordSearchWhereClause)
      Else
          LoadContractors("")
      End If
  
  End Sub
  
  Private Sub LoadContractors(ByVal strSearch As String)
      Dim strConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
  
      strConn.Open()
  
      Dim sql As String = "SELECT * FROM tblContractor1"
  
      If Not strSearch = "" Then
          sql = sql &amp; strSearch
          sql = sql &amp; " ORDER BY LastName"
      End If
  
  
      Dim da As SqlDataAdapter = New SqlDataAdapter(sql, strConn)
  
      Dim dt As DataTable = New DataTable()
  
      da.Fill(dt)
  
      lvContractors.DataSource = dt
  
      lvContractors.DataBind()
  End Sub
  
  
  
  Protected Sub buttonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonAdd.Click
      Response.Redirect("ContractorEdit.aspx", False)
  End Sub
  
  Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
      '   Keyword Search
  
      Dim txtKeywordSearch As String = txtSearch.Text
  
  
      Dim txtWhereClause As String = ""
  
      If txtKeywordSearch.Trim() &lt;&gt; "" Then
          txtWhereClause = " WHERE FirstName LIKE '%" + txtKeywordSearch + "%'"
          txtWhereClause = txtWhereClause + " OR LastName LIKE '%" + txtKeywordSearch + "%'"
          txtWhereClause = txtWhereClause + " OR ContractorNumber LIKE '%" + txtKeywordSearch + "%'"
  
          txtKeywordSearchWhereClause = txtWhereClause
      End If
  
      LoadContractors(txtKeywordSearchWhereClause)
  
  End Sub
  
  Protected Sub lvContractors_PagePropertiesChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles lvContractors.PagePropertiesChanging
      DataPagerContractor.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
      'rebind List View
      If Not DBNull.Value.Equals(txtKeywordSearchWhereClause) Then
          LoadContractors(txtKeywordSearchWhereClause)
      Else
          LoadContractors("")
      End If
  End Sub
  </code></pre>
  
  <p>End Class</p>