`
wsql
  • 浏览: 11757699 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

.NET2.0DataList分页

 
阅读更多

今天做了一个简单的在DataList中分页的小东西.用到的核心的东西就是PageDataSource类.和repeater的分页思想是一样的。

代码如下:

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;
using System.Data.SqlClient;
public partial class DataListPage : System.Web.UI.Page
{
private const int PAGESIZE = 3;//声明每一页包含的记录数
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataListBind(this.getDataView());
int currentPageIndex = 1;//指定当前页
ViewState["currentPageIndex"] = currentPageIndex;
}

}
/// <summary>
/// 向后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//首先判断显示的记录数是否已经超过了所有的记录数
if ((int)ViewState["currentPageIndex"] * PAGESIZE < this.CaculateRecordCount())
{
int recordIndex = ((int)ViewState["currentPageIndex"]) * PAGESIZE;//计算当前页要显示的记录的索引
this.DataListBind(this.getDataView(recordIndex, PAGESIZE));//绑定到DATALIST
ViewState["currentPageIndex"] = (int)ViewState["currentPageIndex"] + 1;//当前页加1
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "kk", "alert('到底最后一页')", true);
}
}
/// <summary>
/// 返回记录的总数目
/// </summary>
/// <returns>记录的总条数</returns>
private int CaculateRecordCount()
{
SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa");
conn.Open();
//计算所有的记录数
SqlDataAdapter da = new SqlDataAdapter("select count(*) as rc from region", conn);
int rc = (int)da.SelectCommand.ExecuteScalar();
conn.Close();
return rc;
}
/// <summary>
/// 向前
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
if ((int)ViewState["currentPageIndex"] > 1)
{
int recordIndex = ((int)ViewState["currentPageIndex"] - 2) * PAGESIZE;//计算当前也要显示的首记录的索引
this.DataListBind(this.getDataView(recordIndex, PAGESIZE));//绑定
ViewState["currentPageIndex"] = (int)ViewState["currentPageIndex"] - 1;//当前页码减1
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "kk", "alert('到达第一页!')",true );
}
}
/// <summary>
/// 页面刚加载时产生绑定视图
/// </summary>
/// <returns></returns>
private DataView getDataView()
{
SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa");
SqlDataAdapter da = new SqlDataAdapter("select * from region", conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0].DefaultView;
}
/// <summary>
/// 翻页的时候产生绑定视图
/// </summary>
/// <param name="recordIndex">该也要加载的首记录的索引</param>
/// <param name="pageSize">页面的记录大小</param>
/// <returns></returns>
private DataView getDataView(int recordIndex, int pageSize)
{
SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa");
SqlDataAdapter da = new SqlDataAdapter("select * from region", conn);
DataSet ds = new DataSet();
da.Fill(ds, recordIndex, pageSize, "tt");//将首记录索引为recordIndex开始的pagesize条记录填充到数据集
return ds.Tables["tt"].DefaultView;
}
/// <summary>
/// 绑定datalist
/// </summary>
/// <param name="dv">数据源</param>
private void DataListBind(DataView dv)
{
PagedDataSource pds = new PagedDataSource();
pds.PageSize = PAGESIZE;
pds.AllowPaging = true;
pds.DataSource = dv;
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
}
}

前台:

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

<!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:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"><%#DataBinder.Eval(Container .DataItem ,"regionid") %></asp:Label><br />
<asp:Label ID="Label2" runat="server"><%#DataBinder.Eval(Container .DataItem ,"regiondescription" )%></asp:Label>
</ItemTemplate>
</asp:DataList></div>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Prev" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Next" />
</form>
</body>
</html>
如果SQL语句换成存储过程,也很容易实现。

分享到:
评论

相关推荐

    在ASP.NET 2.0中操作数据:DataList和Repeater数据分页

    在ASP.NET 2.0中操作数据:DataList和Repeater数据分页

    ASP.NET2.0中datalist仿百度分页

    ASP.NET2.0中datalist仿百度分页ASP.NET2.0中datalist仿百度分页

    ASP.NET 2.0数据教程

    ASP.NET 2.0数据教程DataList和Repeater数据分页等功能

    在ASP.NET 2.0中操作数据:DataList和Repeater数据分页(源码)

    在ASP.NET 2.0中操作数据:DataList和Repeater数据分页(源码)

    零基础学ASP.NET 2.0电子书&源代码绝对完整版1

    示例描述:本章演示ASP.NET 2.0网站的预编译以及学习ASP.NET 2.0的前置知识。 WebSite文件夹 创建的ASP.NET 2.0 Web站点。 www文件夹 第一个用C#开发的Web应用程序。 bianyi.bat 编译网站的批处理文件。 ...

    asp.net2.0简单实用 magicajax+aspnetpager 打造datalist无刷新分页源码,新手学习必备

    asp.net2.0简单实用 magicajax+aspnetpager 打造datalist无刷新分页源码,新手学习必备 数据库使用sqlserver2000默认的northwind,使用前先配置web.config数据库连接路径,然后导入两个存储过程(存储过程.txt)

    asp.net2.0

    DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 ...

    零基础学ASP.NET 2.0&源代码绝对完整版1

    示例描述:本章演示ASP.NET 2.0网站的预编译以及学习ASP.NET 2.0的前置知识。 WebSite文件夹 创建的ASP.NET 2.0 Web站点。 www文件夹 第一个用C#开发的Web应用程序。 bianyi.bat 编译网站的批处理文件。 form...

    asp.net2.0数据教程

    DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 ...

    ASP.NET2.0数据教程

    DataList和Repeater的分页和排序 DataList和Repeater数据分页 DataList和Repeater数据排序(一) DataList和Repeater数据排序(二) DataList和Repeater数据排序(三) DataList和Repeater的自定义按钮行为 ...

    《零基础学ASP.NET 2.0》第15章 数据绑定控件显示数据

    15.1.2 数据排序并进行分页 267 15.1.3 数据编辑和删除 269 15.2 DetailsView控件 272 15.2.1 将数据绑定到DetailsView控件 272 15.2.2 对数据进行编辑和分页 273 15.2.3 数据的插入和删除 275 15.3 FormView控件 ...

    在ASP.NET 2.0中操作数据之四十二:DataList和Repeater数据排序(一)

     DataList和Repeater数据分页里我们学习了如何在DataList里添加分页功能。我们在ProductsBLL类里创建了一个名为GetProductsAsPagedDataSource的方法,它返回一个PagedDataSource对象。当绑定到DataList或Repeater时...

    .NET 通用分页控件

    在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net 1.1)和GridView(asp.net 2.0)控件,但其分页功能并不尽如人意,如可定制性差、无法通过Url实现分页功能等,而且有时候我们需要对DataList和Repeater甚至...

    在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据

    导言  在之前的28篇教程的例子里,... 为了在显示多条记录时,有更好的自定义功能,ASP.NET 2.0提供了DataList 和Repeater (ASP.NET 1.x版本里也有 ).DataList 和Repeater 使用模板来显示内容,而不是象在GridView里那样

    asp.net知识库

    体验.net2.0的优雅(四):Provider、策略、控制反转和依赖注入 泛型最佳实践 asp.net 2.0下嵌套masterpage页的可视化编辑 C# 2.0与泛型 动态调用对象的属性和方法——性能和灵活性兼备的方法 泛型技巧系列:用泛型...

    在ASP.NET 2.0中操作数据之四十四:DataList和Repeater数据排序(三)

    第七步: 在自定义分页的Repeater 里添加排序功能  现在已经完成了自定义分页,我们再来添加排序功能。ProductsBLL类的GetProductsPagedAndSorted方法和GetProductsPaged一样有startRowIndex 和 maximumRows 参数,...

    在ASP.NET 2.0中操作数据之四十一:DataList和Repeater数据分页

    导言  分页和排序是显示数据时经常用到的功能。比如,在一个在线书店里搜索关于ASP.NET 的书的时候,可能结果会是... 不幸的是,DataList 和Repeater 都没有提供内置的分页和排序功能。本章我们将学习如何在DataLis

    Asp.net分页控件AspNetPager7.2

    在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net 1.1)和GridView(asp.net 2.0)控件,但其分页功能并不尽如人意,如可定制性差、无法通过Url实现分页功能等,而且有时候我们需要对DataList和Repeater甚至...

    AspNetPager自动分页源码

    在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net 1.1)和GridView(asp.net 2.0)控件,但其分页功能并不尽如人意,如可定制性差、无法通过Url实现分页功能等,而且有时候我们需要对DataList和Repeater甚至...

Global site tag (gtag.js) - Google Analytics