Code on .aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterPaging1.aspx.cs" Inherits="RepeaterPaging" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<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>
</td>
</tr>
<tr>
<td>
<asp:PlaceHolder ID="plcPaging" runat="server" />
<br />
<asp:Label runat="server" ID="lblPageName" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
code on .cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class RepeaterPaging : System.Web.UI.Page
{
int GpageSize = 3; //global page Size;
VijayDAL vd = new VijayDAL();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getData(); // Get Data From DataBase or DataTable
FetchData(GpageSize, 1); //Filter Record From DataTable
}
else
{
plcPaging.Controls.Clear();
DataTable dt = (DataTable)ViewState["MyTable"];
CreatePagingControl(dt.Rows.Count);
}
}
DataTable table = new DataTable();
private void getData()
{
string[] names = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLL", "MMMM", "NNNNN" };
table.Columns.Add("Name", typeof(System.String));
table.Columns.Add("ID", typeof(System.Int32));
for (int i = 0; i < names.Length; i++)
{
DataRow row = table.NewRow();
row[0] = names[i];
row[1] = i + 1;
table.Rows.Add(row);
}
ViewState.Add("MyTable", table);
}
private void FetchData(int take, int pageSize)
{
System.Web.UI.WebControls.PagedDataSource page = new PagedDataSource(); //This is a Main Import
//This is a scenario where you can use the PagedDataSource class.
//This class encapsulates the paging related properties of a data-bound control.
//This article will not only show you how to create custom paging,
page.AllowCustomPaging = true;
page.AllowPaging = true;
DataTable dt = (DataTable)ViewState["MyTable"];
DataView dv = new DataView();
dv = dt.DefaultView;
dv.RowFilter = "ID>=" + pageSize + " AND " + "ID<=" + take;
page.DataSource = dv;
page.PageSize = GpageSize;//golbal Variable
Repeater1.DataSource = page;
Repeater1.DataBind();
if (!IsPostBack)
{
int RowCount = dt.Rows.Count;
CreatePagingControl(RowCount);
}
}
private void CreatePagingControl(int RowCount)
{
for (int i = 0; i < (RowCount / GpageSize) + 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 * GpageSize;
int skip = currentPage == 1 ? 0 : take - GpageSize;
FetchData(take, skip);
}
}
No comments:
Post a Comment