내가 사용하는 페이징클래스[C#]
일단, 소스부터 보자면..
[CPaging.cs]
using System;
using System.Text;
/// <summary>
/// auth. : ranos94@gmail.com
/// date : 2007-12-01
/// desc. : 페이지 Navigation UI를 보여준다.
/// *퍼가실땐 출처를..*
/// copyright xwing.tistory.com
/// </summary>
public class CPaging : System.Web.UI.Page
{
private int page;
private int rowPerPage;
private int totalRecord;
private int totalPage;
private int sPage;
private int ePage;
private string targetUrl = String.Empty;
private string addParam = string.Empty;
public PagingType pagingType;
public enum PagingType
{
Numeric,
PrevNext,
BoxNumeric
}
public int PageNo
{
set { page = value; }
get { return page; }
}
public int RowPerPage
{
set { rowPerPage = value; }
get { return rowPerPage; }
}
public int TotalRecord
{
set { totalRecord = value;}
get { return totalPage; }
}
public int TotalPage
{
set { totalPage = value; }
get { return totalPage; }
}
public string TargetURL
{
set { this.targetUrl = value; }
get { return this.targetUrl; }
}
public string AddParam
{
set { this.addParam = value; }
get { return this.addParam; }
}
public CPaging()
{
pagingType = PagingType.Numeric;
}
/// <summary>
/// paging 에 필요한 변수를 설정하고, 페이지 렌더링함수를 호출한다.
/// </summary>
private void PagingInit()
{
if ((this.page % this.rowPerPage) == 0)
{
this.sPage = ((this.page / this.rowPerPage) * this.rowPerPage + 1) - this.rowPerPage;
this.ePage = this.sPage + (this.rowPerPage - 1);
}
else
{
this.sPage = (this.page / this.rowPerPage) * this.rowPerPage + 1;
this.ePage = this.sPage + (this.rowPerPage - 1);
}
if (this.ePage >= this.totalPage)
this.ePage = this.totalPage;
}
/// <summary>
/// Paging 을 그린다.
/// </summary>
public string RenderPaging()
{
PagingInit();
switch (pagingType)
{
case PagingType.Numeric:
return RenderNumeric();
case PagingType.PrevNext:
return RenderPrevNext();
case PagingType.BoxNumeric:
return RenderBoxNumeric();
default:
return RenderNumeric();
}
}
/// <summary>
/// 번호로 보여준다.
/// </summary>
/// <returns></returns>
private string RenderNumeric()
{
StringBuilder sb = new StringBuilder();
sb.Append("<table border=0 align='center' cellpadding=0><tbody><tr>");
if (this.sPage > this.rowPerPage)
{
int next10 = this.sPage - this.rowPerPage;
sb.Append("<td width=80 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\">");
sb.Append("<a href='" + targetUrl + "?page=" + next10.ToString() + "&" + AddParam + "'><< 이전10개</a></td>");
}
for (int i = sPage; i <= ePage; i++)
{
if (this.page == i)
{
sb.Append("<td width=25 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\" style=\"color:red;font-weight:bold\" >");
sb.Append(i.ToString() + "</td>");
}
else
{
sb.Append("<td width=25 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\" style=\"font-weight:bold\">");
sb.Append("<a href='" + targetUrl + "?page=" + i.ToString() + "&" + AddParam + "'>" + i.ToString() + "</a></td>");
}
}
if (this.totalPage > this.ePage)
{
int next10 = this.sPage + this.rowPerPage;
sb.Append("<td width=80 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\">");
sb.Append("<a href='" + targetUrl + "?page=" + next10.ToString() + "&" + AddParam + "'>다음10개 >></a></td>");
}
sb.Append("</tr></tbody></table>");
return sb.ToString();
}
/// <summary>
/// Prev, Next 형식의 페이징을 만든다.
/// </summary>
/// <returns></returns>
private string RenderPrevNext()
{
StringBuilder sb = new StringBuilder();
sb.Append("<table border=0 align='center' cellpadding=0><tbody><tr>");
if (this.page > 1)
{
int prev = this.page - 1;
sb.Append("<td width=80 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\">");
sb.Append("<a href='" + targetUrl + "?page=" + prev.ToString() + "&" + AddParam + "'><img src='/images/board/icon_back.gif' border=0></a></td>");
}
if (this.totalPage > this.page)
{
int next = this.page + 1;
sb.Append("<td width=80 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\">");
sb.Append("<a href='" + targetUrl + "?page=" + next.ToString() + "&" + AddParam + "'><img src='/images/board/icon_next.gif' border=0></a></td>");
}
sb.Append("</tr></tbody></table>");
return sb.ToString();
}
/// <summary>
/// 사각형 형태의 모양을 보여준다.
/// </summary>
/// <returns></returns>
private string RenderBoxNumeric()
{
StringBuilder sb = new StringBuilder();
sb.Append("<table align='center' cellpadding=0 style=\"border:0; border-collapse:collapse;\"><tbody><tr>");
if (this.sPage > this.rowPerPage)
{
int next10 = this.sPage - this.rowPerPage;
sb.Append("<td width=80 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\">");
sb.Append("<a href='" + targetUrl + "?page=" + next10.ToString() + "&" + AddParam + "'><< 이전10개</a></td>");
}
for (int i = sPage; i <= ePage; i++)
{
if (this.page == i)
{
sb.Append("<td width=25 align=\"center\" style=\"color:red;background-color:#FFFFFF;border-color:#ACACEC;font-weight:bold;border:1px solid;\" >");
sb.Append(i.ToString() + "</td>");
}
else
{
sb.Append("<td width=25 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\" style=\"font-weight:bold;border:0 solid;\" >");
sb.Append("<a href='" + targetUrl + "?page=" + i.ToString() + "&" + AddParam + "'>" + i.ToString() + "</a></td>");
}
}
if (this.totalPage > this.ePage)
{
int next10 = this.sPage + this.rowPerPage;
sb.Append("<td width=80 align=\"center\" onMouseOver = \"onStyle(this)\" onMouseOut = \"offStyle(this)\">");
sb.Append("<a href='" + targetUrl + "?page=" + next10.ToString() + "&" + AddParam + "'>다음10개 >></a></td>");
}
sb.Append("</tr></tbody></table>");
return sb.ToString();
}
}
그리고, 마우스오버에 필요한 Script
// JScript 파일
function onStyle(obj){
obj.style.background = '#EFF2FE';
obj.style.border='1px solid';
}
function offStyle(obj){
obj.style.background = '#FFFFFF';
obj.style.border = '0px';
}
실제 소스적용예를 보자!
CPaging paging = new CPaging();
paging.pagingType = CPaging.PagingType.PrevNext;
paging.TargetURL = "/board/dev/devInfo.aspx";
paging.PageNo = currentPage; //페이지번호
paging.RowPerPage = 10; //한페이지에 보여줄 row수
paging.AddParam = "p_type=" + p_type;
paging.TotalPage = Int32.Parse(param2.Value.ToString()); // 총 페이지수
paging.TotalRecord = Int32.Parse(param1.Value.ToString()); // 총 레코드수
string pageLayout = paging.RenderPaging();
PageLabel.Text = pageLayout;
이와 같이 사용한다.
오늘도 좋은 하루 되시길...
'개발 > C# .NET' 카테고리의 다른 글
C# 간단 트레이닝 1 (0) | 2008.10.17 |
---|---|
ASP.NET 간단 팁들.. (0) | 2008.10.09 |
웹 Bitmap 그리기 (0) | 2008.10.08 |