Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印 安全与加密 图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程 XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > 尚未分类 >

仿百度分页生成页码不涉及数据源

时间:2010-01-21 16:14来源:未知 作者:admin 点击:

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(1, 10);  
        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();//输出页码  
    }  

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(1, 10);
        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

view plaincopy to clipboardprint?
<%@ 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> 

(责任编辑:admin)
Tags:分页
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价