分页程序
针对搜索引擎无法识别asp.net的LinkButton 等超链接类控件,让网站的在优化时候出现一定的局限性,下午无聊写了一个分页程序:
效果如下:
代码如下:
Code
DIV.technorati {
PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center
}
DIV.technorati A {
BORDER-RIGHT: #ccc 1px solid; PADDING-RIGHT: 6px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #ccc 1px solid; PADDING-LEFT: 6px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; BORDER-LEFT: #ccc 1px solid; COLOR: rgb(66,97,222); MARGIN-RIGHT: 3px; PADDING-TOP: 2px; BORDER-BOTTOM: #ccc 1px solid; TEXT-DECORATION: none
}
DIV.technorati A:hover {
BACKGROUND-IMAGE: none; COLOR: #fff; BACKGROUND-COLOR: #4261df
}
DIV.technorati A:active {
BACKGROUND-IMAGE: none; COLOR: #fff; BACKGROUND-COLOR: #4261df
}
DIV.technorati SPAN.current {
PADDING-RIGHT: 6px; PADDING-LEFT: 6px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; COLOR: #000; MARGIN-RIGHT: 3px; PADDING-TOP: 2px
}
DIV.technorati SPAN.disabled {
DISPLAY: none
}
CodeDIV.technorati {
PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center
}
DIV.technorati A {
BORDER-RIGHT: #ccc 1px solid; PADDING-RIGHT: 6px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #ccc 1px solid; PADDING-LEFT: 6px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; BORDER-LEFT: #ccc 1px solid; COLOR: rgb(66,97,222); MARGIN-RIGHT: 3px; PADDING-TOP: 2px; BORDER-BOTTOM: #ccc 1px solid; TEXT-DECORATION: none
}
DIV.technorati A:hover {
BACKGROUND-IMAGE: none; COLOR: #fff; BACKGROUND-COLOR: #4261df
}
DIV.technorati A:active {
BACKGROUND-IMAGE: none; COLOR: #fff; BACKGROUND-COLOR: #4261df
}
DIV.technorati SPAN.current {
PADDING-RIGHT: 6px; PADDING-LEFT: 6px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; COLOR: #000; MARGIN-RIGHT: 3px; PADDING-TOP: 2px
}
DIV.technorati SPAN.disabled {
DISPLAY: none
}
/// <summary>
/// 分页程序
/// </summary>
/// <param name="_tableName">表名</param>
/// <param name="_id">主键字段名 </param>
/// <param name="_sqlWhere">sql语句的where条件 不带where </param>
/// <param name="_pageIndex">当前页面索引</param>
/// <param name="_pageSize">页面显示数据条</param>
/// <param name="_pageUrl">指向页面的文件,比如"index.aspx"</param>
/// <param name="_itemSize">每次显示分页项目数</param>
/// <returns></returns>
public string webPage(string _tableName,string _id,string _sqlWhere ,int _pageIndex,int _pageSize,string _pageUrl,int _itemSize)
{
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"technorati\">");
double _pageCount=0;
int pageCount=0;
string _pagestr = "";
string _sqlCom = "";
if (_sqlWhere != "")
{
_sqlCom= "select " + _id + " from " + _tableName + " where " + _sqlWhere;
}
else {
_sqlCom = "select " + _id + " from " + _tableName;
}
DataTable dt=publicclass.InquirySql(_sqlCom);
//没有数据则返回
if(dt.Rows.Count<1)
return "";
//
int _DataRowCount=dt.Rows.Count;
_pageCount=Convert.ToDouble(_DataRowCount/_pageSize);
//获取总页数
if (Convert.ToDouble(_DataRowCount) / _pageSize > Convert.ToInt32(Convert.ToInt32(_DataRowCount) / _pageSize))
{
pageCount= Convert.ToInt32(Convert.ToInt32(_DataRowCount) / _pageSize) + 1 ;
}
else
{
pageCount = Convert.ToInt32(_DataRowCount / _pageSize);
}
_pagestr = "本栏共有:" + _DataRowCount.ToString() + " 条 每页" + _pageSize.ToString() + " 条 当前第" + _pageIndex.ToString() + " 页 / 共" + pageCount.ToString() + " 页 ";
if (pageCount == 1)
{
return _pagestr;
}
sb.Append(_pagestr);
if ((_pageIndex - 1)>0)
sb.Append("<a href=\""+_pageUrl+"?page="+Convert.ToString(_pageIndex-1)+"\">上一页 </a>");
if (pageCount <= _itemSize)
{
//当前页面之前的页面
for (int i = 1; i < _pageIndex; i++)
{
sb.Append("<a href=\"" + _pageUrl + "?page=" + i.ToString() + "\">" + Convert.ToString(i) + "</a>");
}
sb.Append("<span class=\"current\">" + _pageIndex.ToString() + "</span>");
//当前页面之后的页面
for (int i = (_pageIndex + 1); i <= pageCount; i++)
{
sb.Append("<a href=\"" + _pageUrl + "?page=" + i.ToString() + "\">" + Convert.ToString(i) + "</a>");
}
}
else {
int itemSize=0;
if (_itemSize % 2 == 0)
{
itemSize = _itemSize / 2;
}
else
{
itemSize = (_itemSize / 2) +1;
}
int pageMin= _pageIndex - itemSize;
if (pageMin <= 0)
{
pageMin = 0;
}
//当前页面之前的页面
for (int i = (pageMin+1); i <_pageIndex; i++)
{
sb.Append("<a href=\"" + _pageUrl + "?page=" + Convert.ToString(i) + "\">" + Convert.ToString(i) + "</a>");
}
sb.Append("<span class=\"current\">" + _pageIndex.ToString() + "</span>");
//当前页面之后的页面
int pageMax = _pageIndex+itemSize;
if (pageMax >= pageCount)
pageMax=pageCount;
for (int j = (_pageIndex+1); j < (pageMax+1); j++)
{
sb.Append("<a href=\"" + _pageUrl + "?page=" + Convert.ToString( j) + "\">" + Convert.ToString(j) + "</a>");
}
}
if ((_pageIndex + 1) <= pageCount)
{
sb.Append("<a href=\"" + _pageUrl + "?page=" + Convert.ToString(_pageIndex+1) + "\">" + "下一页" + "</a>");
}
sb.Append("</div>");
return Convert.ToString(sb);
}
一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。