·您的位置: 首页 » 资源教程 » 编程开发 » ASP » REPEATER显示数据的例子,带分页

REPEATER显示数据的例子,带分页

类别: ASP教程  评论数:0 总得分:0
下面是个Repeater显示数据分页的例子
我在做时候遇到的问题给大家提一下刚开始我使用DataReader做数据源,是不行,后才换DataSet
在设置分页的时候一定要搞清楚当前页,如果你把当前页设置成1,那么他就从第二页显示数据了,因为它的当前页是从0开始的,而我们显示当前页时候是从1开始的
在比较是否达到总页数之前一定要先也给PS设置数据源,我就是先用ps.CurrentPageIndex==ps.PageCount-1检查是否在最后一页,然后再ps.DataSource=ds.Tables["authors"].DefaultView;设置数据源的,结果分页一直有问题.
呵呵,这几个问题搞了我半天。可能是我太菜了吧,学.NET还没多久呢。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace test
{
/// <summary>
/// Repeater 的摘要说明。
/// </summary>
public class Repeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label lbCurPage;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Repeater Repeater1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.lbCurPage.Text="1";
this.BindToRepeater();
}
}
private void BindToRepeater()
{
SqlConnection con=new SqlConnection("data source=.;database=pubs;uid=sa;pwd=123a456b");
// SqlCommand cmd=new SqlCommand("select * from authors",con);
// con.Open();
// SqlDataReader dr;
// dr=cmd.ExecuteReader();
SqlDataAdapter da=new SqlDataAdapter("select * from authors",con);
DataSet ds=new DataSet();
da.Fill(ds,"authors");
PagedDataSource ps=new PagedDataSource();
ps.AllowPaging=true;
ps.PageSize=5;
this.Repeater1.DataSource=ps;
ps.CurrentPageIndex=Convert.ToInt32(this.lbCurPage.Text)-1;
ps.DataSource=ds.Tables["authors"].DefaultView;
this.Button1.Enabled=true;
this.Button2.Enabled=true;
if(ps.CurrentPageIndex==0)
{
this.Button1.Enabled=false;
}
if(ps.CurrentPageIndex==ps.PageCount-1)
{
this.Button2.Enabled=false;
}
this.Repeater1.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
this.lbCurPage.Text=Convert.ToString(Convert.ToInt32(this.lbCurPage.Text)-1);
this.BindToRepeater();
}

private void Button2_Click(object sender, System.EventArgs e)
{
this.lbCurPage.Text=Convert.ToString(Convert.ToInt32(this.lbCurPage.Text)+1);
this.BindToRepeater();
}
}
}
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1