带有Search和DataPager控件的Listview

时间:2011-12-29 17:04:27

标签: asp.net vb.net datapager

由于这是我的第一个项目,我真的没有太多想法来调试它。我有一个listview。我正在手动编写代码来填充listview。由于列表视图非常大,我有一个搜索按钮,用户输入搜索字符串。我正在尝试从这里构建一个sql字符串并填充listview。一切正常,但当我点击下一页时,它不包括搜索字符串。我认为它只是在没有搜索条件的情况下填充第二页。如果我删除数据寻呼机控件,它工作正常。我无法输入正确的代码datapger_PagePropertiesChanging。如果有人能帮助我,我感激不尽。

这是我的列表视图标记:

                                                                                   
         <asp:ListView ID="lvContractors" runat="server" OnPagePropertiesChanging="lvContractors_PagePropertiesChanging">
           <LayoutTemplate>
            <div class="ListViewGrid">
                <table class="tblpadding">
                  <tr>
                      <th class="HeaderStyle">
                           Contractor Number
                      </th>
                      <th class="HeaderStyle">
                           First Name
                      </th>
                      <th class="HeaderStyle">
                           Last Name
                      </th>
                      <th class="HeaderStyle">
                           View
                      </th>
                      <th class="HeaderStyle">
                           Edit
                      </th>
                   </tr>
                   <tbody><asp:PlaceHolder runat="server" ID="itemPlaceholder" /></tbody>

                 </table>
             </div> <!-- End of ListViewGrid -->

             </LayoutTemplate>
             <ItemSeparatorTemplate> <td>   <hr /> </td>

             </ItemSeparatorTemplate>

              <ItemTemplate>
                <div >
                    <tr>
                       <td class="RowStyle"><%#Eval("ContractorNumber")%></td>
                       <td class="RowStyle"><%#Eval("FirstName")%></td>
                       <td class="RowStyle"><%#Eval("LastName")%></td>

                       <td class="RowStyle">
                           <asp:HyperLink ID="lnkView" runat="server" CssClass="link"
                                NavigateUrl='<%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&Mode=" + "CV" %>'
                                Target="_blank" Text="View" />
                       </td>
                       <td class="RowStyle">
                          <asp:HyperLink ID="lnkEdit" runat="server" CssClass="link"
                                NavigateUrl= '<%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&Mode=" + "CE" %>'
                                Text="Edit"> </asp:HyperLink>

                     </tr>
                  </div>
               </ItemTemplate>
               <AlternatingItemTemplate>
                 <div>
                    <tr>
                       <td class="AlternatingRowStyle"><%#Eval("ContractorNumber")%></td>
                       <td class="AlternatingRowStyle"><%#Eval("FirstName")%></td>
                       <td class="AlternatingRowStyle"><%#Eval("LastName")%></td>

                       <td class="AlternatingRowStyle">
                           <asp:HyperLink ID="lnkView" runat="server" CssClass="link"
                                NavigateUrl='<%# "Contractor.aspx?ID=" + Eval("ContractorNumber")%>'
                                Target="_blank" Text="View" />
                       </td>
                       <td class="AlternatingRowStyle">
                          <asp:HyperLink ID="lnkEdit" runat="server" CssClass="link"
                                NavigateUrl= '<%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&Mode=" + "CE" %>'
                                Text="Edit"> </asp:HyperLink>

                     </tr>
                  </div>

               </AlternatingItemTemplate>

               <ItemSeparatorTemplate>
                   <tr>
                      <td colspan="5" class="itemseparator"></td>
                  </tr>
              </ItemSeparatorTemplate>

     </asp:ListView>

     <div class="pager">
        <asp:DataPager PageSize="20" ID="DataPagerContractor" runat="server" PagedControlID="lvContractors"
                        NextPreviousButtonCssClass="PrevNext"
                        CurrentPageLabelCssClass="CurrentPage"
                        NumericButtonCssClass="PageNumber">
            <fields>
                 <asp:NumericPagerField
                      PreviousPageText="&lt; Prev"
                      NextPageText="Next &gt;"
                      ButtonCount="10"
                      NextPreviousButtonCssClass="PrevNext"
                      CurrentPageLabelCssClass="CurrentPage"
                      NumericButtonCssClass="PageNumber" />
            </fields>
        </asp:DataPager>
      </div>
</div> 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 & strSearch
        sql = sql & " 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() <> "" 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

结束班

1 个答案:

答案 0 :(得分:0)

很抱歉,但这不适用于datapager(至少在ASP.Net 3.5中)。 唯一的解决方案是从头开始编写自己的寻呼机,因为datapager可以传递的唯一查询字符串参数是页码。