using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
/// <summary>
/// SimplePagination 的摘要说明
/// </summary>
public class SimplePagination
{
public SimplePagination ()
{
}
/// <summary>
/// 获得总页数
/// </summary>
/// <param name="record_count">总记录数</param>
/// <param name="page_size">每页显示的记录数</param>
/// <returns>int</returns>
public static int GetPageCount (int record_count, int page_size)
{
if(record_count <= 0 || page_size <= 0)
{
return 1;
}
return (int)Math.Ceiling((double)record_count / page_size);
}
/// <summary>
/// 获得当前页
/// </summary>
/// <param name="current_page">当前页</param>
/// <param name="record_count">总记录数</param>
/// <param name="page_size">每页显示的记录数</param>
/// <returns>int</returns>
public static int GetCurrentPage (string current_page, int record_count, int page_size)
{
int current = 1;
if(current_page == null || current_page == "")
{
return 1;
}
try
{
current = int.Parse(current_page);
if(current <= 0)
{
return 1;
}
int page_count = GetPageCount(record_count, page_size);
if(current > page_count)
{
return page_count;
}
}
catch
{
return 1;
}
return current;
}
/// <summary>
/// 获得总记录数的编号索引(针对list循环,循环变量的起始值为1,最大值为每页显示的记录数)
/// </summary>
/// <param name="page_size">每页显示的记录数</param>
/// <param name="current_page">当前页</param>
/// <param name="loop">循环变量</param>
/// <returns></returns>
public static int GetIndex (int page_size, int current_page, int loop)
{
return (page_size * (current_page - 1) + loop);
}
/// <summary>
/// 分页
/// </summary>
/// <param name="current_page">当前页</param>
/// <param name="record_count">总记录数</param>
/// <param name="page_size">每页显示的记录数</param>
/// <param name="url">跳转到指定的页码</param>
/// <returns>string</returns>
public static string GetPage (int current_page, int record_count, int page_size, string url)
{
StringBuilder sb = new StringBuilder();
//分页中的控件的id和name
string text_id = "id_newpage"; //文本框的id
string text_name = "newpage"; //文本框的name
string button_id = "id_enter"; //跳转按钮的id
string button_name = "enter"; //跳转按钮的name
//分页中的样式
string css_div = ""; //分页外框DIV的样式
string css_button = "greenbtn"; //跳转按钮的样式
string strJS = GetJavaScript(text_id, url);
sb.Append(strJS);
//获得总页数
int page_count = GetPageCount(record_count, page_size);
//(1)总记录数为0或者每页显示的记录数为0
if(record_count <= 0 || page_size <= 0)
{
sb.Append("<div style='float: right'>" + "/n");
sb.Append(" 共<strong>" + record_count + "</strong>条 " + "/n");
sb.Append(" 第<span style='color:Red'>" + current_page + "</span>页/" + "/n");
sb.Append(" 共<strong>" + page_count + "</strong>页 " + "/n");
sb.Append(" <span>首页</span> " + "/n");
sb.Append(" <span>上一页</span> " + "/n");
sb.Append(" <span>下一页</span> " + "/n");
sb.Append(" <span>尾页</span> " + "/n");
//sb.Append(" 转到:<input id='" + text_id + "' name='" + text_name + "' type='text' size='2' />" + "/n");
//sb.Append(" <input id='" + button_id + "' name='" + button_name + "' type='button' value='go' οnclick='' />" + "/n");
sb.Append("</div>" + "/n");
return sb.ToString();
}
//(2)当前页小于等于0
if(current_page <= 0)
{
current_page = 1;
}
//(3)总记录数小于等于每页显示的记录数
if(record_count <= page_size)
{
sb.Append("<div style='float: right'>" + "/n");
sb.Append(" 共<strong>" + record_count + "</strong>条 " + "/n");
sb.Append(" 第<span style='color:Red'>1</span>页/" + "/n");
sb.Append(" 共<strong>1</strong>页 " + "/n");
sb.Append(" <span>首页</span> " + "/n");
sb.Append(" <span>上一页</span> " + "/n");
sb.Append(" <span>下一页</span> " + "/n");
sb.Append(" <span>尾页</span> " + "/n");
sb.Append(" 转到:<input id='" + text_id + "' name='" + text_name + "' type='text' size='2' />" + "/n");
sb.Append(" <input class='" + css_button + "' id='" + button_id + "' name='" + button_name + "' type='button' value='go' οnclick='pageRedirect()' />" + "/n");
sb.Append("</div>" + "/n");
}
else
{
if(current_page == 1)
{
sb.Append("<div style='float: right'>" + "/n");
sb.Append(" 共<strong>" + record_count + "</strong>条 " + "/n");
sb.Append(" 第<span style='color:Red'>1</span>页/" + "/n");
sb.Append(" 共<strong>" + page_count + "</strong>页 " + "/n");
sb.Append(" <span>首页</span> " + "/n");
sb.Append(" <span>上一页</span> " + "/n");
sb.Append(" <span><a href='" + url + (current_page + 1).ToString() + "'>下一页</a></span> " + "/n");
sb.Append(" <span><a href='" + url + page_count + "'>尾页</a></span> " + "/n");
sb.Append(" 转到:<input id='" + text_id + "' name='" + text_name + "' type='text' size='2' />" + "/n");
sb.Append(" <input class='" + css_button + "' id='" + button_id + "' name='" + button_name + "' type='button' value='go' οnclick='pageRedirect()' />" + "/n");
sb.Append("</div>" + "/n");
}
else if((current_page > 1) && (current_page < page_count))
{
sb.Append("<div style='float: right'>" + "/n");
sb.Append(" 共<strong>" + record_count + "</strong>条 " + "/n");
sb.Append(" 第<span style='color:Red'>" + current_page + "</span>页/" + "/n");
sb.Append(" 共<strong>" + page_count + "</strong>页 " + "/n");
sb.Append(" <span><a href='" + url + 1.ToString() + "'>首页</a></span> " + "/n");
sb.Append(" <span><a href='" + url + (current_page - 1).ToString() + "'>上一页</a></span> " + "/n");
sb.Append(" <span><a href='" + url + (current_page + 1).ToString() + "'>下一页</a></span> " + "/n");
sb.Append(" <span><a href='" + url + page_count + "'>尾页</a></span> " + "/n");
sb.Append(" 转到:<input id='" + text_id + "' name='" + text_name + "' type='text' size='2' />" + "/n");
sb.Append(" <input class='" + css_button + "' id='" + button_id + "' name='" + button_name + "' type='button' value='go' οnclick='pageRedirect()' />" + "/n");
sb.Append("</div>" + "/n");
}
else if(current_page >= page_count)
{
sb.Append("<div style='float: right'>" + "/n");
sb.Append(" 共<strong>" + record_count + "</strong>条 " + "/n");
sb.Append(" 第<span style='color:Red'>" + current_page + "</span>页/" + "/n");
sb.Append(" 共<strong>" + page_count + "</strong>页 " + "/n");
sb.Append(" <span><a href='" + url + 1.ToString() + "'>首页</a></span> " + "/n");
sb.Append(" <span><a href='" + url + (current_page - 1).ToString() + "'>上一页</a></span> " + "/n");
sb.Append(" <span>下一页</span> " + "/n");
sb.Append(" <span>尾页</span> " + "/n");
sb.Append(" 转到:<input id='" + text_id + "' name='" + text_name + "' type='text' size='2' />" + "/n");
sb.Append(" <input class='" + css_button + "' id='" + button_id + "' name='" + button_name + "' type='button' value='go' οnclick='pageRedirect()' />" + "/n");
sb.Append("</div>" + "/n");
}
}
return sb.ToString();
}
public static string GetJavaScript(string text_id, string url)
{
StringBuilder sb = new StringBuilder();
sb.Append("/n");
sb.Append("<script type='text/javascript'>" + "/n");
sb.Append(" function pageRedirect()" + "/n");
sb.Append(" {" + "/n");
sb.Append(" var txt_page = document.getElementById('" + text_id + "').value;" + "/n");
sb.Append(" if(txt_page == '')" + "/n");
sb.Append(" {" + "/n");
sb.Append(" return;" + "/n");
sb.Append(" }" + "/n");
sb.Append(" window.location.href='" + url + "' + escape(txt_page);" + "/n");
sb.Append(" }" + "/n");
sb.Append("</script>" + "/n");
sb.Append("/n");
return sb.ToString();
/*
<script type='text/javascript'>
function pageRedirect()
{
var txt_page = document.getElementById('id_newpage').value;
if(txt_page == '')
{
return;
}
window.location.href='OnlineBroker.aspx?page=' + escape(txt_page);
}
</script>
*/
}
}
/*
分页
<div style='float: right'>
共<strong>108</strong>条
第<span style='color:Red'>2</span>页/
共<strong>3</strong>页
<span><a href='#'>首页</a></span>
<span><a href='#'>上一页</a></span>
<span><a href='#'>下一页</a></span>
<span><a href='#'>尾页</a></span>
转到:<input id='id_newpage' name='newpage' type='text' size='2' />
<input id='id_enter' name='enter' type='button' value='go' οnclick='' />
</div>
function pageRedirect()
{
var txt_page = document.getElementById("id_newpage").value;
//alert(txt_page);
window.location.href="OnlineBroker.aspx?page=" + txt_page;
}
*/