简单的数字分页代码

分页说来简单但也不易,网上分页控件数不胜数,苦于找不到适合自己用的只好自己做一个,不是说成型的控件不好,只是觉得功能太强大,而且调用过程实在复杂。

要实现复杂功能的朋友不必往下再看,本程序仅实现了简单分页、可以数字显示(可固定显示页数)、数据源为datatable,仅此而已。话不多数上代码。

ContractedBlock.gif ExpandedBlockStart.gif aspx
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="adks.Web.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>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:Repeater ID="myRep" runat="server" OnItemDataBound="myRep_ItemDataBound">
                
                
<ItemTemplate>
                    
<li>
                        
<%#Eval("mycol"%>
                    
</li>
                
</ItemTemplate>
                
                
<FooterTemplate>
                   
                      
<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>
                      
<asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
                      
                      
<asp:PlaceHolder ID="myPh" runat="server"></asp:PlaceHolder>
                    
                      
<asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
                      
<asp:HyperLink ID="hlla" runat="server" Text="末页"></asp:HyperLink>
                      
<br />
                    共
<asp:Label ID="lbl_Count" runat="server" Text=""></asp:Label>    条记录
                      
<asp:Label ID="lbl_PageCount" runat="server" Text=""></asp:Label>条记录/页 
                    共
<asp:Label ID="lbl_all" runat="server" Text=""></asp:Label>
              当前为第
<asp:Label ID="lbl_cur" runat="server" Text=""></asp:Label>页   
                
</FooterTemplate>
            
</asp:Repeater>
        
</div>
    
</form>
</body>
</html>

 

 

ContractedBlock.gif ExpandedBlockStart.gif aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

namespace adks.Web
{
    
public partial class Default : System.Web.UI.Page
    {
        
private int Count = 0;
        
private int pageCount = 20;                       //显示数
        private string pagename = "Default.aspx?";        //跳转页


        
protected void Page_Load(object sender, EventArgs e)
        {

            myRep.DataSource 
= SetPds();
            myRep.DataBind();

        }

        
public PagedDataSource SetPds()
        {
            DataTable myTable 
= new DataTable();
            myTable.Columns.Add(
"mycol"typeof(string));
            
for (int i = 0; i < 1000; i++)
            {
                DataRow myr 
= myTable.NewRow();
                myr[
"mycol"= "gudao " + i.ToString() + " haha";
                myTable.Rows.Add(myr);
            }

            PagedDataSource myPds 
= new PagedDataSource();
            myPds.AllowPaging 
= true;
            myPds.DataSource 
= myTable.DefaultView;
            myPds.PageSize 
= pageCount;
            Count 
= myTable.Rows.Count;
            
if (Request.QueryString["p"!= null)
                myPds.CurrentPageIndex 
= Convert.ToInt32(Request.QueryString["p"]);
            
else
                myPds.CurrentPageIndex 
= 0;
            
return myPds;

        }

        
protected void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            
if (e.Item.ItemType == ListItemType.Footer)
            {
                
int currentPage = SetPds().CurrentPageIndex;    //当前页号
                int pageCount = SetPds().PageCount;             //分页数量
                int startPage, endPage;                         //开始分页号 结束分页号
                int proPageCount = 5;                           //当前页前显示数
                int nextPageCount = 4;

                Label lbl_c 
= (Label)e.Item.FindControl("lbl_cur");
                lbl_c.Text 
= Convert.ToString(currentPage + 1);
                Label lbl_a 
= (Label)e.Item.FindControl("lbl_all");
                lbl_a.Text 
= Convert.ToString(pageCount);

                HyperLink hl_fir 
= (HyperLink)e.Item.FindControl("hlfir");
                hl_fir.NavigateUrl 
= pagename + "p=0";
                HyperLink hl_p 
= (HyperLink)e.Item.FindControl("hlp");
                
                HyperLink hl_n 
= (HyperLink)e.Item.FindControl("hln");
                
                HyperLink hl_la 
= (HyperLink)e.Item.FindControl("hlla");
                hl_la.NavigateUrl 
= pagename + "p=" + Convert.ToString(pageCount - 1);

                
//数据量
                Label lbl_count = (Label)e.Item.FindControl("lbl_Count");

                lbl_count.Text 
= Convert.ToString(this.Count);

                
//每页显示数据量
                Label lbl_PageCount = (Label)e.Item.FindControl("lbl_PageCount");
                lbl_PageCount.Text 
= Convert.ToString(this.pageCount);

                
if (currentPage <= 0)
                {
                    hl_fir.Enabled 
= false;
                    hl_p.Enabled 
= false;
                    hl_n.Enabled 
= true;
                    hl_la.Enabled 
= true;
                }
                
else
                {
                    hl_p.NavigateUrl 
= pagename + "p=" + Convert.ToString(currentPage - 1);
                }
                
if (currentPage > pageCount - 2)
                {
                    hl_fir.Enabled 
= true;
                    hl_p.Enabled 
= true;
                    hl_n.Enabled 
= false;
                    hl_la.Enabled 
= false;
                    
if (currentPage == 0)
                    {
                        hl_fir.Enabled 
= true;
                        hl_p.Enabled 
= false;
                        hl_n.Enabled 
= false;
                        hl_la.Enabled 
= false;
                    }
                }
                
else
                {
                    hl_n.NavigateUrl 
= pagename + "p=" + Convert.ToString(currentPage + 1);
                }


                
//动态生成数字页码
                    
//获得第一个页号
                if (currentPage <= proPageCount)
                    startPage 
= 0;
                
else
                {
                    
int linkLength = (pageCount - currentPage + 1+proPageCount;

                     startPage 
= currentPage - proPageCount;

                    
while (linkLength < proPageCount + nextPageCount + 1 && startPage > 1)
                    {
                        linkLength
++;
                        startPage
--;
                    }
                }
                   
//获得最后一个页号
                if (currentPage + nextPageCount >= pageCount)
                    endPage 
= pageCount;
                
else
                {
                    
int linkLength = (currentPage - startPage + 1+ nextPageCount;

                   endPage 
= currentPage + nextPageCount;

                    
while (linkLength < proPageCount + nextPageCount + 1 && endPage < pageCount)
                    {
                        linkLength
++;
                        endPage
++;
                    }
                }

                
//循环生成页号
                for (int i = startPage; i < endPage; i++)
                {
                    Label lb1 
= new Label();
                    lb1.Text 
= "[";
                    Label lb2 
= new Label();
                    lb2.Text 
= "]";

                    HyperLink myLkb 
= new HyperLink();
                    myLkb.ID 
= ((int)(i + 1)).ToString();
                    myLkb.NavigateUrl 
= pagename + "p=" + i;
                    myLkb.Text 
= ((int)(i + 1)).ToString();
                    ((PlaceHolder)e.Item.FindControl(
"myPh")).Controls.Add(lb1);
                    ((PlaceHolder)e.Item.FindControl(
"myPh")).Controls.Add(myLkb);
                    ((PlaceHolder)e.Item.FindControl(
"myPh")).Controls.Add(lb2);
                }
            }
        }
    }
    
}

 

如果有需要传递其它参数的请将pagename修改并加上自己的参数重新构造。如果数据量特别大建议不要使用。

 

转载于:https://www.cnblogs.com/gudao119/archive/2009/03/11/1408810.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值