Paging in Datalist or Repeater Control In ASP.NET

No.of Views1143
Bookmarked0 times
Downloads 
Votes1
By  jalpesh   On  26 Jun 2010 03:06:11
Tag : ASP.NET , List Controls
Paging in Datalist or Repeater Control In ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippet,  i have given code for do the custom pageing to DataList or Repeater control.

HTML of Datalist

<asp:datalist id="dsList" runat="server" width=100%><ItemTemplate><%#DataBinder.Eval(Container.DataItem, "ID").ToString()%><%#DataBinder.Eval(Container.DataItem, "Name").ToString()%></ItemTemplate></asp:datalist><table width="100%" border="0" align="Center"><tr><td><asp:LinkButton id="lnkPrevious" runat="server"></asp:LinkButton></td><td><asp:LinkButton id="lnkNext" runat="server">></asp:LinkButton></td></tr></table>

C#

int Start ;
SqlConnection dbCon ;
SqlDataAdapter Adpt ;
DataSet dtUser ;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack )
{
ViewState["Start"] = 0;
BindData();
}
}
void BindData()
{
dbCon = new SqlConnection("server=localhost;uid=sa;pwd=;database=user");
Adpt = new SqlDataAdapter("Select * from user", dbCon);
dtUser = new DataSet();
Start = (int)ViewState["Start"];
ViewState["Size"] = 14;
Adpt.Fill(dtUser, Start,(int) ViewState["Size"], "user");
dsList.DataSource = dtUser;
dsList.DataBind();
}

private void lnkPrevious_Click(object sender, System.EventArgs e)
{
Start = (int) ViewState["Start"] -(int) ViewState["Size"];
ViewState["Start"] = Start;
if (Start <= 0 ) { ViewState["Start"] = 0; } BindData(); }private void lnkNext_Click(object sender, System.EventArgs e) 
{ 
int count = dsList.Items.Count; 
Start = (int) ViewState["Start"]+(int) ViewState["Size"]; 
ViewState["Start"] = Start;
 if ( count < (int)ViewState["Size"] )
 { 
ViewState["Start"] = (int)ViewState["Start"] - (int)ViewState["Size"];
 }
 BindData();
 }

VB.NET

Private Start As IntegerPrivate dbCon As SqlConnection
Private Adpt As SqlDataAdapter
Private dtUser As DataSet
Private Sub Page_Load(sender As Object, e As System.EventArgs)If Not Page.IsPostBack ThenViewState("Start") = 0BindData()End IfEnd SubPrivate Sub BindData()
    dbCon = New SqlConnection("server=localhost;uid=sa;pwd=;database=user")
    Adpt = New SqlDataAdapter("Select * from user", dbCon)
    dtUser = New DataSet()
    Start = CInt(ViewState("Start"))
    ViewState("Size") = 14Adpt.Fill(dtUser, Start, CInt(ViewState("Size")), "user")
    dsList.DataSource = dtUser
    dsList.DataBind()
End SubPrivate Sub lnkPrevious_Click(sender As Object, e As System.EventArgs)
    Start = CInt(ViewState("Start")) - CInt(ViewState("Size"))
    ViewState("Start") = StartIf Start <= 0 ThenViewState("Start") = 0End IfBindData()
End SubPrivate Sub lnkNext_Click(sender As Object, e As System.EventArgs)Dim count As Integer = dsList.Items.Count
    Start = CInt(ViewState("Start")) + CInt(ViewState("Size"))
    ViewState("Start") = StartIf count < CInt(ViewState("Size")) ThenViewState("Start") = CInt(ViewState("Start")) - CInt(ViewState("Size"))End IfBindData()
End Sub

Thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation- Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top