How to do Paging with Repeater control

The following code shows how to do Paging with Repeater control


protected void Page_Load(object sender, EventArgs e)
{




if (!IsPostBack)
{
Application["index"] = 0;
display();

}









}
protected void previous_Click(object sender, EventArgs e)
{


if ( System.Convert.ToInt32(Application["index"]) > 1)
{


Application["index"] = System.Convert.ToInt32(Application["index"]) - 1;



}

display();

}
protected void next_Click(object sender, EventArgs e)
{
if (System.Convert.ToInt32(Application["index"]) < System.Convert.ToInt32(Application["count"]))
{

Application["index"] = System.Convert.ToInt32(Application["index"]) + 1;


}
display();

}
public void display()
{


OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/registration.mdb"));
conn.Open();
OleDbDataAdapter adpter = new OleDbDataAdapter("select * from students", conn);
DataSet data = new DataSet();
adpter.Fill(data);
PagedDataSource paging = new PagedDataSource();
paging.DataSource = data.Tables[0].DefaultView;
paging.AllowPaging = true;
paging.PageSize = 2;
Application["count"] = paging.PageCount;

paging.CurrentPageIndex = System.Convert.ToInt32(Application["index"]);
Application["current"] = paging.CurrentPageIndex;
result.DataSource = paging;
result.DataBind();

}
protected void result_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
if (System.Convert.ToInt32(Application["current"]) == 0)
{


((LinkButton)e.Item.FindControl("previous")).Enabled = false;


}
else
{

((LinkButton)e.Item.FindControl("previous")).Enabled = true;

}
}


if (e.Item.ItemType == ListItemType.Header)
{
if (System.Convert.ToInt32(Application["current"]) == System.Convert.ToInt32(Application["count"]) - 1)
{

((LinkButton)e.Item.FindControl("next")).Enabled = false;


}
else
{

((LinkButton)e.Item.FindControl("next")).Enabled = true;

}
}


}


Attachments

  • Paging with Repeater control (16319-15157-coding.zip)
  • Comments

    Author: husnain12 Nov 2010 Member Level: Bronze   Points : 1

    using System;
    using System.Data;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public partial class PagedRepeater : System.Web.UI.Page
    {
    public int PageNumber
    {
    get
    {
    if (ViewState["PageNumber"] != null)
    return Convert.ToInt32(ViewState["PageNumber"]);
    else
    return 0;
    }
    set
    {
    ViewState["PageNumber"] = value;
    }
    }

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    rptPages.ItemCommand +=
    new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    LoadData();
    }

    private void LoadData()
    {
    SqlConnection cn = new SqlConnection("your connection goes
    here");
    cn.Open();
    SqlDataAdapter da = new SqlDataAdapter("your query goes
    here", cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    cn.Close();

    PagedDataSource pgitems = new PagedDataSource();
    DataView dv = new DataView(dt);
    pgitems.DataSource = dv;

    pgitems.AllowPaging = true;
    pgitems.PageSize = 25;
    pgitems.CurrentPageIndex = PageNumber;

    if (pgitems.PageCount > 1)
    {
    rptPages.Visible = true;
    ArrayList pages = new ArrayList();
    for (int i = 0; i < pgitems.PageCount; i++)
    pages.Add((i + 1).ToString());
    rptPages.DataSource = pages;
    rptPages.DataBind();

    }
    else
    rptPages.Visible = false;

    rptItems.DataSource = pgitems;
    rptItems.DataBind();
    }

    void rptPages_ItemCommand(object source,
    RepeaterCommandEventArgs e)
    {
    PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
    LoadData();
    }


    }

    //code behind page ends here

    //aspx page starts here

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

    Paged Repeater










    Page: 


    CommandName="Page"
    CommandArgument="<%#
    Container.DataItem %>"
    CssClass="text"
    Runat="server"><%# Container.DataItem %>
     











    • <%# Eval("pkItemID") %>: <%# Eval("Description") %>













  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: