Having trouble with paging in asp repeater

1.2k views Asked by At

I have this aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchCustomer.aspx.cs" Inherits="WebApplication1.eyeofheaven.SearchCustomer" %>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta name="viewport" content="width = device-width, initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 1.0, user-scalable = no" />
    <!-- Stylesheets -->
    <link rel="stylesheet" type="text/css" href="StyleSheets/SearchCustomerStyle.css">
    <link rel="stylesheet" href="Plugins/bootstrap-3.3.4-dist/css/bootstrap.min.css">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Pagination Script -->
    <!--Customer Script-->
    <script src="JavaScripts/SearchCustomerJavascript.js"></script>

    <title>Search Customer</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <form id="Form1" class="form1" runat="server">
                <div class="row">
                    <div class="twelve columns">
                        <!-- Header-->
                        <div class="container">
                            <nav role="navigation" class="navbar navbar-inverse navbar-fixed-top">
                                <!-- Brand and toggle get grouped for better mobile display -->
                                <div class="navbar-header">
                                    <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                                        <span class="sr-only">Toggle navigation</span>
                                        <span class="icon-bar"></span>
                                        <span class="icon-bar"></span>
                                        <span class="icon-bar"></span>
                                    </button>
                                </div>
                                <!-- Collection of nav links, forms, and other content for toggling -->
                                <div id="navbarCollapse" class="collapse navbar-collapse">
                                    <ul class="nav navbar-nav">
                                        <li><a href="EyeOfHeaven.aspx">Home</a></li>
                                        <li class="dropdown">
                                            <a data-toggle="dropdown" class="dropdown-toggle active" href="#">Search<b class="caret"></b></a>
                                            <ul role="menu" class="dropdown-menu">
                                                <li><a href="SearchCustomer.aspx">Search Form(Customer)</a></li>
                                                <li><a href="SearchVehicle.aspx">Search Form(Vehicle)</a></li>
                                            </ul>
                                        </li>
                                    </ul>
                                </div>
                            </nav>
                        </div>
                    </div>
                </div>

                <!-- Search form customer-->
                <div id="searchcustomer" class="page-header">
                    <h3><span class="glyphicon glyphicon-th-large"></span>Search Customer</h3>
                </div>

                <div class="row">
                    <div class="col-md-4">
                        <input type="text" runat="server" id="search" size="20" class="form-control" placeholder="Customer ID">
                    </div>
                    <div class="col-md-4">
                        <select class="form-control" runat="server" id="Country">
                            <option value="select" selected disabled>Search by Country</option>
                            <option value="A:C ESTUDIO">A:C ESTUDIO</option>
                            <option value="Aaron McEwen-194712">Aaron McEwen-194712</option>
                            <option value="Accra">Accra</option>
                            <option value="Adoany">Adoany</option>
                            <option value="Aduanas">Aduanas</option>
                            <option value="Alex Sanchez-259029">Alex Sanchez-259029</option>
                            <option value="ALG Consulting-288078">ALG Consulting-288078</option>
                            <option value="Algeria">Algeria</option>
                            <option value="Algimantas Ramaskevicius">Algimantas Ramaskevicius</option>
                            <option value="Allan Demeritte-233953">Allan Demeritte-233953</option>
                            <option value="Althea  Gibson-223990">Althea  Gibson-223990</option>
                            <option value="Alvaro Delgado-279974">Alvaro Delgado-279974</option>
                            <option value="Amber Williamsen-212332">Amber Williamsen-212332</option>
                            <option value="Amos Peters-217784">Amos Peters-217784</option>
                            <option value="andersongordon-234453">andersongordon-234453</option>
                            <option value="Andrey Bolshakov-189544">Andrey Bolshakov-189544</option>
                            <option value="Angela green-220269">Angela green-220269</option>
                            <option value="ANGELMILLER-179184">ANGELMILLER-179184</option>
                            <option value="Angola">Angola</option>
                            <option value="Anguilla">Anguilla</option>
                            <option value="Anthony Knight-235064">Anthony Knight-235064</option>
                            <option value="Antigua and Barbuda">Antigua and Barbuda</option>
                        </select>
                    </div>

                    <div class="col-md-4">
                        <select class="form-control" runat="server" id="Currency">
                            <option value="selected" selected disabled>Search by Currency</option>
                            <option value="AUD">AUD (Australian Dollar)</option>
                            <option value="EUR">EUR (Euro)</option>
                            <option value="GBP">GBP (United Kingdom Pounds)</option>
                            <option value="JPY">JPY (Japan Yen)</option>
                            <option value="NZD">NZD (New Zealand Dollar)</option>
                            <option value="USD">USD (United States Dollar)</option>
                        </select>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-4">
                        <button type="button" runat="server" onserverclick="Button1_Click" id="searchinfo" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>Search Info</button>
                        <button type="button" runat="server" onserverclick="Button2_Click" id="Button2" class="btn btn-danger"><span class="glyphicon glyphicon-repeat"></span>Reset</button>
                    </div>
                </div>

                <hr style="border-top: dotted 1px;" />

             <!-- Information Table-->
            <div class="row">
                <asp:Repeater runat="server" ID="repeater">
                    <ItemTemplate>
                        <div class="col-md-8">
                            <div id="panelmain" class="panel panel-default">
                                <div id="panel-heading" class="panel-heading">
                                    <h3 id="name" align="center" class="panel-title"><%# String.Format("{0} {1}", DataBinder.Eval(Container.DataItem, "Firstname").ToString(), DataBinder.Eval(Container.DataItem, "Lastname").ToString()) %></h3>
                                </div>
                                <div id="panelbody" class="panel-body">
                                    <ul class="list-unstyled">
                                        <li>
                                            <span class="spanlist">IDCustomer : </span><%# DataBinder.Eval(Container.DataItem, "IDCustomer").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDAccountManager : </span><%# DataBinder.Eval(Container.DataItem, "IDAccountManager").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDBillingAddress : </span><%# DataBinder.Eval(Container.DataItem, "IDBillingAddress").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDCountry : </span><%# DataBinder.Eval(Container.DataItem, "IDCountry").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDCredit : </span><%# DataBinder.Eval(Container.DataItem, "IDCredit").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDFrequency : </span><%# DataBinder.Eval(Container.DataItem, "IDFrequency").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDOwner : </span><%# DataBinder.Eval(Container.DataItem, "IDOwner").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDPort : </span><%# DataBinder.Eval(Container.DataItem, "IDPort").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDSite : </span><%# DataBinder.Eval(Container.DataItem, "IDSite").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">IDRecipient : </span><%# DataBinder.Eval(Container.DataItem, "IDRecipient").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">AccessType : </span><%# DataBinder.Eval(Container.DataItem, "AccessType").ToString() %>
                                        </li>
                                        <li>
                                            <span class="spanlist">Active : </span><%# DataBinder.Eval(Container.DataItem, "Active").ToString() %>
                                        </li>
                                    </ul>

                                </div>
                            </div>
                        </div>

                    </ItemTemplate>
                </asp:Repeater>
        </div>

            <div>
                <asp:LinkButton ID="linkPrevious" runat="server" NavigateUrl="~/SearchCustomer.aspx" Font-Bold="true" OnClick="linkPrevious_Click">Prev Page</asp:LinkButton>
                <asp:LinkButton ID="linkNext" runat="server" NavigateUrl="~/SearchCustomer.aspx" Font-Bold="true" OnClick="linkNext_Click">Next Page</asp:LinkButton>
            </div>
