<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="lblContactName" Text='<%# Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<table>
<tr>
<td>
<asp:PlaceHolder ID="plcPaging" runat="server" />
<br /><asp:Label runat="server" ID="lblPageName" />
</td>
</tr>
</table>
C# Coding
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FetchData(10, 0);
}
else
{
plcPaging.Controls.Clear();
CreatePagingControl();
}
}
private void FetchData(int take, int pageSize)
{
using (NorthwindDataContext dc = new NorthwindDataContext())
{
var query = from p in dc.Customers
.OrderBy(o => o.ContactName)
.Take(take)
.Skip(pageSize)
select new
{
ID = p.CustomerID,
Name = p.ContactName,
Count = dc.Customers.Count()
};
PagedDataSource page = new PagedDataSource();
page.AllowCustomPaging = true;
page.AllowPaging = true;
page.DataSource = query;
page.PageSize = 10;
Repeater1.DataSource = page;
Repeater1.DataBind();
if (!IsPostBack)
{
RowCount = query.First().Count;
CreatePagingControl();
}
}
}
private void CreatePagingControl()
{
for (int i = 0; i < (RowCount / 10) + 1; i++)
{
LinkButton lnk = new LinkButton();
lnk.Click += new EventHandler(lbl_Click);
lnk.ID = "lnkPage" + (i + 1).ToString();
lnk.Text = (i + 1).ToString();
plcPaging.Controls.Add(lnk);
Label spacer = new Label();
spacer.Text = " ";
plcPaging.Controls.Add(spacer);
}
}
void lbl_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
int currentPage = int.Parse(lnk.Text);
int take = currentPage * 10;
int skip = currentPage == 1 ? 0 : take - 10;
FetchData(take, skip);
}
VB Coding
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If (Not IsPostBack) Then
FetchData(10, 0)
Else
plcPaging.Controls.Clear()
CreatePagingControl()
End If
End Sub
Private Sub FetchData(ByVal take As Integer, ByVal pageSize As Integer)
Using dc As New NorthwindDataContext()
Dim query = From p In dc.Customers.OrderBy(Function(o) o.ContactName).Take(take).Skip(pageSize) _
Select New
p.ContactName, Count = dc.Customers.Count()
p.CustomerID, Name = p.ContactName, Count
ID = p.CustomerID, Name
Dim page As New PagedDataSource()
page.AllowCustomPaging = True
page.AllowPaging = True
page.DataSource = query
page.PageSize = 10
Repeater1.DataSource = page
Repeater1.DataBind()
If (Not IsPostBack) Then
RowCount = query.First().Count
CreatePagingControl()
End If
End Using
End Sub
Private Sub CreatePagingControl()
For i As Integer = 0 To (RowCount / 10)
Dim lnk As New LinkButton()
AddHandler lnk.Click, AddressOf lbl_Click
lnk.ID = "lnkPage" & (i + 1).ToString()
lnk.Text = (i + 1).ToString()
plcPaging.Controls.Add(lnk)
Dim spacer As New Label()
spacer.Text = " "
plcPaging.Controls.Add(spacer)
Next i
End Sub
Private Sub lbl_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim lnk As LinkButton = TryCast(sender, LinkButton)
Dim currentPage As Integer = Integer.Parse(lnk.Text)
Dim take As Integer = currentPage * 10
Dim skip As Integer = If(currentPage = 1, 0, take - 10)
FetchData(take, skip)
End Sub
Support link
http://www.codeguru.com/csharp/.net/net_asp/controls/article.php/c12925/ASPNET-Tip-Creating-Paging-for-a-Repeater-Control.htm
http://www.dotnetcurry.com/ShowArticle.aspx?ID=345
http://www.codeguru.com/csharp/.net/net_asp/controls/article.php/c12925/ASPNET-Tip-Creating-Paging-for-a-Repeater-Control.htm
http://www.dotnetcurry.com/ShowArticle.aspx?ID=345
No comments:
Post a Comment