datalist控件实现分页功能

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

DataList控件实现分页功能...

DataList控件其实功能也很强大,不过最令人头疼的就是它不像GridView控件一样内置了分页的功能,这么好的一个控件竟然不能分页,不过,只是DataList没有提供内置的分页功能,但是并不表示,我们不能使用DataList控件来实现分页,既然它不给我分页功能,那只好自己动手了。下面是全部原代码,这里用的是DataAdapter与DataSet 组合。

<% @ Page Language="C#" %>

<% @ Import Namespace="System.Data" %>

<% @ Import Namespace="System.Data.OleDb" %> <Script Language="C#" Runat="Server"> 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 = mandName;

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

switch(cmd)

{

case "next":

if(CurrentPage<(PageCount-1)) CurrentPage++;

break;

case "prev":

if(CurrentPage>0) CurrentPage--;

break;

}

ViewState["PageIndex"] = CurrentPage;

ListBind();

}

</script>

<html>

<head>

<title></title>

</head>

<body>

<form runat="server">

共有<asp:Label id="lblRecordCount" ForeColor="red"

相关文档
最新文档