Article

Implement Custom Paging in Repeater control in ASP.Net

18 Aug 2016 vikram singh
0 Comments 954 Views



In this article i explain how to use / populate paging (Pagination) in repeater. By default repeater control does not have pagination so we need to write custom code for populate the paging.

HTML Markup

In HTML Markup one repeater is showing the data for students and othe repeater is used to populate the paging.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Paging in Repeater</title>
    
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="rptStudentData" runat="server">
            <HeaderTemplate>
                <table id="tbDetails" style="width: 500px; border-collapse: collapse;" border="1" cellpadding="5" cellspacing="0">
                    <tr style="background-color: #ff0000; height: 20px; color: #fff">
                        <th>Student Id</th>
                        <th>Student Name</th>
                        <th>Student Qualification</th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr style="height: 25px;">
                    <td>
                        <%#Eval("StudentId").ToString()%>
                    </td>
                    <td>
                        <%#Eval("StudentName").ToString()%>
                    </td>
                    <td>
                        <%#Eval("StudentQualification").ToString()%>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        <br />
        <br />
        <asp:Repeater ID="rptPaging" runat="server" OnItemCommand="rptPaging_ItemCommand">
            <ItemTemplate>
                <asp:LinkButton ID="lnkPage" style="padding: 8px; margin: 2px; color: #fff; font-weight: bold" Font-Underline="false"
                    CommandName="Page" CommandArgument="<%# Container.DataItem %>"
                    runat="server" Font-Bold="True"><%# Container.DataItem %>
                </asp:LinkButton>
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeater();
        }
    }
    // Binding Repeater Control with Paging
    private void BindRepeater()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("StudentId", typeof(Int32));
        dt.Columns.Add("StudentName", typeof(string));
        dt.Columns.Add("StudentQualification", typeof(string));
        dt.Rows.Add(1, "Vikram Singh", "B.Tech");
        dt.Rows.Add(2, "Uma Shankar Rai", "MCA");
        dt.Rows.Add(3, "Naresh Chandra", "MCA");
        dt.Rows.Add(4, "Naga Bhusan Jena", "B.Tech");
        dt.Rows.Add(6, "Anand Chauhan", "B.Tech");
        dt.Rows.Add(7, "Zaki Ahmad", "B.Tech");
        dt.Rows.Add(8, "Isha Phatik", "B.Tech");
        PagedDataSource pageds = new PagedDataSource();
        DataView dv = new DataView(dt);
        pageds.DataSource = dv;
        pageds.AllowPaging = true;
        pageds.PageSize = 3;
        if (ViewState["PageNumber"] != null)
            pageds.CurrentPageIndex = Convert.ToInt32(ViewState["PageNumber"]);
        else
            pageds.CurrentPageIndex = 0;
        if (pageds.PageCount > 1)
        {
            rptPaging.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pageds.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPaging.DataSource = pages;
            rptPaging.DataBind();
            for(int i=0;i<rptPaging.Items.Count;i++)
            {
                LinkButton lnkPage = (LinkButton)rptPaging.Items[i].FindControl("lnkPage");
               if(i==pageds.CurrentPageIndex)
               {
                   lnkPage.BackColor = Color.Red;
               }
                else
               {
                   lnkPage.BackColor = Color.Green;
               }
            }
        }
        else
        {
            rptPaging.Visible = false;
        }
        rptStudentData.DataSource = pageds;
        rptStudentData.DataBind();
    }
    // Binding Data on Page Item Change
    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument) - 1;
        BindRepeater();
    }
   
}

 

vikram singh

I am asp.net developer with 3+ years exp. in asp.net, jquery, javascript,SQL area knowledege.

Comments

No coments found to display!

Leave a Comment