Oct 28, 2009

Creating Paging for a Repeater Control









using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected int iCatID = 0;
    private int iPageCount = 0;
    private int iPagePerLevel = 10;
    private int iRecordCount = 0;
    private int iStart;
    private int iCurPage;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindData();
        }
    }
    public void BindData()
    {
        if (Request.QueryString["Page"] != "" && Request.Params["Page"] != null)
        {
            iCurPage = Convert.ToInt32(Request.QueryString["Page"]);
        }
        else
        {
            iCurPage = 1;
        }
        if (Request.QueryString["St"] != "" && Request.Params["St"] != null)
        {
            iStart = Convert.ToInt32(Request.QueryString["St"]);
        }
        else
        {
            iStart = 1;
        }
        string connectionString = "server=localhost;user id=root;password=root;database=lm";
        MySqlConnection conn = new MySqlConnection(connectionString);
        conn.Open();
        MySqlCommand  cmd = conn.CreateCommand();
        cmd.CommandText = "Select * from packet_splitlog";
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "splitlog");//Insert all data to paged data source and then bind to the repeater
        PagedDataSource pPaging = new PagedDataSource();
        pPaging.DataSource = ds.Tables[0].DefaultView;
        pPaging.AllowPaging = true;
        pPaging.PageSize = 10;
        pPaging.CurrentPageIndex = iCurPage - 1;
        rptProducts.DataSource = pPaging;
        rptProducts.DataBind();
        iRecordCount = ds.Tables[0].Rows.Count; if (iRecordCount > iPagePerLevel)
        {
            Paging(iStart, iCurPage, pPaging.PageCount, iRecordCount);
        }
    }
    protected void rptPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
        ListItemType.Item)
        {
            LiteralControl objLit = (LiteralControl)(e.Item.Controls[0]); if (objLit.Text.IndexOf("|") == -1)
            {
                HyperLink lnkPage = (HyperLink)e.Item.FindControl("lnkPage"); if (iPageCount > 1)
                {
                    lblMorePages.Visible = true;
                }
                if (iCurPage.ToString() == e.Item.DataItem.ToString())
                {
                    lnkPage.NavigateUrl = "";
                    lnkPage.Attributes.Add("style", "font-weight:bold;");
                    lnkPage.ForeColor = System.Drawing.Color.Black;
                }
                else
                {
                    lnkPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + e.Item.DataItem.ToString() + "&St=" + iStart;
                }
            }
        }
    }
  
    public void Paging(int intStart, int intPage, int intPageCount, int
    intRecordCount)
    {
        int intEnd;
        ArrayList aryList = new ArrayList(); if (intPageCount < iPagePerLevel)
        {
            intStart = 1;
            intEnd = intPageCount;
        }
        else
        {
            if (intPage > intPageCount)
            {
                intPage = intPageCount;
            }
            else if (intPage < 1)
            {
                intPage = 1;
            }
            if (intPage == (intStart + iPagePerLevel - 1) || intPage == intStart)
            {
                intStart = intPage - (iPagePerLevel / 2); if (intStart <= 0)
                {
                    intStart = 1;
                }
            }
            intEnd = intStart + (iPagePerLevel - 1); if (intEnd > intPageCount)
            {
                intEnd = intPageCount;
                intStart = intPageCount - (iPagePerLevel - 1);
            }
        }
        if (intPage != 1)
        {
            if (intStart > 1)
            {
                LnkFirst.Text = "1 ... ";
                LnkLast.NavigateUrl =
                Request.CurrentExecutionFilePath + "?Page=1&St=1";
            }
        }
        if (intPage != intPageCount)
        {
            if (intEnd < intPageCount)
            {
                LnkLast.Text = " ... " + intPageCount;
                LnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
                Convert.ToString(intPageCount) + "&St=" + intPageCount;
            }
        }
        else
        {
            LnkLast.Visible = false;
        }
        for (int i = intStart; i <= intEnd; i++)
        {
            aryList.Add(i);
        }
        // display page number pageriStart = intStart;
        rptPages.DataSource = aryList;
        rptPages.DataBind();
    }


}

No comments:

Post a Comment