SET PAGING IN DATALIST IN ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList id="theDataList" runat="server">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<tr>
<td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></font></td>
</tr>
<tr>
<td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "ContactName") %></td>
</tr>
<tr>
<td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></font></td>
</tr>
</table>
</ItemTemplate>
<separatortemplate>
<hr color="#0099FF" />
</separatortemplate>
</asp:DataList>
<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click">First</asp:LinkButton>
<asp:LinkButton id="btnPrev" Text="Prev" OnClick="btnPrev_Click" runat="server" />
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton id="btnNext" Text="Next" OnClick="btnNext_Click" runat="server" />
<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click">Last</asp:LinkButton></div>
</form>
</body>
</html>
and in code behind:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
PagedDataSource pagedData = new PagedDataSource();
int CurPage=1;
void Page_Load(Object obj, EventArgs e)
{
doPaging();
}
public DataTable getTheData()
{
DataSet DS = new DataSet();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlDataAdapter objSQLAdapter = new SqlDataAdapter("SELECT CompanyName, ContactName, ContactTitle FROM Customers", myConnection); objSQLAdapter.Fill(DS, "Customers");
return DS.Tables[0];
}
void doPaging()
{
pagedData.DataSource = getTheData().DefaultView;
pagedData.AllowPaging = true;
pagedData.PageSize = 4;
try
{
if (Request["Page"].ToString() != null)
{
CurPage = Int32.Parse(Request["Page"].ToString());
}
else
{
CurPage = 1;
}
pagedData.CurrentPageIndex = CurPage - 1;
}
catch
(Exception ex)
{
pagedData.CurrentPageIndex = 0;
}
btnPrev.Enabled = (!pagedData.IsFirstPage);
//btnPrev.Visible = (!pagedData.IsFirstPage);
btnFirst.Enabled = (!pagedData.IsFirstPage);
btnNext.Enabled = (!pagedData.IsLastPage);
//btnNext.Visible = (!pagedData.IsLastPage);
btnLast.Enabled = (!pagedData.IsLastPage);
//pagedData.CurrentPageIndex = CurPage - 1;
lblCurrentPage.Text = "Page: " + CurPage.ToString() + " of " + pagedData.PageCount.ToString(); ;
theDataList.DataSource = pagedData;
theDataList.DataBind();
}
protected void btnPrev_Click(object sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage - 1));
}
protected void btnNext_Click(object sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage+ 1));
}
protected void btnFirst_Click(object sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (1));
}
protected void btnLast_Click(object sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (pagedData.PageCount));
}
}