当数据集缩小但仍然有记录时,Listview返回EmptyDataTemplate

时间:2018-06-26 03:21:17

标签: c# asp.net

我能够成功绑定ListView,而没有返回100条记录的查询条件。 ListView的DataPager的页面大小为10。如果我更改查询条件以减少记录数(例如55),则会发生以下两种情况之一:

  1. 如果我位于初始ListView的第1页到第6页,则新的ListView将正确填充。
  2. 如果我在第7页或更高版本上,即使存在记录,ListView也会返回EmptyDataTemplate(我可以在VS的DataSet中看到它)。

Re#2,所需的操作是转到第1页。

.CS

     protected void bindfindListView()
    {
    SqlConnection conn = new SqlConnection(connectionString);
    if (conn.State == ConnectionState.Closed)
    {
        conn.Open();
    }

    string criteria = "";

        if (ddl.SelectedValue == "1" && !String.IsNullOrEmpty(txt.Text))
        {
            criteria = " AND CONTAINS(transactions.[blah], '\"*" + txt.Text + "*\"')";
        }

    SqlCommand cmdaccount = new SqlCommand("SELECT DISTINCT id, name, blah FROM nah.dbo.items WHERE email=@userstr" + criteria + " GROUP BY id, name, blah", conn);
    cmdaccount.Parameters.AddWithValue("@userstr", Session["user"].ToString());

    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmdaccount);
    DataSet myDataSet = new DataSet();
    mySqlAdapter.Fill(myDataSet);

    findlv.DataSource = myDataSet;
    findlv.DataBind();

    Session["findds"] = myDataSet;

    conn.Close();
}

protected void findlv_DataBound(object sender, EventArgs e)
{
    if (findlv.Items.Count > 0)
    {
        DataPager pager = (DataPager)findlv.FindControl("findpager");

        if (pager != null)
        {
            pager.Visible = (pager.PageSize < pager.TotalRowCount);
        }
    }
}

protected void findlv_PagePropertiesChanged(object sender, EventArgs e)
{
    bindfindListView();
}

.aspx

 <asp:ListView ID="findlv" runat="server" OnDataBound="findlv_DataBound" OnPagePropertiesChanged="findlv_PagePropertiesChanged" Visible="true">
   <LayoutTemplate>
       <table class="results" id="tblresults">
           <tr>
               <th class="tdpadleft col170">Property Address</th>
               <th class="col170">Property Name</th>
               <th class="col170">Seller/Landlord</th>
               <th class="col170">Buyer/Tenant</th>
               <th class="col78">Status</th>
               <th class="col100">Last Update</th>
               <th class="col50">Delete</th>
           </tr>
           <tr id="itemPlaceholder" runat="server"></tr>
       </table>
       <div class="pager2">
           <asp:DataPager ID="findpager" runat="server" PageSize="10" PagedControlID="findlv">
               <Fields>
                   <asp:NextPreviousPagerField ShowNextPageButton="False" ButtonCssClass="previousNextLink" />
                   <asp:NumericPagerField ButtonCount="10" ButtonType="Link" NumericButtonCssClass="numericLink" />
                   <asp:NextPreviousPagerField ShowPreviousPageButton="False" ButtonCssClass="previousNextLink" />
                   <asp:TemplatePagerField>
                       <PagerTemplate>
                           <div class="pagertotal">


Showing
                       <asp:Label runat="server" ID="CurrentPageLabel" Text='<%# Container.StartRowIndex + 1 %>' />
  to
                       <asp:Label runat="server" ID="TotalPagesLabel" Text='<%#(Container.StartRowIndex+Container.PageSize) > Container.TotalRowCount ? ((Container.StartRowIndex+Container.PageSize)-((Container.StartRowIndex+Container.PageSize)-Container.TotalRowCount)) : (Container.StartRowIndex+Container.PageSize) %>' />
  (of
  <asp:Label runat="server" ID="TotalItemsLabel" Text='<%# Container.TotalRowCount%>' />
  records)
                               </div>
                           </PagerTemplate>
                       </asp:TemplatePagerField>
                   </Fields>
               </asp:DataPager>
           </div>
       </LayoutTemplate>
       <ItemTemplate>
           <tr class="altRow rowcolor">
               <td>
                   <asp:Label runat="server" ID="lblid" Text='<%#((DataRowView)Container.DataItem)["id"] %>' />
               </td>
               <td>
                   <asp:Label runat="server" ID="lblname" Text='<%#((DataRowView)Container.DataItem)["name"] %>' />
               </td>
               <td>
                   <asp:Label runat="server" ID="lblblah" Text='<%#((DataRowView)Container.DataItem)["blah"] %>' />
               </td>
           </tr>
       </ItemTemplate>
             <EmptyDataTemplate>
           <table id="empty">
               <tr>
                   <td id="tdempty">No Transactions Found
                   </td>
               </tr>
           </table>
       </EmptyDataTemplate>
   </asp:ListView>

0 个答案:

没有答案