</form>
</body>
</html>

and its aspx code behind:

using MSSQLConnector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.eyeofheaven
{

    public partial class SearchCustomer : System.Web.UI.Page
    {
        public static string query = null;
        private int cnt;
        private DataSet selectedData;
        private DataTable dt;
        private MSConnector connector = new MSConnector();

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Function for BindRepeater 
            BindRepeater();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
           this.repeater.Visible = false;
           this.search.Value = "";
           this.Country.Value = "select";
           this.Currency.Value = "selected";
        }

        //This property will contain the current page number 
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 0;
                }
            }
            set { ViewState["PageNumber"] = value; }
        }

        //Asp:ListView
        private void BindRepeater()
        {
            //ConnectionString for accessing into MSSql
            connector.ConnectionString = "SERVER=xbetasql,52292;UID=username;Password=secret;DATABASE=ATDBSQL;";

            //Get the values from id's
            string customer = (this.search.Value);
            string country = (this.Country.Value);
            string idcurrency = (this.Currency.Value);

            //Conditions for query
            if (country != "select")
            {
                if (idcurrency != "selected")
                {
                    query = "select * from customer where country = '" + country + "' and idcurrency = '" + idcurrency + "'";
                }
                else
                {
                    query = "select * from customer where country = '" + country + "'";
                }
            }
            else if (idcurrency != "selected")
            {
                query = "select * from customer where idcurrency = '" + idcurrency + "'";
            }
            else if ((this.search.Value) == customer)
            {
                query = "select * from customer where idcustomer = '" + customer + "'";
            }
            else if (customer == "")
            {
                Response.Write("<script>alert('No Id Inputted, Data Not Found.')</script>");
            }

            //DataSet and DataTable (get the data and display it into asp:repeater
            selectedData = connector.ExecuteQuery(query);
            dt = selectedData.Tables[0];

            //Set PageData Settings
            PagedDataSource pagedData = new PagedDataSource();
            pagedData.DataSource = dt.DefaultView;
            pagedData.AllowPaging = true;
            pagedData.PageSize = 3;
            pagedData.CurrentPageIndex = PageNumber;

            int vcnt = cnt / pagedData.PageSize;

            if (PageNumber < 1)
            {
                linkPrevious.Visible = false;
            }
            else if (PageNumber > 0)
            {
                linkPrevious.Visible = true;
            }
            if (PageNumber == vcnt)
            {
                linkNext.Visible = false;
            }
            else if(PageNumber < vcnt)
            {
                linkNext.Visible = true;
            }
            //Binding the repeater 
            repeater.Visible = true;
            repeater.DataSource = pagedData;
            repeater.DataBind();
           //Binding the repeater 
        }

        protected void linkNext_Click(object sender, EventArgs e)
        {
            PageNumber += 1;
            BindRepeater();
        }
        protected void linkPrevious_Click(object sender, EventArgs e)
        {
            PageNumber -= 1;
            BindRepeater();
        }

    }
}

