Repeater、DataList分页

本文介绍使用PagedDataSource类为Web控件Repeater和DataList实现分页。 PagedDataSource封装数据绑定控件(如 DataGrid、GridView、DetailsView 和 FormView)的与分页相关的属性,以允许该控件执行分页操作。下面以一个实例来介绍如何实现分页:

页面代码:

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Home.aspx.cs "  Inherits = " Home "   %>

<! 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" >
        BODY 
{  MARGIN :  0px  }
        TD 
{  FONT-SIZE :  13px  }
    
</ style >
</ head >
< body  bgcolor ="#f2ead5"  ms_positioning ="GridLayout"  style ="font-size: small;" >
    
< form  id ="form1"  runat ="server" >
        
< div >
            
< table  cellspacing ="0"  cellpadding ="0"  width ="100%" >
                
< tr >
                    
< td  style ="border-right: white thin groove; border-top: white thin groove; padding-left: 5px;
                        border-left: white thin groove; padding-top: 2px; border-bottom: white thin groove"

                        valign
="middle"  align ="left"  width ="100%"  bgcolor ="#ba8439"  height ="22" >
                        
< font  face ="宋体"  color ="white"  size ="2" > 系统公告 </ font ></ td >
                
</ tr >
                
< tr >
                    
< td >
                    
</ td >
                
</ tr >
            
</ table >
        
</ div >
        
< div  align ="center" >
            
&nbsp; < asp:HyperLink  ID ="hlFistPage"  runat ="server"  ToolTip ="第一页" >< <</asp:HyperLink >
            
< asp:HyperLink  ID ="lnkPrev"  runat ="server"  ToolTip ="上一页" >< </asp:HyperLink > &nbsp;&nbsp;
            
&nbsp; < asp:Label  ID ="lblCurrentPage"  runat ="server"  Text ="Label" ></ asp:Label >
            
&nbsp;&nbsp;
            
< asp:HyperLink  ID ="lnkNext"  runat ="server"  ToolTip ="下一页" > > </ asp:HyperLink >
            
< asp:HyperLink  ID ="hlLastPage"  runat ="server"  ToolTip ="最后一页" > >> </ asp:HyperLink >< br  />
            
< br  />
        
</ div >
        
< asp:DataList  ID ="DataList1"  runat ="server" >
            
< ItemTemplate >
                
< p >
                    
< asp:Label  ID ="A_TimeLabel"  runat ="server"  Text ='<%#  Eval("A_Time", "{0:d}") % > '> </ asp:Label >< br  />
                    
< br  />
                    
< asp:Label  ID ="ContentsLabel"  runat ="server"  Text ='<%#  Eval("Contents") % > '> </ asp:Label ></ p >
                
< br  />
            
</ ItemTemplate >
            
< AlternatingItemStyle  BackColor ="White"   />
        
</ asp:DataList >
    
</ form >
</ body >
</ html >

程序代码:(这里假定已存在数据集bgsDataSet和表Affiche)

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;

public   partial   class  Home : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        bgsDataSet ds 
=   new  bgsDataSet();

        bgsDataSetTableAdapters.AfficheTableAdapter ta 
=
            
new  bgsDataSetTableAdapters.AfficheTableAdapter();

        ta.Fill(ds.Affiche);
        
        ds.Affiche.DefaultView.Sort 
=   " SN DESC " ;

        PagedDataSource pds 
=   new  PagedDataSource();

        pds.DataSource 
=  ds.Affiche.DefaultView;

        pds.AllowPaging 
=   true ;

        pds.PageSize 
=   5 ;

        
int  CurretPage;

        
if  (Request.QueryString[ " Page " !=   null )
            CurretPage 
=  Convert.ToInt32(Request.QueryString[ " Page " ]);
        
else
            CurretPage 
=   1 ;

        pds.CurrentPageIndex 
=  CurretPage  -   1 ;

        lblCurrentPage.Text 
=   " 第  "   +  CurretPage.ToString()  +   "  页 / 共  "   +
            pds.PageCount 
+   "  页 " ;

        
if  ( ! pds.IsFirstPage)
        {
            lnkPrev.NavigateUrl 
=  Request.CurrentExecutionFilePath  +   " ?Page= "   +
                Convert.ToString(CurretPage 
-   1 );
            hlFistPage.NavigateUrl 
=  Request.CurrentExecutionFilePath  +   " ?Page=1 " ;
        }
        
if  ( ! pds.IsLastPage)
        {
            lnkNext.NavigateUrl 
=  Request.CurrentExecutionFilePath  +   " ?Page= "   +
                Convert.ToString(CurretPage 
+   1 );
            hlLastPage.NavigateUrl 
=  Request.CurrentExecutionFilePath  +   " ?Page= "   +
                Convert.ToString(pds.PageCount);

        }


        DataList1.DataSource 
=  pds;
        DataList1.DataBind();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值