datalist java,DataList分页

/*  CreateBy飞刀  http://www.aspcn.C#

/*

Create By 飞刀

2001-7-25 01:44

Support .Net Framework Beta 2

*/

OleDbConnection MyConn;

int PageSize,RecordCount,PageCount,CurrentPage;

public void Page_Load(Object src,EventArgs e)

{

//设定PageSize

PageSize = 10;

//连接语句

string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..//DataBase//db1.mdb;";

MyConn = new OleDbConnection(MyConnString);

MyConn.Open();

//第一次请求执行

if(!Page.IsPostBack)

{

ListBind();

CurrentPage = 0;

ViewState["PageIndex"] = 0;

//计算总共有多少记录

RecordCount = CalculateRecord();

lblRecordCount.Text = RecordCount.ToString();

//计算总共有多少页

PageCount = RecordCount/PageSize;

lblPageCount.Text = PageCount.ToString();

ViewState["PageCount"] = PageCount;

}

}

//计算总共有多少条记录

public int CalculateRecord()

{

int intCount;

string strCount = "select count(*) as co from Score";

OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);

OleDbDataReader dr = MyComm.ExecuteReader();

if(dr.Read())

{

intCount = Int32.Parse(dr["co"].ToString());

}

else

{

intCount = 0;

}

dr.Close();

return intCount;

}

ICollection CreateSource()

{

int StartIndex;

//设定导入的起终地址

StartIndex = CurrentPage*PageSize;

string strSel = "select * from Score";

DataSet ds = new DataSet();

OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);

MyAdapter.Fill(ds,StartIndex,PageSize,"Score");

return ds.Tables["Score"].DefaultView;

}

public void ListBind()

{

score.DataSource = CreateSource();

score.DataBind();

lbnNextPage.Enabled = true;

lbnPrevPage.Enabled = true;

if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;

if(CurrentPage==0) lbnPrevPage.Enabled = false;

lblCurrentPage.Text = (CurrentPage+1).ToString();

}

public void Page_OnClick(Object sender,CommandEventArgs e)

{

CurrentPage = (int)ViewState["PageIndex"];

PageCount = (int)ViewState["PageCount"];

string cmd = e.CommandName;

//判断cmd,以判定翻页方向

switch(cmd)

{

case "next":

if(CurrentPage

break;

case "prev":

if(CurrentPage>0) CurrentPage--;

break;

}

ViewState["PageIndex"] = CurrentPage;

ListBind();

}

共有条记录

当前为/页

HeaderStyle-BackColor="#aaaadd"

AlternatingItemStyle-BackColor="Gainsboro"

EditItemStyle-BackColor="yellow"

>

姓名:

用viewstate传递分页的信息,最重要的就是这一句   MyAdapter.Fill(ds,StartIndex,PageSize,"Score");

================================================================================

DataList分页2

Repeater和DataList控件提供了一个快速、灵活的表现数据的方式,但是,它们没有内建的分页功能;DataGrid控件提供了内建的分页功能,但它的结构比较复杂。下面就用PagedDataSource类实现Repeater和DataList的分页。 PagedDataSource封装了DataGrid的分页属性,我们可以象DataGrid那样进行分页。代码如下:

public void Page_Load(Object src,EventArgs e) {

OleDbConnection objConn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +

Server.MapPath("../aspxWeb.mdb"));

OleDbDataAdapter objCommand=new OleDbDataAdapter("select * from Document",objConn);

DataSet ds=new DataSet();

objCommand.Fill(ds);

PagedDataSource objPds = new PagedDataSource();

objPds.DataSource = ds.Tables[0].DefaultView;

objPds.AllowPaging = true;

objPds.PageSize = 5;

int CurPage;

if (Request.QueryString["Page"] != null)

CurPage=Convert.ToInt32(Request.QueryString["Page"]);

else

CurPage=1;

objPds.CurrentPageIndex = CurPage-1;

lblCurrentPage.Text = "当前页:" + CurPage.ToString();

if (!objPds.IsFirstPage)

lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);

if (!objPds.IsLastPage)

lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);

Repeater1.DataSource=objPds;

Repeater1.DataBind();

}

Repeater控件分页的例子

P,TD,DIV,SPAN {font-size:9pt}

 上一页

下一页 


本文由来源 21aspnet,由 system_mush 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值