Simple Paging Control

HOW TO
Use with Repeater
Page - Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPaging.aspx.cs" Inherits="TestPaging" %>

<%@ Register Assembly="ncToolControls" Namespace="ncToolControls" TagPrefix="nc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Paging</title>
    <style type="text/css">
        body {font-family: Verdana, Arial, MS Sans Serif; font-size: 9pt; vertical-align: middle;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p align="center"><nc:Paging ID="ctrlPg"  runat="server" /></p> 
         <table style="border: 1px solid black; vertical-align: middle;" width="795px;"  cellpadding="1" cellspacing="0" align="center" id="table5">        
            <tr bgcolor="silver">
                <td style="font-weight: bold; vertical-align: middle;" align="center">City</td>
                <td style="font-weight: bold; vertical-align: middle;" align="center">State</td>
                <td style="font-weight: bold; vertical-align: middle;" align="center">Zipcode</td>                
            </tr>      
            <asp:Repeater ID="rptList" runat="server" >                     
                <ItemTemplate>       
                    <tr>                              
                        <td style="border: 1px solid black; vertical-align: middle;" align="left"><%# DataBinder.Eval(Container.DataItem, "city_name") %></td>
                        <td style="border: 1px solid black; vertical-align: middle;" align="center"><%# DataBinder.Eval(Container.DataItem, "state_abbv") %></td>               
                        <td style="border: 1px solid black; vertical-align: middle;" align="left"><%# DataBinder.Eval(Container.DataItem, "zipcode")%></td>                              
                    </tr>
                </ItemTemplate>            
            </asp:Repeater>                    
         </table>
    </div>   
    </form>
</body>
</html>
Code behind - Default.aspx.cs
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncTools;

public partial class TestPaging : System.Web.UI.Page
{
    private oMyODBC oDB = new oMyODBC();
    private string strConn = ConfigurationManager.ConnectionStrings["sqldb2_ConnectionString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ctrlPg.btnFirstRow.Command += btnFirstRow_Click;
        this.ctrlPg.btnPrevious.Command += btnPrev_Click;
        this.ctrlPg.btnNext.Command += btnNext_Click;
        this.ctrlPg.btnLastRow.Command += btnLastRow_Click;
        LoadRepeaterDataListing();
    }
    protected void LoadRepeaterDataListing()
    {
        this.ctrlPg.PagingSize = 25;
        this.ctrlPg.LoadPageDataSource(LoadDataListing());
        this.ctrlPg.lblPagingMessage.Text = this.ctrlPg.PagingMessage;
               
        rptList.DataSource = this.ctrlPg.Paging_DataSource;
        rptList.DataBind();
    }
    private DataTable LoadDataListing()
    {   
        DataView dv = new DataView();
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("city_name"));
        dt.Columns.Add(new DataColumn("state_abbv"));
        dt.Columns.Add(new DataColumn("zipcode"));
        DataRow dr;

        string sTSQL1 = "select cityname,stateabbrv,zipcode from tblzipcodes";
        //-sTSQL1 += "  from tblzipcodes";

        SqlDataReader oRs = oDB.SqlRecordSet(sTSQL1, strConn);

        if (oRs.HasRows ==true)
        {
            if (oRs["cityname"].ToString().Trim() != "System.Data.SqlClient.SqlDataReader")
            {
                dr = dt.NewRow();
                dr["city_name"] = oRs["cityname"].ToString();
                dr["state_abbv"] = oRs["stateabbrv"].ToString();
                dr["zipcode"] = oRs["zipcode"].ToString();
                dt.Rows.Add(dr);
            }
            while (oRs.Read())            
            {
                if (oRs["cityname"].ToString().Trim() != "System.Data.SqlClient.SqlDataReader")
                {
                    dr = dt.NewRow();
                    dr["city_name"] = oRs["cityname"].ToString();
                    dr["state_abbv"] = oRs["stateabbrv"].ToString();
                    dr["zipcode"] = oRs["zipcode"].ToString();
                    dt.Rows.Add(dr);
                }
            }
        }
        oRs.Close();        
        dt.AcceptChanges();
        return dt;
    }
    protected void btnFirstRow_Click(object sender, System.EventArgs e)
    {
        //- Set viewstate variable to the previous page
        ctrlPg.CurrentPage = 0;
        //- Reload control
        LoadRepeaterDataListing();
    }
    protected void btnPrev_Click(object sender, System.EventArgs e)
    {
        //- Set viewstate variable to the previous page
        ctrlPg.CurrentPage -= 1;
        //- Reload control
        LoadRepeaterDataListing();
    }
    protected void btnNext_Click(object sender, System.EventArgs e)
    {
        //- Set viewstate variable to the next page
        ctrlPg.CurrentPage += 1;
        //- Reload control
        LoadRepeaterDataListing();
    }
    protected void btnLastRow_Click(object sender, System.EventArgs e)
    {
        //- Set viewstate variable to the previous page
        ctrlPg.CurrentPage = this.ctrlPg.Paging_DataSource.PageCount-1;
        //- Reload control
        LoadRepeaterDataListing();
    }
}
 

Add Feedback