王朝网络
分享
 
 
 

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

王朝other·作者佚名  2006-11-24
宽屏版  字体: |||超大  

下面是个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();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有