读书笔记:《Aspx开发200问》——如何实现Repeater控件的分页

由于Repeater控件没有分页相关的属性,要使用System.Web.UI.WebControl中的PageDataSource类。

PageDataSource封装了DataGrid控件的分页属性

 PagedDataSource 类的部分公共属性:
 AllowCustomPaging  获取或设置指示是否启用自定义分页的值。
 AllowPaging   获取或设置指示是否启用分页的值。
 Count    获取要从数据源使用的项数。
 CurrentPageIndex   获取或设置当前页的索引。
 DataSource   获取或设置数据源。
 DataSourceCount   获取数据源中的项数。
 FirstIndexInPage   获取页中的第一个索引。
 IsCustomPagingEnabled  获取一个值,该值指示是否启用自定义分页。
 IsFirstPage   获取一个值,该值指示当前页是否是首页。
 IsLastPage   获取一个值,该值指示当前页是否是最后一页。
 IsPagingEnabled   获取一个值,该值指示是否启用分页。
 IsReadOnly   获取一个值,该值指示数据源是否是只读的。
 IsSynchronized   获取一个值,该值指示是否同步对数据源的访问(线程安全)。
 PageCount   获取显示数据源中的所有项所需要的总页数。
 PageSize   获取或设置要在单页上显示的项数。
 VirtualCount   获取或设置在使用自定义分页时数据源中的实际项数。

页面代码:

 

<%@ Page language="c#" Codebehind="RepeaterPager.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.RepeaterCommand" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>RepeaterCommand</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<h2>Repeater控件分页例子</h2>
            
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 48px" cellSpacing="1"
                cellPadding
="1" width="300" border="0">
                
<TR>
                    
<TD align="right">
                        
<asp:label id="lblCurPage" runat="server"></asp:label>
                        
<asp:HyperLink id="lnkPrev" runat="server">上一页</asp:HyperLink>
                        
<asp:HyperLink id="lnkNext" runat="server">下一页</asp:HyperLink></TD>
                
</TR>
                
<TR>
                    
<TD style="HEIGHT: 29px">
                        
<HR style="WIDTH: 400px; HEIGHT: 1px" color="red" SIZE="1">
                        
&nbsp;
                        
<asp:Repeater id="RepeaterPage" runat="server">
                            
<ItemTemplate>
                                
<li>
                                    
<%#DataBinder.Eval(Container.DataItem,"TitleOfCourtesy")%>
                                    
<%#DataBinder.Eval(Container.DataItem,"LastName")%>
                                    
<%#DataBinder.Eval(Container.DataItem,"FirstName")%>
                                
</li>
                            
</ItemTemplate>
                        
</asp:Repeater></TD>
                
</TR>
            
</TABLE>
        
</form>
    
</body>
</HTML>

 

后台代码:

 

private void Page_Load(object sender, System.EventArgs e)
        {
            
//页面初试化时进行数据绑定
            if(!IsPostBack)
                RepeaterDataBind();
        }
        
private void RepeaterDataBind()
        {
            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
            
//创建数据适配器对象
            SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName,TitleOfCourtesy from Employees",conn);
            
//创建DataSet对象
            DataSet ds = new DataSet();        
            
try
            {
                
//填充数据集
                da.Fill(ds,"testTable");
                
//创建分页类
                PagedDataSource objPage = new PagedDataSource();
                
//设置数据源
                objPage.DataSource = ds.Tables["testTable"].DefaultView;
                
//允许分页
                objPage.AllowPaging = true;
                
//设置每页显示的项数
                objPage.PageSize = 5;
                
//定义变量用来保存当前页索引
                int CurPage;
                
//判断是否具有页面跳转的请求
                if (Request.QueryString["Page"!= null)
                    CurPage
=Convert.ToInt32(Request.QueryString["Page"]);
                
else
                    CurPage
=1;
                
//设置当前页的索引
                objPage.CurrentPageIndex = CurPage-1;
                
//显示状态信息
                lblCurPage.Text = "当前页:第" + CurPage.ToString()+"";
                
//如果当前页面不是首页
                if (!objPage.IsFirstPage)
                    
//定义"上一页"超级链接的URL为:当前执行页面的虚拟路径,并传递下一页面的索引值
                    lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
                
//如果当前页面不是最后一页
                if (!objPage.IsLastPage)
                    
//定义"下一页"超级链接的URL为:当前执行页面的虚拟路径,并传递下一页面的索引值
                    lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);

                
//进行数据绑定
                RepeaterPage.DataSource = objPage;
                RepeaterPage.DataBind();
            }
            
catch(Exception error)
            {
                Response.Write(error.ToString());
            }        
        }
posted @ 2009-07-23 11:14  宿远  阅读(460)  评论(0编辑  收藏  举报