Thursday, June 7, 2012

Paging in Data List C# with .NET Code

On Form View Take
"DataList" and Name it as DataList1
"SqlDataSource" and Named it  as SqlDataSource1
Take a Label and named it as "lblCurrentPage"
take 2 button one for next and one for previous

With SqlDataSource
configure it with database and table

With grid view
set the "RepeatColumn" property and height width property

On Code window
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class DataListPaging : System.Web.UI.Page
{
    static int CurrentPage;
    protected void Page_Load(object sender, EventArgs e)
    {
        items();
    }
    protected void items()
    {
        PagedDataSource objDs = new PagedDataSource();
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        objDs.DataSource = dv;
        objDs.AllowPaging = true;
        objDs.PageSize = 6;
        objDs.CurrentPageIndex = CurrentPage;
        lblCurrentPage.Text = "Page:" + (CurrentPage + 1).ToString() + " Of " + objDs.PageCount.ToString();
        back1.Enabled = !objDs.IsFirstPage;
        next1.Enabled = !objDs.IsLastPage;
        DataList2.DataSource = objDs;
        DataList2.DataBind();

    }

    protected void next1_Click(object sender, EventArgs e)
    {
        try
        {

            CurrentPage += 1;
            items();
        }
        catch (Exception)
        {        }
    }
    protected void back1_Click(object sender, EventArgs e)
    {
        try
        {
            CurrentPage -= 1;
            items();
        }
        catch (Exception)
        {    }
    }
  
}



No comments:

Post a Comment