自定义的一个分页类

1》

this.Literal1.Text 其实这个Literal控件就是生成一个DIV而已。


首先是在WebForm页面上拖一个Literal控件,或者放一个div控件,给他改成服务端控件,然后给strPage方法的返回值给它。strPage方法的返回值就是一个分页字符串


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.BLL
{
    public static class PagerHelper
    {
        #region 数字分页类
        /// <summary>
        /// 
        /// </summary>
        /// <param name="intCounts">数据总条数</param>
        /// <param name="intPageSizes">页大小</param>
        /// <param name="intPageCounts">总共页数</param>
        /// <param name="intThisPages">当前页</param>
        /// <param name="strUrl">要提交到的页面</param>
        /// 使用: this.Literal1.Text = PagerHelper.strPage(6, 3, 2, id, "WebForm3.aspx?id=");
        /// <returns></returns>
        public static string strPage(int intCounts, int intPageSizes, int intPageCounts, int intThisPages, string strUrl)
        {
            int intCount = Convert.ToInt32(intCounts); //总记录数
            int intPageCount = Convert.ToInt32(intPageCounts); //总共页数
            int intPageSize = Convert.ToInt32(intPageSizes); //每页显示
            int intPage = 7;  //数字显示
            int intThisPage = Convert.ToInt32(intThisPages); //当前页数
            int intBeginPage = 0; //开始页数
            int intCrossPage = 0; //变换页数
            int intEndPage = 0; //结束页数
            string strPage = null; //返回值

            intCrossPage = intPage / 2;
                strPage = "共 <font color=\"#FF0000\">" + intCount.ToString() + "</font> 条记录 第 <font color=\"#FF0000\">" + intThisPage.ToString() + "/" + intPageCount.ToString() + "</font> 页 每页 <font color=\"#FF0000\">" + intPageSize.ToString() + "</font> 条     ";
                if (intThisPage > 1)
                {
                    strPage = strPage + "<a href=\"" + strUrl + "1\">首页</a> ";
                    strPage = strPage + "<a href=\"" + strUrl + Convert.ToString(intThisPage - 1) + "\">上一页</a> ";
                }
                if (intPageCount > intPage)
                {
                    if (intThisPage > intPageCount - intCrossPage)
                    {
                        intBeginPage = intPageCount - intPage + 1;
                        intEndPage = intPageCount;
                    }
                else
                {
                    if (intThisPage <= intPage - intCrossPage)
                    {
                        intBeginPage = 1;
                        intEndPage = intPage;
                    }
                    else
                    {
                        intBeginPage = intThisPage - intCrossPage;
                        intEndPage = intThisPage + intCrossPage;
                    }
                }
            }
            else
            {
                intBeginPage = 1;
                intEndPage = intPageCount;
            }
            if (intCount > 0)
            {

                for (int i = intBeginPage; i <= intEndPage; i++)
                {
                    if (i == intThisPage)
                    {
                        strPage = strPage + " <font color=\"#FF0000\">" + i.ToString() + "</font> ";
                    }
                    else
                    {
                        strPage = strPage + " <a href=\"" + strUrl + i.ToString() + "\" title=\"第" + i.ToString() + "页\">" + i.ToString() + "</a> ";
                    }
                }
            }
            if (intThisPage < intPageCount)
            {
                strPage = strPage + "<a href=\"" + strUrl + Convert.ToString(intThisPage + 1) + "\">下一页</a> ";
                strPage = strPage + "<a href=\"" + strUrl + intPageCount.ToString() + "\">尾页</a> ";
            }
            return strPage;
        }
        #endregion
    }
}

调用的时候

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Web.BLL;
using Web.Model;

namespace WebApp
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            if (Request["id"] == null)
            {
                id = 1;
            }
            else
            {
                id = Convert.ToInt32(Request["id"].ToString());
            }
            //int id = Request["id"].ToString()==null?1:int.Parse(Request["id"].ToString());
            //UserBll bll = new UserBll();
            //List<UserInfo> list = bll.PagingPage(id);
            //this.Repeater1.DataSource = list;
            //this.Repeater1.DataBind();


            //每次页面加载的时候生成分页的导航HTML,并设置到Literal1
            this.Literal1.Text = PagerHelper.strPage(6, 3, 2, id, "WebForm3.aspx?id=");

        }

       
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值