Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Pagination
|
Introduction
The pagination is the important part in the application. .Net provided tools for the pagination and data display in the grid format. ASP.Net provides three controls for displaying data. These control has own unique traits and associated advantages and disadvantages.Amongst the three Web Controls the data grid Control is the most advanced and supports pagination, sorting and templates. There is certain limitation in customization. The Data List and Data Repeater controls are more customizable but It has less feature then Data grid control. The Data List and Repeater have very flexible templates that give you total control over the formatting of your data, but one major drawback of these controls do not support pagination.
Pagination simply means splitting data display over number of pages in order to facilitate easy to browse interface and minimize the data sent out to the client. For Example: To display the information of the employee listing. It would not be good to display all the employee information on a single page. It will take too much time to display on the browser and user has to find out the particular information.
Sample Code
Code of DataRepeater.aspx <%@ Page language="c#" Codebehind="DataRepeater.aspx.cs" AutoEventWireup="false" Inherits="Application.DataRepeater" SmartNavigation=true%> <%@ Import Namespace=System.Collections %>
<HTML> <HEAD> <title>DataRepeter</title> </HEAD> <body> <form id="frm" runat="server">
// Hidden field to store the current page number <INPUT type="hidden" runat="server" ID="pagenumber" value="0">
<TABLE height="337" cellSpacing="0" cellPadding="0" width="400" border="0"> <TBODY> <TR> <TD colSpan="2" align=center> <asp:repeater id="rpt" runat="server" Visible="true" EnableViewState=false> <HeaderTemplate>
// Header text of the Repeater control
<table border="0" cellspacing="1" cellpadding="2" valign=top> <tr> <td bgcolor=Gray><font size=2 face=Arial color=White> Controller</font></td> <td bgcolor=Gray><font size=2 face=Arial color=White>Action</td> </tr> </HeaderTemplate>
// Bind data to the repeater control
<ItemTemplate> <tr> <td bgcolor=#ffffcc><font size=2 face=Arial ><%# DataBinder.Eval(Container.DataItem, "Controller") %></td>
<td bgcolor=#ffffcc><font size=2 face=Arial><%# DataBinder.Eval(Container.DataItem, "Action") %></td> </tr> </ItemTemplate> <FooterTemplate> </TABLE> </FooterTemplate> </asp:repeater></TD></TR> <tr> <td>
// Link buttons to provide the navigation in the search page eg.(“Back”, “Next”)
<asp:LinkButton id="LBack" runat="server" OnClick="set_page" Font-Name="arial" Font-Size="smaller">Back</asp:LinkButton></td>
<td> <% int shownumberoflink=10; // Number of page int startNumber =0; int endNumber =0 ; %> <%
// ArrayList used for the display the page number
ArrayList ary = new ArrayList(); ary = getPagenumber(); IEnumerator myEnumerator = ary.GetEnumerator();
// Set the value of the page number. If the value of // end page number is less then back button should be visible. // startnumber page number calculated as // (mCurrentPage - ((int)(mCurrentPage % 10)) + 1)
startNumber=mCurrentPage - ((int)(mCurrentPage % 10)) + 1;
if (ary.Count> (startNumber + 9)) endNumber = startNumber + 9 ; else endNumber = ary.Count;
if (endNumber >10) { LBack.Visible=true; }
// Display the Page number. The current page did not have hyperlink.
for(int i=startNumber;i<=endNumber;i++) { if (i == mCurrentPage) { Response.Write("<font size=2 face=Arial >" + i + "</font> " ); } else { Response.Write("<font size=2 face=Arial ><A href=javascript:setPageNumber(" + i + ") >" + i +" </A> </font> "); } }
%>
</td>
// Link buttons to provide the navigation in the search page eg.(“Back”, “Next”)
<td> <asp:LinkButton id="LNext" runat="server" OnClick="set_page" Font-Name="arial" Font-Size="smaller">Next</asp:LinkButton></td> </TD>
</tr> </TD> </TBODY> </TABLE> </form> // Set the current page number, when you click the page number. <script> function setPageNumber(n) { document.frm.action="DataRepeater.aspx?page=" + n; document.forms["frm"].pagenumber.value=n; document.frm.submit(); }
</script> </body> </HTML> -------------------------- --------------------------
<HTML> <HEAD> <title>DataRepeter</title> </HEAD> <body> <form id="frm" runat="server">
// Hidden field to store the current page number <INPUT type="hidden" runat="server" ID="pagenumber" value="0">
<TABLE height="337" cellSpacing="0" cellPadding="0" width="400" border="0"> <TBODY> <TR> <TD colSpan="2" align=center> <asp:repeater id="rpt" runat="server" Visible="true" EnableViewState=false> <HeaderTemplate>
// Header text of the Repeater control
<table border="0" cellspacing="1" cellpadding="2" valign=top> <tr> <td bgcolor=Gray><font size=2 face=Arial color=White> Controller</font></td> <td bgcolor=Gray><font size=2 face=Arial color=White>Action</td> </tr> </HeaderTemplate>
// Bind data to the repeater control
<ItemTemplate> <tr> <td bgcolor=#ffffcc><font size=2 face=Arial ><%# DataBinder.Eval(Container.DataItem, "Controller") %></td>
<td bgcolor=#ffffcc><font size=2 face=Arial><%# DataBinder.Eval(Container.DataItem, "Action") %></td> </tr> </ItemTemplate> <FooterTemplate> </TABLE> </FooterTemplate> </asp:repeater></TD></TR> <tr> <td>
// Link buttons to provide the navigation in the search page eg.(“Back”, “Next”)
<asp:LinkButton id="LBack" runat="server" OnClick="set_page" Font-Name="arial" Font-Size="smaller">Back</asp:LinkButton></td>
<td> <% int shownumberoflink=10; // Number of page int startNumber =0; int endNumber =0 ; %> <%
// ArrayList used for the display the page number
ArrayList ary = new ArrayList(); ary = getPagenumber(); IEnumerator myEnumerator = ary.GetEnumerator();
// Set the value of the page number. If the value of // end page number is less then back button should be visible. // startnumber page number calculated as // (mCurrentPage - ((int)(mCurrentPage % 10)) + 1)
startNumber=mCurrentPage - ((int)(mCurrentPage % 10)) + 1;
if (ary.Count> (startNumber + 9)) endNumber = startNumber + 9 ; else endNumber = ary.Count;
if (endNumber >10) { LBack.Visible=true; }
// Display the Page number. The current page did not have hyperlink.
for(int i=startNumber;i<=endNumber;i++) { if (i == mCurrentPage) { Response.Write("<font size=2 face=Arial >" + i + "</font> " ); } else { Response.Write("<font size=2 face=Arial ><A href=javascript:setPageNumber(" + i + ") >" + i +" </A> </font> "); } }
%>
</td>
// Link buttons to provide the navigation in the search page eg.(“Back”, “Next”)
<td> <asp:LinkButton id="LNext" runat="server" OnClick="set_page" Font-Name="arial" Font-Size="smaller">Next</asp:LinkButton></td> </TD>
</tr> </TD> </TBODY> </TABLE> </form> Code in Page Behind
using System; using System.Collections; using System.ComponentModel; using System.Data.SqlClient; 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.Configuration;
namespace Application { /// /// Summary description for DataRepeter. /// public class DataRepeater : System.Web.UI.Page { string cs; int intPageSize=10; int startPage=0; int rowCount;
public int mCurrentPage; protected System.Web.UI.WebControls.Repeater rpt; protected System.Web.UI.WebControls.TextBox textmode; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.LinkButton LBack; protected System.Web.UI.WebControls.LinkButton LNext; protected System.Web.UI.HtmlControls.HtmlInputHidden pagenumber;
private void Page_Load(object sender, System.EventArgs e) {
// Pick the DSN configuration from the Web.config file.
cs = ConfigurationSettings.AppSettings["dsnstring"];
// Read the current page number from Hidden value. // if the current value is blank set it to the 0. This situation // occurred at first time. try { mCurrentPage=Int32.Parse(pagenumber.Value); } catch { mCurrentPage=0; } // If the current page number is not blank, set the start page.
if (mCurrentPage==0) mCurrentPage=0; startPage = intPageSize * mCurrentPage; getRowCount();
if( !IsPostBack) { BindData(); LBack.Visible =false; } else { if (int.Parse(pagenumber.Value) >= 10) { LBack.Visible=true; } else LBack.Visible=false; } } #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); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load);
} #endregion
// Count the number of records.
public void getRowCount() { BindData(); SqlConnection cn = new SqlConnection(cs); SqlCommand cmd = new SqlCommand (" Select Count(controller) from Application ",cn); cn.Open(); rowCount=(int)cmd.ExecuteScalar(); cn.Close(); } void BindData() { SqlConnection cn = new SqlConnection(cs); SqlCommand cmd = new SqlCommand("Select * from Application order by ApplicationID",cn); SqlDataAdapter adp = new SqlDataAdapter(cmd); DataSet ds=new DataSet(); DataView dv = new DataView(); DataTable dt =new DataTable();
string strSort="Controller";
// pass theses parameters (start page and page size) to the data set.
adp.Fill(ds,startPage,intPageSize,"Result"); dv=ds.Tables["Result"].DefaultView; dv.Sort=strSort; rpt.DataSource=dv; rpt.DataBind(); cn.Close();
}
// this function retrieve the page number.
public ArrayList getPagenumber() { int counter=1; ArrayList ary= new ArrayList(); for(int i=counter; i<=rowCount/intPageSize;i++) { ary.Add(i); } return ary; }
// public void set_page(object sender , System.EventArgs e) { // Set the Page number and the current page number. In case when you // click the back button.
if( ((LinkButton)sender).ID == "LBack" ) { if( ( int.Parse(pagenumber.Value) - 1 )>=0 ) { pagenumber.Value=(int.Parse(pagenumber.Value) - 1).ToString(); mCurrentPage=int.Parse(pagenumber.Value); } } // Set the Page number and the current page number. In case when you // click the Next button.
else if( ((LinkButton)sender).ID == "LNext" ) { if( (int.Parse(pagenumber.Value))< (getPagenumber().Count)) { mCurrentPage = (int.Parse(pagenumber.Value) + 1 ) ; pagenumber.Value=(mCurrentPage).ToString(); } }
startPage = mCurrentPage * intPageSize;
getPagenumber();
BindData(); } public void write(string functionname , string strvalue) { Response.Write ("" + functionname + " ::" + strvalue + ""); } } Summary This article explains one of many possible solutions to pagination. When displaying the data in ASP.Net Web Page, many developers choose the data web control they are most familiar with, typically the Data Grid. But the blind decisions are not wise. This article will also work with Data List.
|
Responses
|
| Author: arun 19 Aug 2009 | Member Level: Gold Points : 1 | This article explains pagination when we use datagrid or other controls. But most of the time we may have to give pagination in a footer section with PREVIOUS and NEXT button. this has to be done through C# coding. In this case we need to put a for loop and html page display within the loop
|
|