.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Pagination in Repeater, DataList control in asp.net

Posted By:Pankaj Mishra       Posted Date: July 30, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Pagination in Repeater, DataList control in asp.net.

As we all know that Paging is not supported in Repeater, DataList Control in asp.net directly but using PagedDataSource is it possible to use Pagination in Repeater and DataList control.

what is PagedDataSource?

PagedDataSource is a class available in all version of .NET Framework 1.1, 2.0,3.0,3.5 and 4.0 which Encapsulates the paging-related properties of a data-bound control (such as DataGrid, GridView, DetailsView, and FormView) that allow it to perform paging. This class cannot be inherited. Reference

Let's use PagedDataSource to build Pagination in Repeater, DataList control

Step 1: First Drag a Repeater Control in your asp.net Web Page and assign some field to it. In below example i have created a table with 3 column in html table inside repeater control. Also Drag tow asp.net link button control to your web page and Name them lnkPrevious and lnkNext for Previous and Next button respectively.

And the html code will look like this after adding the HTML table to Repeater control.



ID First Name Last Name
<%#Eval("ID")%> <%#Eval("FName")%> <%#Eval("LName")%>
<     Next Page>>

Step 2 : Add DataSource (DataTable, ObjectDataSource, Data from DataBase) to Paged PagedDataSource and assign some important property of PagedDatSource.

 

//Create an object of PagedDataSource Object
PagedDataSource objPagedDataSource = new PagedDataSource();
//Get the Data Source and assign to Data View
DataView objDataView = new DataView(GetDataTable());
true;
//Set the Page Size, Means the number os records you want to dispay in a page
objPagedDataSource.CurrentPageIndex = GetCurrentPageNumber;

Now Assign the PagesDataSource object to Repeter control if you want you can change your control to datalist

if (objPagedDataSource.PageCount > 0)
{

            Repeater1.Visible = true;

            Repeater1.DataSource = objPagedDataSource;

            Repeater1.DataBind();
 }

If you want you can Enable Disable LinkButton used for Paging you can use Below code.

 

//Enable/Disable Previous Link Button 
if (GetCurrentPageNumber == 0) 
{ 
lnkPrevious.Visible = false; 
} 
else 
{ 
lnkPrevious.Visible = true; 
} 
//Enable/Disable Next link Button 
if (objPagedDataSource.PageCount == GetCurrentPageNumber+1) 
{ 
lnkNext.Visible = false; 
} 
else 
{ 
lnkNext.Visible = true; 
}

Step 3: In your Next and Previous link button you can add the following code.

 

// For Previous link Button Pagination
protected void lnkPrevious_Click(object sender, EventArgs e)
{
GetCurrentPageNumber -= 1;
BindDataToRepeter();
}
// For Next link Button Pagination
protected void lnkNext_Click(object sender, EventArgs e)
{
GetCurrentPageNumber += 1;
BindDataToRepeter();
}

Step 4: Create one property and Name that GetCurrentPageNumber which will be used to store current Page number in ViewState and the code for that is

 

/// 
/// Property to get and set the current page number and Page Number is stored in ViewState
/// 
private int m_GetCurrentPageNumber = 0;
private int GetCurrentPageNumber
get
{
if (ViewState["CurrentPage"] != null)
return Convert.ToInt32(ViewState["CurrentPage"]);
else
{
return m_GetCurrentPageNumber;
set
{
ViewState["CurrentPage"] = value; 
}
}
}  


NOTE: For the above example i have used DataTable GetDataTable() which will return DataTable and the code for creating datatable is as follows.

 

/// 
/// Create data table to bind to Repeter / DataList Control 
/// 
/// 
private DataTable GetDataTable()
{
// Create an object of DataTable class
DataTable objDataTable = new DataTable("MyDataTable");
//Create ID DataColumn
DataColumn dataColumn_ID = new DataColumn("ID", typeof(Int32));
objDataTable.Columns.Add(dataColumn_ID);
//Create another DataColumn Name
DataColumn dataColumn_FName = new DataColumn("FName", typeof(string));
objDataTable.Columns.Add(dataColumn_FName);
//Create another DataColumn Name
DataColumn dataColumn_LName = new DataColumn("LName", typeof(string));
objDataTable.Columns.Add(dataColumn_LName);
//Now Add some row to newly created Data Table
DataRow objDataRow;
for (int i = 0; i < 50; i++)
{
objDataRow = objDataTable.NewRow();// Important you have create New row
objDataRow["ID"] = i;
objDataRow["FName"] = "First Name_" + i.ToString();
objDataRow["LName"] = "Last Name_" + i.ToString();
objDataTable.Rows.Add(objDataRow);
}
objDataTable.AcceptChanges();
return objDataTable;
}


you can see the Pagination link this if you run sample code available for download from bottom of the page


In the similar above faishol you can assign the Pagination in DataList control also sample code is available for donload which have both example for Repeter and DataList Control.

Hope this will help all

Cheers
Pankaj!!!


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend