Here we create our .aspx page with Data List and Next,
Previous button control:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Paging.aspx.cs"
Inherits="Paging"
%>
DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DataList
Custom Pagingtitle>
head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptM" runat="server">asp:ScriptManager>
<div>
<asp:UpdatePanel ID="updtdlEmployee" runat="server">
<ContentTemplate> <asp:DataList ID="dlEmployee" runat="server">
<HeaderTemplate>
<table cellpadding="5" cellspacing="5" width="50%" align="center">
HeaderTemplate>
<ItemTemplate>
<tr>
<td align="left"><img
src="roxocon.jpg"/
alt='<%#Eval("empName")%>' title='<%#Eval("empName")%>'>td>
<td align="left">
<asp:Label runat="server" ID="lblContactName" Text='<%#
Eval("empName") %>' />
td>
tr>
ItemTemplate>
<FooterTemplate>
table>
FooterTemplate>
asp:DataList>
<table width="100%" border="0" style="text-align:center;">
<tr>
<td> <asp:label id="lblCurrentPage" runat="server">asp:label>td>
tr>
<tr>
<td>
<asp:ImageButton ID="cmdPrev" runat="server" ImageUrl="~/prev.png" Width="25" Height="25" onclick="cmdPrev_Click1" AlternateText="Previous"/>
<asp:ImageButton ID="cmdNext" runat="server" ImageUrl="~/next.png" Width="25" Height="25" onclick="cmdNext_Click1" AlternateText="Next"/>
td>
tr>
table>
ContentTemplate>
asp:UpdatePanel>
div>
form>
body>
html>
.aspx.cs page
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public partial class Paging : System.Web.UI.Page
{
clsDAL
objDAL = new clsDAL(); //Data Access
Layer
PagedDataSource objPds = new
PagedDataSource();//page
data source
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindDataList();
}
}
public int CurrentPage
{
get
{
int
_currentPage = Convert.ToInt32(ViewState["_CurrentPage"]);
if
(_currentPage == 0)
return
0; //set default page index
else
return
(int)_currentPage;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
public void BindDataList()
{
DataTable
dt = objDAL.getDataTable("SELECT * FROM
EmployeeMaster ORDER BY empName DESC");
objPds.DataSource = dt.DefaultView;
objPds.AllowPaging = true;
// Set page
size
objPds.PageSize = 5;
objPds.CurrentPageIndex = CurrentPage;
lblCurrentPage.Text = "You are viewing Page: " + (CurrentPage
+ 1).ToString() + " of " + objPds.PageCount.ToString();
// Disable
Prev & Next buttons
cmdPrev.Enabled = !objPds.IsFirstPage;
cmdNext.Enabled = !objPds.IsLastPage;
dlEmployee.DataSource = objPds;
dlEmployee.DataBind();
}
protected void cmdNext_Click1(object
sender, EventArgs e)
{
CurrentPage += 1;
BindDataList();
}
protected void cmdPrev_Click1(object
sender, EventArgs e)
{
CurrentPage -= 1;
BindDataList();
}
}
Comments