locked
DataPager does not work as expected. RRS feed

  • Question

  • User-909867351 posted

    Hi

    I have one ListView and onde DataPager connected to that ListView as follow:

    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="9">
                                <Fields>
                                    <asp:NextPreviousPagerField ShowNextPageButton="False" ButtonCssClass="numericButtonCSS" />                                
                                    <asp:NumericPagerField ButtonCount="9" ButtonType="Button" NumericButtonCssClass="numericButtonCSS" />                                
                                     <asp:NextPreviousPagerField ShowPreviousPageButton="False" ButtonCssClass="numericButtonCSS" />
                                </Fields>
                            </asp:DataPager>

    My ListView is connected to one sqldatasource. Everything works fine but when I filter this sqldatasource with this code:

     SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from bbbracelete where nome like CONCAT('%',?,'%')";
                SqlDataSource1.SelectParameters.Add("@a", txtFind.Text);
                SqlDataSource1.DataBind();
                ListView1.DataBind();
                DataPager1.DataBind();  

    The listview is filtered, the datapager show the correct number of pages but when I click in one of those pages the listview shows all the records. Maybe the problem is with the page postback.

    Any help?

    Thank you

    Friday, August 31, 2018 4:23 PM

All replies

  • User475983607 posted

    If you are using SQL server the code shown always returns every record.  If you are using some other DB engine then maybe the txtFind textbox is empty which will produce all records as well.

    The quickest way to a solution is setting a breakpoint and debugging the code.  View the SQL script and parameter values for correctness and try running the resulting SQL against  the DB.

    Friday, August 31, 2018 4:54 PM
  • User-909867351 posted

    Hi

    I'm using Mysql and the filter works fine. The problem is when I hit the buttom 

    Please visit

    https://blog.cheirabem.com/braceletes-pulseiras-artigo-moda-atual/

    Click in the find icon and write masculino

    At the button you get 5 pages, the filter works fine. If you select page 2 you see the filter gone away and you get all the records. I Think the problem is with postback of the page.

    Friday, August 31, 2018 5:39 PM
  • User475983607 posted

    mariolopes

    At the button you get 5 pages, the filter works fine. If you select page 2 you see the filter gone away and you get all the records. I Think the problem is with postback of the page.

    There is not enough code to guess where your logical bug is located.  I used dev tools to see the POST and found the txtFind value is submitted.

    My best guess is you have code elsewhere that is binding the listview to a query that returns all the records. 

    You need to learn how to troubleshoot and debug.  Set a breakpoint and step though your code.  You wrote the code and know how you expect the code to function.  As you step  through the code you should be able to identify the point where what you think should happen and what actually happens differs.  This is where the bug(S) is located.

    Friday, August 31, 2018 6:08 PM
  • User-909867351 posted

    I made one test

    The following code is very simple, it has one listview, one datasource, one DataPager.

    ascx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="lixo.aspx.cs" Inherits="lixo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"/>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Procura" />
                <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" DataSourceID="SqlDataSource1" GroupItemCount="3">
                    <AlternatingItemTemplate>
                        <div class="col-md-4">
                            <div class="card border-primary">
                                <div class="card-header border-success ">
                                    <div class="row">
                                        <div class="col-12 text-center">
                                            <%--<asp:LinkButton ID="LinkButton2" OnClick="Duvida" runat="server">Pergunta sobre este produto</asp:LinkButton>--%>
                                        </div>
                                        
                                    </div>
                                </div>                            
                                <asp:Image ID="Image3" class="img card-img-top" runat="server" ImageUrl='<%#Eval("imagens") %>' AlternateText='<%# Eval("nome") %>' ToolTip='<%# Eval("nome")  %>' />
                                
                                    <div class="card-body">
                                    <h5 class="card-title">
                                        
                                        <asp:Label ID="lblProduto" runat="server" Text='<%# Eval("nome") %>' />
                                        <small>                                     
                                            <asp:Label ID="lblSku" runat="server" Text='<%# Eval("sku") %>' /></small></h5>                                                                 
                                    <div class="card-footer">
                                        <div class="row pt-1">
                                            <div class="col-lg-7 text-center">
                                                
                                                <h3><span class="text-success">Preço:<asp:Label ID="preco_venda" runat="server" Text='<%# Eval("pvr") %>' />€ </span></h3>
                                            </div>
                                            <div class="col-lg-5 text-center">
                                                <asp:Button ID="Button2" runat="server" CssClass="btn-success" Text="Comprar"   />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </AlternatingItemTemplate>                
                    <EmptyDataTemplate>
                        <table runat="server" style="">
                            <tr>
                                <td>Não foram devolvidos dados.</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>                
                    <GroupTemplate>
                        <hr />
                                <div class="row ">
                                <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                                </div>
                    </GroupTemplate>                    
                    <ItemTemplate>
                     <div class="col-md-4">
                            <div class="card border-primary">
                                <div class="card-header border-success ">
                                    <div class="row">
                                        <div class="col-12 text-center">
                                            <%--<asp:LinkButton ID="LinkButton2" OnClick="Duvida" runat="server">Pergunta sobre este produto</asp:LinkButton>--%>
                                        </div>
                                        
                                    </div>
                                </div>                            
                                <asp:Image ID="Image3" class="img card-img-top" runat="server" ImageUrl='<%#Eval("imagens") %>' AlternateText='<%# Eval("nome") %>' ToolTip='<%# Eval("nome")  %>' />
                                
                                    <div class="card-body">
                                    <h5 class="card-title">
                                        
                                        <asp:Label ID="lblProduto" runat="server" Text='<%# Eval("nome") %>' />
                                        <small>                                     
                                            <asp:Label ID="lblSku" runat="server" Text='<%# Eval("sku") %>' /></small></h5>                                                                 
                                    <div class="card-footer">
                                        <div class="row pt-1">
                                            <div class="col-lg-7 text-center">                                            
                                                <h3><span class="text-success">Preço:<asp:Label ID="preco_venda" runat="server" Text='<%# Eval("pvr") %>' />€ </span></h3>
                                            </div>
                                            <div class="col-lg-5 text-center">
                                                <asp:Button ID="Button2" runat="server" CssClass="btn-success" Text="Comprar"   />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <div class="row ">
                            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                        </div>
                    </LayoutTemplate>                
                </asp:ListView>
                 <div id="pager">
                            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="9">
                                <Fields>
                                    <asp:NextPreviousPagerField ShowNextPageButton="False" ButtonCssClass="numericButtonCSS" />                                
                                    <asp:NumericPagerField ButtonCount="9" ButtonType="Button" NumericButtonCssClass="numericButtonCSS" />                                
                                     <asp:NextPreviousPagerField ShowPreviousPageButton="False" ButtonCssClass="numericButtonCSS" />
                                </Fields>
                            </asp:DataPager>
                        </div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:perfumes %>" ProviderName="<%$ ConnectionStrings:perfumes.ProviderName %>" SelectCommand="select * from bbcolares"></asp:SqlDataSource>
            </div>
            <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
        </form>
    </body>
    </html>

    and cs 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class lixo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Procura(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters.Clear();
            SqlDataSource1.SelectCommand = "select * from bbcolares where nome like CONCAT('%',?,'%')";
            SqlDataSource1.SelectParameters.Add("@a", TextBox1.Text);
            SqlDataSource1.DataBind();
            ListView1.DataBind();
            DataPager1.DataBind();
        }
    
    }

    The Datapager seems to loose the filter

    Friday, August 31, 2018 10:41 PM
  • User475983607 posted

    mariolopes

    I made one test

    The following code is very simple, it has one listview, one datasource, one DataPager.

    According to the code only Button1 is wired to the Procura event handler and only the Procura event handler filters the ListView.  You've code the DataSouce to to select all records and that's why clicking DataPage shows all records.  

    If you would have simply placed a breakpoint on the Procura event and ran the debugger this fact would have been abundantly clear.  The page is working exactly as you coded it.

    I would place the filter logic is a separate method and call that method from the Procura event handler as well as the Pager Click handlers.

    See the DataPage API docs.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.datapager?view=netframework-4.7.2

    Friday, August 31, 2018 11:01 PM
  • User-893317190 posted

    Hi mariolopes,

    I have simulated your code , but I couldn't reproduce your problem.

    In my case , even I click the page button , the data is still filtered.

    It may be because your select command in your sqldatasource is " SelectCommand="select * from bbcolares" which selects all the record in your database.So when the page posts back , data which the listview bind is the result of this sql.

    I suggest you could modify your sqldatasource . In the configuration wizard, please choose where option  to specify where  clause.

    Below is my code .

      <asp:ListView ID="ListView1" runat="server" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
                  
                  
                   <EmptyDataTemplate>
                       <table runat="server" style="">
                           <tr>
                               <td>No data was returned.</td>
                           </tr>
                       </table>
                   </EmptyDataTemplate>
                  
                   <ItemTemplate>
                       <tr style="">
                           <td>
                               <asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
                           </td>
                           <td>
                               <asp:Label ID="CompanyNameLabel" runat="server" Text='<%# Eval("CompanyName") %>' />
                           </td>
                           <td>
                               <asp:Label ID="ContactNameLabel" runat="server" Text='<%# Eval("ContactName") %>' />
                           </td>
                           <td>
                               <asp:Label ID="ContactTitleLabel" runat="server" Text='<%# Eval("ContactTitle") %>' />
                           </td>
                           <td>
                               <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
                           </td>
                           <td>
                               <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
                           </td>
                           <td>
                               <asp:Label ID="RegionLabel" runat="server" Text='<%# Eval("Region") %>' />
                           </td>
                           <td>
                               <asp:Label ID="PostalCodeLabel" runat="server" Text='<%# Eval("PostalCode") %>' />
                           </td>
                           <td>
                               <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
                           </td>
                           <td>
                               <asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' />
                           </td>
                           <td>
                               <asp:Label ID="FaxLabel" runat="server" Text='<%# Eval("Fax") %>' />
                           </td>
                       </tr>
                   </ItemTemplate>
                   <LayoutTemplate>
                       <table runat="server">
                           <tr runat="server">
                               <td runat="server">
                                   <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                       <tr runat="server" style="">
                                           <th runat="server">CustomerID</th>
                                           <th runat="server">CompanyName</th>
                                           <th runat="server">ContactName</th>
                                           <th runat="server">ContactTitle</th>
                                           <th runat="server">Address</th>
                                           <th runat="server">City</th>
                                           <th runat="server">Region</th>
                                           <th runat="server">PostalCode</th>
                                           <th runat="server">Country</th>
                                           <th runat="server">Phone</th>
                                           <th runat="server">Fax</th>
                                       </tr>
                                       <tr id="itemPlaceholder" runat="server">
                                       </tr>
                                   </table>
                               </td>
                           </tr>
                           <tr runat="server">
                               <td runat="server" style="">
                                  
                               </td>
                           </tr>
                       </table>
                   </LayoutTemplate>
                   
               </asp:ListView>
             <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="7">
                                       <Fields>
                                           <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                           <asp:NumericPagerField />
                                           <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                       </Fields>
                                   </asp:DataPager>
    
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="search" OnClick="Button1_Click" />
               <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindDbContext %>" SelectCommand="SELECT * FROM [Customers] WHERE ([ContactName] LIKE '%' + @ContactName2 + '%')">
                   <SelectParameters>
                       <asp:Parameter DefaultValue=" " Name="ContactName2" Type="String" />
                   </SelectParameters>
               </asp:SqlDataSource>

    Code behind.

       protected void Button1_Click(object sender, EventArgs e)
            {
               
                if (!string.IsNullOrEmpty(TextBox1.Text))
                {
                    SqlDataSource1.SelectParameters.Clear();
                    SqlDataSource1.SelectParameters.Add("ContactName2", TextBox1.Text.Trim());
                    SqlDataSource1.DataBind();
                    ListView1.DataBind();
                    DataPager1.DataBind();
                }
                else
                {
                    SqlDataSource1.SelectParameters.Clear();
                    
                    SqlDataSource1.SelectParameters.Add("ContactName2", " ");
                    SqlDataSource1.DataBind();
                    ListView1.DataBind();
                    DataPager1.DataBind();
    
                }
                
             
            }

    Best regards,

    Ackerly Xu

    Monday, September 3, 2018 7:33 AM
  • User-909867351 posted

    Hi 

    I found the solution in the following code:

    In the Find buttom

     protected void Procurar_Artigo(object sender, EventArgs e)
        {
            if (txtFind.Text != "")
            {
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from bbcolares where nome like CONCAT('%',?,'%')";
                SqlDataSource1.SelectParameters.Add("@a", txtFind.Text);
                SqlDataSource1.DataBind();
                ListView1.DataBind();
                DataPager1.DataBind();
    
            }
            else
            {
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from bbcolares ";
                SqlDataSource1.DataBind();
                ListView1.DataBind();
                DataPager1.DataBind();
            }
        }

    and in the Listview

     protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
             if (txtFind.Text != "")
            {
                DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from bbcolares where nome like CONCAT('%',?,'%')";
                SqlDataSource1.SelectParameters.Add("@a", txtFind.Text);
                SqlDataSource1.DataBind();
                ListView1.DataBind();
                DataPager1.DataBind();
    
            }
        }

    Monday, September 3, 2018 10:42 AM