Click here to Skip to main content
15,891,513 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.
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;

namespace Paging
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm2 : 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 iShowNoPage=10;//-Change Parameter
		private static int jIter=10;//------Change Parameter
		private const int jVirtualInc=10;
		private const int zVirtualInc=1;
		private const int iInitialRecDisplay=5;
		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;
		#endregion
		
		#region On Page Load
		public void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			//	if(!IsPostBack)
			//Button1.Attributes.Add("onclick","return alert(addDays(new Date(),10));");
			//LoadDynamicTable();
			//LoadStyleTable();
			// Button1.Attributes.Add("onclick","return alert(addDays(new Date('5/23/2006'),10));");	
			//------------------------------------------------------------------------------------	
			if(iCntMovePtr==1)
			{
				btnPrev.Visible=false;
				btnFirstRec.Visible=false;
			}
			CalculatePaging();
			
			//------------------------------------------------------------------------
		}
		#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.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 LoadDynamicTable()
		public void LoadDynamicTable()
		{
			TextBox _txt;
			CheckBox _chk;
			LinkButton _lnkBtn;
			tr=new TableRow();
			TableHeader("Id");
			TableHeader("Name");
			TableHeader("Links");
			TableHeader("SelectOption");
			
			for(int i=0;i<5;i++)
			{
				tr=new TableRow();
				for(int j=0;j<4;j++)
				{
					TableCell tc=new TableCell();
					tc.BorderWidth=1;
					switch(j)
					{
						case 0:
							_txt=new TextBox();
							_txt.Text=j.ToString();
							tc.Controls.Add(_txt);
							break;
						case 1:
							_txt=new TextBox();
							_txt.Text=j.ToString();
							tc.Controls.Add(_txt);
							break;
						case 2:
							_lnkBtn=new LinkButton();
							_lnkBtn.Text="Link"+j;
							tc.Controls.Add(_lnkBtn);
							_lnkBtn.Click += new EventHandler(LinkClick);
							break;
						case 3:
							_chk=new CheckBox();
							_chk.Text="Select";
							tc.Controls.Add(_chk);
							break;
							
					}
					
					tr.Cells.Add(tc);
			
				}
				tbPickList.Rows.Add(tr);
			}
		}
		#endregion

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

		#region TableHeader
		public void TableHeader(string strHeaderName)
		{
		
			TableHeaderCell tc=new TableHeaderCell();
			
			Literal li=new Literal();
			li.Text=strHeaderName;
			tc.Controls.Add(li);
			tc.BorderWidth=1;
			tr.Cells.Add(tc);
			tbPickList.Rows.Add(tr);
		
		}
		#endregion

		#region LoadStyleTable()
		public void LoadStyleTable()
		{
			string[] tmp = new string[10];
			string StrDump="San~SD~TYY~JKL~LJLJ~LJLJ~LLJ~LKKL~FG~RE";
			tr=new TableRow();
			TblHeader("30","Maturity Date");
			
			TblHeader("31","Maturity Date");
			
			TblHeader("31","Maturity Date");
			
			TblHeader("31","Maturity Date");
		
			TblHeader("31","Maturity Date");
			
			TblHeader("11","Maturity Date");
			
			TblHeader("11","Maturity Date");
			
			TblHeader("11","Maturity Date");
		
			TblHeader("11","Maturity Date");
			
			TblHeader("11","Maturity Date");
			Table1.Rows.Add(tr);
			

			if (StrDump!= "") 
			{
				tr = new TableRow();
				Label _lbl;
				TableCell tc;
				tmp = StrDump.Split("~".ToCharArray());
				Response.Write(tmp.Length);
				for(int i = 0; i<10;i++)
				{
					tc = new TableCell();
					_lbl=new Label();
					_lbl.Text=tmp[i];
					tc.Controls.Add(_lbl);
					tr.Cells.Add(tc);
			
				}
				Table1.Rows.Add(tr);
			}
		
		}
		#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 < iShowNoPage))
			{
				for(int iIter=zIter;iIter<=jIter-(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<=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)
		{

			string strPageNo=((LinkButton)sender).ID;
			//Response.Write("LinkButton Clicked  "+strPageNo);
			Label1.Text="LinkButton Clicked  "+strPageNo;
			tblPaging.Rows.Clear();
			ShowPaging();
		}
		#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+iShowNoPage;
				jIter=iShowNoPage+jIter;
				ShowPaging();
		
				Label1.Text="LinkButton Clicked"+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 - iShowNoPage;
				jIter=jIter - iShowNoPage;
	
				ShowPaging();

				Label1.Text="LinkButton Clicked"+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;
			jIter=jVirtualInc;
			zIter=zVirtualInc;
			iCntMovePtr=1;
			btnPrev.Visible=false;
			btnFirstRec.Visible=false;
			btnLastRec.Visible=true;
			CalculatePaging();

		
		}
		#endregion

		#region CalculatePaging Interval
		public void CalculatePaging()
		{
			

			iTotalRec=Convert.ToInt16(ddlTotalRec.SelectedValue);
			if(iTotalRec % iInitialRecDisplay !=0)
			{
				iTotalPages=iTotalRec/iInitialRecDisplay +1;
			}
			else
			{
				iTotalPages=iTotalRec/iInitialRecDisplay;
			}
			iFirstStepMod=iTotalPages%iShowNoPage;
			//	Response.Write("iFirstStepMod"+iFirstStepMod);
			if(iFirstStepMod!=0)
			{
				iLastStep=iTotalPages/iShowNoPage+1;
					
			}
			else
			{
				iLastStep=iTotalPages/iShowNoPage;
			}
			if(iTotalPages <= 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

		public void btnFirstRec_Click(object sender, System.EventArgs e)
		{
			btnNext.Visible=true;
			jIter=jVirtualInc;
			zIter=zVirtualInc;
			iCntMovePtr=1;
			btnPrev.Visible=false;
			btnFirstRec.Visible=false;
			btnLastRec.Visible=true;
			CalculatePaging();

		
		}

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

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