User Control Code For Paging In ASP.NET 3.5

naimish's Avatar author of User Control Code For Paging In ASP.NET 3.5
This is an article on User Control Code For Paging In ASP.NET 3.5 in ASP.NET.
Rated 5.00 By 1 users

Introduction



To privide smooth paging mechanism while delaing with huge quantitys of DATA.

Background



Below code snippet can be placed in a user control and can be reused as and when we require paging to be performed on a web UI.

The code



Code: C#.NET

Code:
private int GetItems()
{
	//create new instance of PagedDataSource
	PagedDataSource objPds = new PagedDataSource();
	//set number of pages will appear
	objPds.PageSize = PageSize;
	objPds.DataSource = Ods.Select();
	objPds.AllowPaging = true;
	int count = objPds.PageCount;


	objPds.CurrentPageIndex = CurrentPage;
	if (objPds.Count > 0)
	{
		//dispaly controls if there are pages
		btnPrevious.Visible = true;
		btnNext.Visible = true;
		btnLastRecord.Visible = true;
		btnFirstRecord.Visible = true;
		lblCurrentPage.Visible = true;
		lblCurrentPage.Text = "Page " + Convert.ToString(CurrentPage + 1) + " of " + Convert.ToString(objPds.PageCount);
	}
	else
	{
		//disable controls if there are no pages
		btnPrevious.Visible = false;
		btnNext.Visible = false;
		btnLastRecord.Visible = false;
		btnFirstRecord.Visible = false;
		lblCurrentPage.Visible = false;
	}
	btnPrevious.Enabled = !objPds.IsFirstPage;
	btnNext.Enabled = !objPds.IsLastPage;
	btnLastRecord.Enabled = !objPds.IsLastPage;
	btnFirstRecord.Enabled = !objPds.IsFirstPage;
	//check for object control if it a DataList
	//we will use DList Variable
	if (ObjectControl is DataList)
	{
		DList = (DataList)ObjectControl;
		DList.DataSource = objPds;
		DList.DataBind();
	}
	//check for object control if it a Repeater
	//we will use Rep Variable
	else if (ObjectControl is Repeater)
	{
		Rep = (Repeater)ObjectControl;
		Rep.DataSource = objPds;
		Rep.DataBind();
	}

	return count;
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
	//back to previous page
	CurrentPage -= 1;
	GetItems();
}
protected void btnNext_Click(object sender, EventArgs e)
{
	//go to next page
	CurrentPage += 1;
	GetItems();
}

Thanks

Go4Expert Founder
3Oct2009,17:07   #2
shabbir's Avatar
Nomination for Article of the month - Sep 2009 Started. Nominate this article.