Click here to Skip to main content
15,892,927 members
Articles / Web Development / ASP.NET

Generic Paging for Recordset with Next Back Navigation

Rate me:
Please Sign up or sign in to vote.
3.55/5 (5 votes)
20 Jun 2005CPOL1 min read 32.6K   582   29  
This article is basically about a Paging control required for project purposes. One can use this just by changing three parameters.
//Programmer: Santosh Poojari
//--------------------------------------------------
//Date : 21/05/05
//--------------------------------------------------
//Description :Generic Paging Control
//Email_id:Santosh_poojari@rediffmail.com 
//--------------------------------------------------

#region Libraries
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Text;
#endregion 

namespace Paging
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm3 : System.Web.UI.Page
	{
		#region Private & Protected Variables
		protected System.Web.UI.WebControls.Table tbPickList;
		protected System.Web.UI.WebControls.Table Table1;
		protected System.Web.UI.WebControls.Table tblPaging;
		protected TableRow tr;
		//------------------------------------------------------
		//private const int Constant.iShowNoPage = 5;//-Change Parameter
		//private static int Constant.jIter = 5;//------Change Parameter
		//private const int Constant.jVirtualInc = 5;
		//private const int Constant.iInitialRecDisplay = 10;
		private const int zVirtualInc = 1;
		private  int iTotalRec;//Change Paramater
		private static int iFirstStepMod;
		private static int iLastStep;
		private static int zIter=1;
		private  static int iCntMovePtr=1;
		private int iTotalPages;
		//---------------------------------------------------
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.DropDownList ddlTotalRec;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Button btnFirstRec;
		protected System.Web.UI.WebControls.Button btnLastRec;
		protected System.Web.UI.WebControls.Button btnNext;
		protected System.Web.UI.WebControls.Button btnPrev;
		protected System.Web.UI.WebControls.Button Button1;


		private DataTable tblRec ;
		private TableRow trRec;
		protected static string strConn=System.Configuration.ConfigurationSettings.AppSettings["DSNConn"];
		private static int iCurrentPage=1;
		private static string strPageNo="1";

		protected System.Web.UI.WebControls.Table tblRecordset;
		#endregion
		
		#region On Page Load
		public void Page_Load(object sender, System.EventArgs e)
		{
		
			if(iCntMovePtr==1 && strPageNo=="1")
			{
				string strSelectCat="SELECT CustomerID, CompanyName FROM Customers";//ddlTotalRec.SelectedItem;//"SELECT CustomerID, CompanyName FROM Customers";
				
			
				//Create object for DataAccess class 
			
				ReadData objDac=new ReadData(strSelectCat,strConn);
				tblRec = new DataTable();
				tblRec=objDac.LoadRecordIntoTable();
				//Response.Write("tblRec.Rows.Count"+tblRec.Rows.Count);
				iTotalRec=	tblRec.Rows.Count;
				strPageNo="1";
				//LoadTableWithRec("1");
				btnPrev.Visible=false;
				btnFirstRec.Visible=false;
			}
			
			CalculatePaging();
			LoadTableWithRec(strPageNo);
			
			//Page.RegisterStartupScript("OpenNewWin",strOpenWin);
			
			//------------------------------------------------------------------------
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ddlTotalRec.SelectedIndexChanged += new System.EventHandler(this.ddlTotalRec_SelectedIndexChanged);
			this.btnFirstRec.Click += new System.EventHandler(this.btnFirstRec_Click);
			this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			this.btnLastRec.Click += new System.EventHandler(this.btnLastRec_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region LinkClick(Object sender, EventArgs e)
		public void LinkClick(Object sender, EventArgs e)
		{
		
		}
		#endregion

		#region TblHeader
		public void  TblHeader(string width,string HeaderName)
		{
			TableCell tc;
			tc=new TableCell();
			tc.Text=HeaderName;
			//tc.Width.Value;
			tc.Width = new Unit("5px");

			tc.CssClass="darkgray";
			tr.Cells.Add(tc);
			
		}	
		#endregion

		#region Paging
		public void ShowPaging()
		{
			tblPaging.Rows.Clear();
			
			TableRow tr=new TableRow();
			TableCell tc;
			LinkButton _li;
			if((iLastStep==iCntMovePtr && iFirstStepMod != 0) ||(iCntMovePtr == 1 && iTotalPages < Constant.iShowNoPage))
			{
				for(int iIter=zIter;iIter<=Constant.jIter-(Constant.iShowNoPage-iFirstStepMod);iIter++)
				{
				
					tc=new TableCell();
					_li=new LinkButton();
					_li.ID=Convert.ToString(iIter);
					_li.Text=Convert.ToString(iIter);
					_li.Click+=new EventHandler(LinkToRecord);
					tc.Controls.Add(_li);
					tr.Cells.Add(tc);
				
				}
				
			}
			else
			{
				for(int iIter=zIter;iIter<=Constant.jIter;iIter++)
				{
					tc=new TableCell();
					_li=new LinkButton();
					_li.ID=Convert.ToString(iIter);
					_li.Text=Convert.ToString(iIter);
					_li.Click+=new EventHandler(LinkToRecord);
					tc.Controls.Add(_li);
					tr.Cells.Add(tc);
			
				}
			}
			tblPaging.Rows.Add(tr);
	

		}
		#endregion

		#region On Page Link Button Click
		public void LinkToRecord(object sender,EventArgs e)
		{

			strPageNo=((LinkButton)sender).ID;
			Label1.Text="LinkButton Clicked  "+strPageNo;
			tblPaging.Rows.Clear();
			CalculatePaging();
			LoadTableWithRec(strPageNo);
		}
		#endregion

		#region Next Navigation
		public void btnNext_Click(object sender, System.EventArgs e)
		{
			btnFirstRec.Visible=true;
			btnFirstRec.Visible=true;
			tblPaging.Rows.Clear();
			
			if(iLastStep > iCntMovePtr ||  iCntMovePtr >1)
			{
				btnPrev.Visible=true;
				btnFirstRec.Visible=true;
				iCntMovePtr++;
				zIter=zIter+Constant.iShowNoPage;
				Constant.jIter=Constant.iShowNoPage+Constant.jIter;
				ShowPaging();
		
				Label1.Text="LinkButton Clicked"+zIter;
				//--------------------------------------
				strPageNo=Convert.ToString(zIter);
				//---------------------------------------
				LoadTableWithRec(Convert.ToString(zIter));
				if(iCntMovePtr == iLastStep)
				{
					
					btnNext.Visible=false;
					btnLastRec.Visible=false;
				
				}
			
			}
		}
		#endregion

		#region Previous Navigation Rec
		public void btnPrev_Click(object sender, System.EventArgs e)
		{
		
			tblPaging.Rows.Clear();
			if(iCntMovePtr>1)
			{
				btnNext.Visible=true;
				btnLastRec.Visible=true;
				iCntMovePtr--;
			
				zIter=zIter - Constant.iShowNoPage;
				Constant.jIter=Constant.jIter - Constant.iShowNoPage;
	
				ShowPaging();

				Label1.Text="LinkButton Clicked"+zIter;
				//--------------------------------------
				strPageNo=Convert.ToString(zIter);
				//---------------------------------------
				LoadTableWithRec(Convert.ToString(zIter));
				if(iCntMovePtr==1)
				{
					btnPrev.Visible=false;
					btnFirstRec.Visible=false;
				}
			
			}
	}
		#endregion

		#region DropDown event
		public void ddlTotalRec_SelectedIndexChanged(object sender, System.EventArgs e)
		{	
			btnNext.Visible=true;
			Constant.jIter=Constant.jVirtualInc;
			zIter=zVirtualInc;
			iCntMovePtr=1;
			btnPrev.Visible=false;
			btnFirstRec.Visible=false;
			btnLastRec.Visible=true;
	
			strPageNo="1";
			
			CalculatePaging();
			LoadTableWithRec("1");
		
		}
		#endregion

		#region CalculatePaging Interval
		public void CalculatePaging()
		{
			string strSelectCat=ddlTotalRec.SelectedItem.ToString();//"SELECT CustomerID, CompanyName FROM Customers";
			ReadData objDac=new ReadData(strSelectCat,strConn);
			tblRec = new DataTable();
			tblRec=objDac.LoadRecordIntoTable();
			iTotalRec=	tblRec.Rows.Count;
			if(iTotalRec % Constant.iInitialRecDisplay !=0)
			{
				iTotalPages=iTotalRec/Constant.iInitialRecDisplay +1;
			}
			else
			{
				iTotalPages=iTotalRec/Constant.iInitialRecDisplay;
			}
			iFirstStepMod=iTotalPages%Constant.iShowNoPage;
			if(iFirstStepMod!=0)
			{
				iLastStep=iTotalPages/Constant.iShowNoPage+1;
					
			}
			else
			{
				iLastStep=iTotalPages/Constant.iShowNoPage;
			}
			if(iTotalPages <= Constant.iShowNoPage)
			{
				
				btnNext.Visible=false;
				btnLastRec.Visible=false;
				
			}
			if(iTotalRec!=0)
			{
				ShowPaging();
				Label1.Text="LinkButton Clicked  1";
			}
			else
			{
				Label1.Text="Sorry No Records found .";
				btnLastRec.Visible=false;
				btnFirstRec.Visible=false;
				btnNext.Visible=false;
				btnPrev.Visible=false;
			}
		}

		#endregion

		#region btnFirstRec_Click
		public void btnFirstRec_Click(object sender, EventArgs e)
		{
			btnNext.Visible=true;
			Constant.jIter=Constant.jVirtualInc;
			zIter=zVirtualInc;
			iCntMovePtr=1;
			btnPrev.Visible=false;
			btnFirstRec.Visible=false;
			btnLastRec.Visible=true;
			strPageNo=Convert.ToString(iCntMovePtr);
			LoadTableWithRec(Convert.ToString(iCntMovePtr));
			CalculatePaging();
		}
		#endregion

		#region btnLastRec_Click
		public void btnLastRec_Click(object sender, System.EventArgs e)
		{
			btnFirstRec.Visible=true;
			btnPrev.Visible=true;
			iCntMovePtr=iLastStep;
			btnNext.Visible=true;
			Constant.jIter=Constant.jVirtualInc*iCntMovePtr;
			zIter=Constant.jIter-Constant.jVirtualInc+1;
			
			btnNext.Visible=false;
			btnLastRec.Visible=false;
			strPageNo=Convert.ToString(zIter);
			LoadTableWithRec(Convert.ToString(zIter));
			CalculatePaging();
		
		}
		#endregion

		#region LoadTableWithRec
		public void LoadTableWithRec(string strPageNo)
		{

			tblRecordset.Rows.Clear();
			

			if(strPageNo=="1")
			{
				//((LinkButton)sender).ForeColor=System.Drawing.Color.Blue;
				iCurrentPage=(Convert.ToInt32(strPageNo)-1)*Constant.iInitialRecDisplay+1;
				//Response.Write("step1");
			}
			else
			{
				iCurrentPage=(Convert.ToInt32(strPageNo)-1)*Constant.iInitialRecDisplay+1;
				//Response.Write("step2");
			}
		
			trRec=new TableRow();
			
			TblHeader1("10%","Deal Id");
			TblHeader1("15%","Entity Ref #");
			tblRecordset.Rows.Add(trRec);
			TableCell tcRec;

			for(int iRow=(iCurrentPage-1);iRow<(iCurrentPage+Constant.iInitialRecDisplay-1) && iRow<iTotalRec;iRow++)
			{
				{
					trRec=new TableRow();
					for(int iCol=0;iCol<2;iCol++)
					{
						if(iCol==0)
						{
							LinkButton _liDetails=new LinkButton();
							_liDetails.ID=Convert.ToString("RecordDetail"+iRow);
							tcRec=new TableCell();
							tcRec.BorderWidth=new Unit("1px");
							//tcRec.Text=tblRec.Rows[iRow][iCol].ToString();
							_liDetails.Text=tblRec.Rows[iRow][iCol].ToString();
							tcRec.Controls.Add(_liDetails);
							_liDetails.Click+=new EventHandler(LinkToDetailsRec);
							
							trRec.Cells.Add(tcRec);

						}
						else
						{
							
							tcRec=new TableCell();
							tcRec.BorderWidth=new Unit("1px");
							tcRec.Text=tblRec.Rows[iRow][iCol].ToString();
							trRec.Cells.Add(tcRec);
					
						}
					}
					tblRecordset.Rows.Add(trRec);
				
				}
			 }
		}
		#endregion

		#region Link to Detail Record
		public void LinkToDetailsRec(object sender ,EventArgs e)
		{
				string strTemp=((LinkButton)sender).ID;
				
			string strOpenWin="<script>";
			
		//	strOpenWin+="alert('"+strTemp+"')";
			strOpenWin+="alert('"+strTemp+"');window.open('Basic.aspx','Asset','width=350,height=400,left=270,top=180,scrollbars=yes');";
			
			strOpenWin+="</script>";
			RegisterClientScriptBlock("str",strOpenWin);	
		
			//	((LinkButton)sender).Attributes.Add("onclick","return OpenNewWin();");
	
			
		}
		#endregion

		#region TblHeader1
		public void  TblHeader1(string width,string HeaderName)
		{
			TableCell tc;
			tc=new TableCell();
			
			tc.Text="<b>"+HeaderName+"</b>";
			//tc.Width.Value;
			tc.Width = new Unit(width);
			tc.BorderWidth=new Unit("1px");
			tc.CssClass="lightgray";
			trRec.Cells.Add(tc);
			
		}
		#endregion
	

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions