All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
当ASP.NET撞上JSF之构建应用程序的异同_[Asp.Net教程]
用户需要怎样的网站设计_研究探讨_seo学堂
关于discuz论坛附件不能上传解决办法_php资料_编程技术
C#2.0基本语法二_[Asp.Net教程]
SQL实现其他进制到十进制的转换_[SQL Server教程]
ASP.NET技巧:DataGridView,的分页处理_.net资料_编程技术
ASP.NET实例:Asp.net,FMS,开发视频网站_[Asp.Net教程]
利用单元测试在每个层上对PHP代码进行检查_php资料_编程技术
Asp.net中多彩下拉框的实现_[Asp.Net教程]
图片、文件防盗链程序_.net资料_编程技术
Reading number is top 10 pictures
云南大理的美女
银行20年后可能消失
泳装名模阿尔贝特妮写真2
mythology hero1
2012 national geographic daily picture5
Chinese paper-cut grilles art appreciation7
The money of more than 100 countries and regions17
A man's favorite things1
Ashlynn Brooke photograph of a group3
Earthquake hedge common sense
Download software ranking
天龙八部十二宫服务端
WebService在.NET中的实战应用教学视频 → 第1集
matrix3
Boxer's Top ten classic battle9
实战黑客不求人
JSP+Ajax Web development typical examples
Tram sex maniac 2 (H) rar bag13
1400篇各类破解文章
Red cliff
jdk1.6 for windows
delv published in(发表于) 2014/1/24 9:02:04 Edit(编辑)
.NET2.0DataList分页_[Asp.Net教程]

.NET2.0DataList分页_[Asp.Net教程]

.NET2.0DataList分页_[Asp.Net教程]

今天做了一个简单的在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;
}

}
///


/// 向后
///

///
///
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);
}
}
///
/// 返回记录的总数目
///

/// 记录的总条数
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;
}
///
/// 向前
///

///
///
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 );
}
}
///
/// 页面刚加载时产生绑定视图
///

///
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;
}
///
/// 翻页的时候产生绑定视图
///

/// 该也要加载的首记录的索引
/// 页面的记录大小
///
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;
}
///
/// 绑定datalist
///

/// 数据源
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" %>





无标题页







<%#DataBinder.Eval(Container .DataItem ,"regionid") %>

<%#DataBinder.Eval(Container .DataItem ,"regiondescription" )%>



onClick="Button2_Click" Text="Prev" />
onClick="Button1_Click" Text="Next" />



如果SQL语句换成存储过程,也很容易实现。

来源:sdtsfhh的blog







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.