Hi...
I know how to apply paging in gridview in asp.net but I want to know how to apply paging in datalist ? Please Explain with an example ?
Thanks...........
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jan 18, 2012, 7:41 PM
paging in datalist
Ex:-
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Default.aspx.vb code
using System;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str ;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddatalist();
}
}
private void binddatalist()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
PagedDataSource Pds1 = new PagedDataSource();
Pds1.DataSource = ds.Tables[0].DefaultView;
Pds1.AllowPaging = true;
Pds1.PageSize = 3;
Pds1.CurrentPageIndex = CurrentPage;
lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
btnPrevious.Enabled = !Pds1.IsFirstPage;
btnNext.Enabled = !Pds1.IsLastPage;
dl1.DataSource = Pds1;
dl1.DataBind();
con.Close();
}
public int CurrentPage
{
get
{
object s1 = this.ViewState["CurrentPage"];
if (s1 == null)
{
return 0;
}
else
{
return Convert.ToInt32(s1);
}
}
set { this.ViewState["CurrentPage"] = value; }
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
binddatalist();
}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
binddatalist();
}
}
Thanks
Vineet Kumar SainiPosted Jan 21, 2012, 6:38 AM
Datta KharadPosted Jan 19, 2012, 1:51 AM
You know the datalist doesn't have in-built support for paging, so we want do with custom paging. We can do this two ways:-
1) Using PagedDatasource class:-
2) Using Fill() method of DataAdapter object:-
1) Using PagedDatasource class:-
This class comes from the System.Web.UI.WebControls Namespace, After that you should know properties of pagedDatasource class:-
a) PageSize:– This property takes an integer (by default it's set at 10), and defines how many records are displayed on a page.
b) AllowPaging:– This property determines whether the paging should be turned on or off (Boolean "true" or "false"). By default it's set to "false".
c) CurrentPageIndex:– This returns/sets the current page number. By default it returns 0.
d) PageCount:– Returns a count of the total number of pages that are available.
e) DataSource:– The value of this property will be the source of the data you want to page.
f) IsFirstPage:– Returns "true" or "false", depending on whether the current page is the first page in the series.
g) IsLastPage:– As above, but for the last page.
Example of Paging in Datalist using PagedDatasource class:-
See example in above post which has given by Satyapriya.
2) Using Fill() method of DataAdapter object:-
The DataAdapter's Fill method which can be overloaded in several different ways. One way to overload it is to pass the arguments DataSet, starting record, number of records, and the table name or index. Number of records is analogous to "page size" in a DataGrid. Starting record is the "index" of the current starting record. For example, if our page size is 10 and we are on the first page of records, then starting record would be 0 and our 10 displayed rows would be 0 through 9. If the NextPage button is clicked, then starting record would be 10 and displayed rows would be 10 through 19, etc. All we need then is a mechanism for specifying page size (number of records) and current index (starting record). Page size is normally a static value that can be specified once in the program. Starting record (current index) will change from page to page and must be tracked within our code-behind logic.
dataAdapter.Fill (dataSet, Convert.ToInt32(lblCurrentIndex.Text), Convert.ToInt32 (lblPageSize.Text), "Customers")
In code file use Fill method of dataadapter :-
Example:-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Configuration;
public class PagingDataList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblRecordCount;
protected System.Web.UI.WebControls.DataList dtlCustomers;
protected System.Web.UI.WebControls.Label lblPageSize;
protected System.Web.UI.WebControls.Label lblCounts;
private void Page_Load(System.Object sender, System.EventArgs e)
{
if (!Page.IsPostBack()) {
BindTheData();
}
}
private void BindTheData()
{
SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["NorthwindConnection"]);
string strSql = "SELECT CompanyName, ContactName, ContactTitle, Country, Phone FROM Customers";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSql, objConn);
DataSet dataSet = new DataSet();
if (!Page.IsPostBack()) {
dataAdapter.Fill(dataSet);
lblRecordCount.Text = Convert.ToString(dataSet.Tables[0].Rows.Count);
dataSet = null;
dataSet = new DataSet();
}
dataAdapter.Fill(dataSet, Convert.ToInt32(lblCurrentIndex.Text), Convert.ToInt32(lblPageSize.Text), "Customers");
dtlCustomers.DataSource = dataSet.Tables["Customers"].DefaultView;
dtlCustomers.DataBind();
objConn.Close();
ShowCounts();
}
public void ShowFirstPage(System.Object s, System.EventArgs e)
{
lblCurrentIndex.Text = "0";
BindTheData();
}
public void ShowPreviousPage(System.Object s, System.EventArgs e)
{
lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) - Convert.ToInt32(lblPageSize.Text));
if (Convert.ToInt32(lblCurrentIndex.Text) < 0) {
lblCurrentIndex.Text = "0";
}
BindTheData();
}
public void ShowNextPage(System.Object s, System.EventArgs e)
{
if (Convert.ToInt32(Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPageSize.Text)) < Convert.ToInt32(lblRecordCount.Text)) {
lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPageSize.Text));
}
BindTheData();
}
public void ShowLastPage(System.Object s, System.EventArgs e)
{
int intMod = 0;
intMod = Convert.ToInt32(lblRecordCount.Text) % Convert.ToInt32(lblPageSize.Text);
if (intMod > 0) {
lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblRecordCount.Text) - intMod);
} else {
lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblRecordCount.Text) - Convert.ToInt32(lblPageSize.Text));
}
BindTheData();
}
private void ShowCounts()
{
lblCounts.Text = "|Total Rows: " + lblRecordCount.Text;
lblCounts.Text += " | Page: ";
lblCounts.Text += Convert.ToString(Convert.ToInt32(Convert.ToInt32(lblCurrentIndex.Text) / Convert.ToInt32(lblPageSize.Text) + 1));
lblCounts.Text += " of ";
if ((Convert.ToInt32(lblRecordCount.Text) % Convert.ToInt32(lblPageSize.Text)) > 0) {
lblCounts.Text += Convert.ToString(Conversion.Fix(Convert.ToInt32(lblRecordCount.Text) / Convert.ToInt32(lblPageSize.Text) + 1));
} else {
lblCounts.Text += Convert.ToString(Conversion.Fix(lblRecordCount.Text) / Convert.ToInt32(lblPageSize.Text));
}
lblCounts.Text += " |";
}
}