Repeater Paging 03
이번 방법은 기존과는 다른 방법으로 페이징을 했습니다. 먼저 모든 레코드수를 읽어와서 총 페이지를 나눈다음 페이지 사이즈에 의하여 페이징을 하였습니다. 이 페이징 방법은 꽤 쓸만 한 방법이라고 저는 개인적으로 생각을 합니다.
==============================================================================================
Used DB is "Northwind". Used table is "Products". Add a connectionString in web.config.
Create a folder named "pager". Add a webform named "Pager_Repeate02" under the folder.
Result:
============================================================================================
Pager_Repeater02.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pager_Repeater02.aspx.cs" Inherits="Pager_Pager_Repeater02" %>
<!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>Repeater Paging Test02</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Repeater ID="myRepeater" runat="server">
<HeaderTemplate>
<table cellpadding="0" border="0" width="300px">
<tr style="width:100%">
<td align="center">
<div style="background-color:#999999; color:White"><strong>ProductName</strong></div>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="font-size:9pt">
<li><%# DataBinder.Eval(Container.DataItem,"ProductName") %></li>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
-------------------------------------------------<br />
<asp:LinkButton ID="pageFirst" runat="server" CommandArgument="First" Text="First" Font-Size="9pt"></asp:LinkButton>
<asp:LinkButton ID="pagePrev" runat="server" CommandArgument="Prev" Text="Prev" Font-Size="9pt"></asp:LinkButton>
<asp:LinkButton ID="pageNext" runat="server" CommandArgument="Next" Text="Next" Font-Size="9pt"></asp:LinkButton>
<asp:LinkButton ID="pageLast" runat="server" CommandArgument="Last" Text="Last" Font-Size="9pt"></asp:LinkButton>
<asp:DropDownList ID="ddlPage" runat="server" AutoPostBack="true"
AppendDataBoundItems="true"
onselectedindexchanged="ddlPage_SelectedIndexChanged">
<asp:ListItem Text="Select Page"></asp:ListItem>
</asp:DropDownList>
<br />
<table>
<tr>
<td style="color:Blue; font-size:9pt">Current page:</td>
<td><asp:Label ID="Label1" runat="server" Font-Size="9pt"></asp:Label> | </td>
<td style="color:Blue; font-size:9pt">Total Pages:</td>
<td><asp:Label ID="Label2" runat="server" Font-Size="9pt"></asp:Label></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
============================================================================================
Pager_Repeater02.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
public partial class Pager_Pager_Repeater02 : System.Web.UI.Page
{
private int pageSize = 10;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid(1);
//ddlPage DataBind
for (int i = 1; i <= Convert.ToInt32(Label2.Text); i++)
{
ddlPage.Items.Add(Convert.ToString(i));
}
}
}
private void BindGrid(int rsCount)
{
int totalPage;
int totalRecord;
string cmdString = "select count(*) from Products";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand(cmdString, con);
con.Open();
totalRecord = (int)cmd.ExecuteScalar();
con.Close();
if ((totalRecord % pageSize) > 0)
{
totalPage = totalRecord / pageSize + 1;
}
else
{
totalPage = totalRecord / pageSize;
}
cmdString = "select productName from Products";
SqlDataAdapter sda = new SqlDataAdapter(cmdString,con);
DataSet ds = new DataSet();
sda.Fill(ds,(rsCount - 1)*pageSize,pageSize,"ProductName");
myRepeater.DataSource = ds;
myRepeater.DataBind();
DataTable t;
t = ds.Tables["ProductName"];
Label1.Text = rsCount.ToString();
Label2.Text = totalPage.ToString();
ds.Clear();
con.Close();
//pagePrev 와 pageNext 활성을 제어
if (Label1.Text == "1")
this.pagePrev.Enabled = false;
else
this.pagePrev.Enabled = true;
if (Label1.Text == Label2.Text)
pageNext.Enabled = false;
else
pageNext.Enabled = true;
}
private void PageCut_Click(object sender, System.EventArgs e)
{
LinkButton lb = (LinkButton)sender;
string commangArg = lb.CommandArgument.ToString();
switch (commangArg)
{
case "First":
{
//첫 페이지
BindGrid(1);
break;
}
case "Prev":
{
//앞 페이지
BindGrid(int.Parse(Label1.Text) - 1);
break;
}
case "Next":
{
//다음 페이지
BindGrid(int.Parse(Label1.Text) + 1);
break;
}
case "Last":
{
//마지막 페이지
BindGrid(int.Parse(Label2.Text));
break;
}
default:
{
break;
}
}
}
private void InitializeComponent()
{
this.pageFirst.Click += new System.EventHandler(this.PageCut_Click);
this.pagePrev.Click += new System.EventHandler(this.PageCut_Click);
this.pageNext.Click += new System.EventHandler(this.PageCut_Click);
this.pageLast.Click += new System.EventHandler(this.PageCut_Click);
}
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(ddlPage.SelectedItem.Text == "Select Page"))
{
BindGrid(Convert.ToInt32(ddlPage.SelectedValue));
}
}
}