仿百度产生页码(仅是表现形式)可自行附加数据源

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
public partial class _Default : System.Web.UI.Page 
{
    
int ToatalCountRecord = 0;
    
public string pageHmtl = string.Empty;
    
protected void Page_Load(object sender, EventArgs e)
    {
        
//无数据源测试
        
//仅仅是个模拟
        ToatalCountRecord = 18;
        
if (Request.QueryString["page"== null)
            pageHmtl 
= BuildPages(110);
        
else
            pageHmtl 
= BuildPages(int.Parse(Request.QueryString["page"].ToString()), 10);
        
//pageHmtl在aspx页面    用<%=pageHmtl%> 输出
    }
    
/// <summary>
    
/// 构造UI的页面分页演样式及布局不涉及数据
    
/// </summary>
    
/// <param name="PageIndex">当前页索引</param>
    
/// <param name="PageSize">页尺寸(就是每页显示几条)</param>
    
/// <returns></returns>
    public string BuildPages(int PageIndex, int PageSize)
    {
        
int PageStep = 5;//偏移量 
        int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageSize);//页数
        int NumStep = (int)Math.Ceiling((double)(PageCount) / PageStep);//步数
        int CurrentStep=0;
        
int isNext = 0;//标识符判断是否找到当前页对应的数值
        for (int n =1; n <= NumStep;n++ )//循环步数
        {
            
for (int k = (n - 1* PageStep+1; k <= n * PageStep; k++)
            {
                
// 当前页在索引范围内时
                if (PageIndex == k)
                {
                    CurrentStep 
= n;
                    isNext 
= 1;
                    
break;//退出内循环
                }
            }
            
if (isNext == 1)
                
break;//退出外循环
        }
        
string PageUrl = Request.CurrentExecutionFilePath;//当前页
        int LeftNum = (CurrentStep - 1* PageStep + 1;//左界限
        int RightNum = PageStep * CurrentStep;//右界限
        if (PageCount <= PageStep)
        { 
            LeftNum 
= 1;
            RightNum 
= PageCount;
        }
        
if (PageCount % PageStep != 0 && CurrentStep == NumStep)
            RightNum 
= PageCount % PageStep + (LeftNum-1);
        StringBuilder st 
= new StringBuilder();
        st.Append(
"<div id=\"pageInfo\" class=\"pages\">\n");
        
if (PageIndex > 1)//第一页判断
        {
            st.Append(
"<a href=\"" + PageUrl + "?page=1\" >首页</a>\n");
            st.Append(
"<a href=\"" + PageUrl + "?page=" + (PageIndex - 1).ToString() + "\" >上一页</a>\n");
        }
        
else
        {
            st.Append(
"<a  disable=\"disable\" >首页</a>\n");
            st.Append(
"<a  disable=\"disable\" >上一页</a>\n");
        }
        
if (LeftNum > 1)
            st.Append(
"<a href=\"" + PageUrl + "?page=" + (LeftNum - 1).ToString() + "\" >...</a>\n");
        
for (int p = LeftNum; p <= RightNum; p++)
        {
            
if (p == PageIndex)//判断当前页
                st.Append("<a  class=\"currentbtn\" disable=\"disable\">" + p.ToString() + "</a>\n");
            
else
                st.Append(
"<a href=\"" + PageUrl + "?page=" + p.ToString() + "\">" + p.ToString() + "</a>\n");
        }
        
if ((LeftNum + PageStep - 1<= PageCount)
            st.Append(
"<a href=\"" + PageUrl + "?page=" + (RightNum + 1).ToString() + "\">...</a>\n");
        
if (PageIndex < PageCount)//尾页判断
        {
            st.Append(
"<a href=\"" + PageUrl + "?page=" + (PageIndex + 1).ToString() + "\" >下一页</a>\n");
            st.Append(
"<a href=\"" + PageUrl + "?page=" + PageCount.ToString() + "\" >尾页</a>\n");
        }
        
else
        {
            st.Append(
"<a   disable=\"disable\" >下一页</a>\n");
            st.Append(
"<a   disable=\"disable\" >尾页</a>\n");
        }
        st.Append(
"</div>");
        
return st.ToString();//输出页码
    }
}

html

代码
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
        
<style type="text/css">
.pages 
{ color: #999; }
.pages a, .pages .currentbtn 
{text-decoration:none;float:left;padding:0 5px;border: 1px solid #ddd;background:#ffff;margin:0 2px;color:#000;}
.pages a:hover
{background-color: #E61636; color:#fff;border:1px solid #E61636;  text-decoration:none;}
.pages .currentbtn 
{font-weight: bold;color: #fff; background: #E61636;  border:1px solid #E61636;}
</style>

</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<%=pageHmtl%>
    
</div>
    
</form>
</body>
</html>


 

posted @ 2009-10-28 12:32  麦兜很乖  阅读(556)  评论(1编辑  收藏  举报