My asp:LinkButton, does not work for paging in my repeater when I first open my web page the asp:linkbutton is there, but when searched and display all the data from my page the linkbutton is gone although it display data that I searched for. But the problem is pagination doesn't seem to work. What seems to be the problem for my code? Here's the link as a reference for my code.

I'm new to c# programming(webform) and I have few knowledge about pagination in c#.

2

There are 2 answers

1
Bhavin On BEST ANSWER

It's because of

 int vcnt = cnt / pagedData.PageSize;

'cnt' you haven't set, So its always zero that's why 'vcnt' will zero. that's why your link will visible false always after you search things.

So Solution is

 int vcnt = dt.DefaultView.Count / pagedData.PageSize;

Hope this will solve your problem

Happy Coding :)

0
laurence keith albano On

I got it right :) here's the code for pagination:

using MSSQLConnector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.eyeofheaven
{

    public partial class SearchCustomer : System.Web.UI.Page
    {
        public static string query = null;
        private int cnt;
        private DataSet selectedData;
        private DataTable dt;
        private MSConnector connector = new MSConnector();

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Function for BindRepeater 
            BindRepeater();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
           this.repeater.Visible = false;
           this.linkPrevious.Visible = false;
           this.linkNext.Visible = false;
           this.search.Value = "";
           this.Country.Value = "select";
           this.Currency.Value = "selected";
        }

        //This property will contain the current page number 
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 0;
                }
            }
            set { ViewState["PageNumber"] = value; }
        }

        //Asp:ListView
        private void BindRepeater()
        {
            //ConnectionString for accessing into MSSql
            connector.ConnectionString = "SERVER=xbetasql,52292;UID=username;Password=secret;DATABASE=ATDBSQL;";

            //Get the values from id's
            string customer = (this.search.Value);
            string country = (this.Country.Value);
            string idcurrency = (this.Currency.Value);

            //Conditions for query
            if (customer != "")
            {
                query = "select * from customer where idcustomer = '" + customer + "'";
            }
            else if (country != "select")
            {
                if (idcurrency != "selected")
                {
                    query = "select * from customer where country = '" + country + "' and idcurrency = '" + idcurrency + "'";
                }
                else
                {
                    query = "select * from customer where country = '" + country + "'";
                }
            }
            else if (idcurrency != "selected")
            {
                query = "select * from customer where idcurrency = '" + idcurrency + "'";
            }
            else if (customer == "")
            {
                Response.Write("<script>alert('No Id Inputted, Data Not Found.')</script>");
            }
            else
            {
                Response.Write("<script>alert('Invalid input.')</script>");
            }
            //DataSet and DataTable (get the data and display it into asp:repeater
            selectedData = connector.ExecuteQuery(query);
            dt = selectedData.Tables[0];

            //Set PageData Settings
            PagedDataSource pagedData = new PagedDataSource();
            pagedData.DataSource = dt.DefaultView;
            pagedData.AllowPaging = true;

            //Set to 2 pages to be viewed
            pagedData.PageSize = 2;
            pagedData.CurrentPageIndex = PageNumber;

            //Count to 2 pages to appear when clicking next or previous
            int vcnt = dt.DefaultView.Count / pagedData.PageSize;

            if (PageNumber == 1)
            {
                linkPrevious.Visible = false;
            }
            else if (PageNumber < 1)
            {
                linkPrevious.Visible = false;
            }
            else if (PageNumber > 0)
            {
                linkPrevious.Visible = true;
            }

            if (PageNumber == vcnt)
            {
                linkNext.Visible = false;
            }
            //Hide previous and next if it is firstpage and lastpage
            else if(PageNumber < vcnt)
            {
                linkNext.Visible = !pagedData.IsLastPage;
                linkPrevious.Visible = !pagedData.IsFirstPage;
            }

           //Binding the repeater 
           if (dt.Rows.Count > 0)
            {
                repeater.Visible = true;
                repeater.DataSource = pagedData;
                repeater.DataBind();
            }
            else
            {
                Response.Write("<script>alert('No Data Found.')</script>");
            }
        }

        protected void linkNext_Click(object sender, EventArgs e)
        {
            linkNext.Visible = true;
            PageNumber += 1;
            BindRepeater();
        }
        protected void linkPrevious_Click(object sender, EventArgs e)
        {
            linkPrevious.Visible = true;
            PageNumber -= 1;
            BindRepeater();
        }

    }
}

Hope this is a good reference among programmers. :)