ASP.NETページング
サンプルコード:
/// <summary>
/// ページ内容
/// </summary>
/// <param name="size">サイズ</param>
/// <param name="count">ページ数</param>
/// <param name="currendIndex">現在のページ</param>
/// <param name="pattern">urlモード:demo.aspx?page={0}</param>
/// <param name="target">ウィンドウモード</param>
/// <returns></returns>
public static string get_pagenation(int size,
int count,
int currendIndex,
string pattern,
string target)
{
//1>ウィンドウのターゲットを開く
target = string.IsNullOrEmpty(target) ? “_top" : target;
//2>ページ
int pageCount = count / size;
pageCount = pageCount * size == count ? pageCount : pageCount + 1;
//3>ページコンテンツ
StringBuilder strHtml = new StringBuilder();
strHtml.Append(“<span class=’pagenation’>");
#region ヘッダ処理
if (currendIndex > 1)
{
strHtml.AppendFormat(“<a href=’1′ target='{0}’>[最初]</a>", target);
strHtml.AppendFormat(“<a href='{0}’ target='{1}’>[前のページ]</a>", string.Format(pattern, currendIndex – 1), target);
}
else
{
strHtml.Append(“<span class=’disabled’>[最初]</span> <span class=’disabled’>[前のページ]</span>");
}
#endregion
#region 中間部分
int i = 1;
int right = (currendIndex + 4) > pageCount ? pageCount : currendIndex + 4;
if (currendIndex > 6)
{
i = currendIndex – 5;
}
else
{
right = pageCount >= 10 ? 10 : pageCount;
}
for (; i <= right; i++)
{
if (i == currendIndex)
{
strHtml.AppendFormat(“<font class=’current’>{0}</font>", i);
strHtml.AppendLine();
continue;
}
strHtml.AppendFormat(“<a href='{0}’ target='{1}’>[{2}]</a>", string.Format(pattern, i), target, i);
strHtml.AppendLine();
}
#endregion
#region フッター処理
if (currendIndex == pageCount)
{
strHtml.Append(“<span class=’disabled’>[次のページ]</span><span class=’disabled’>[最後]</span>");
strHtml.AppendFormat(“計({0})", pageCount);
}
else
{
strHtml.AppendFormat(“<a href='{0}’ target='{1}’>[次のページ]</a>", string.Format(pattern, currendIndex + 1), target);
strHtml.AppendFormat(“<a href='{0}’ target='{1}’>[最後]</a>", string.Format(pattern, pageCount), target);
strHtml.AppendFormat(“ <label>計({0})ページ</label>", pageCount);
}
#endregion
strHtml.Append(“</span>");
return strHtml.ToString();
}