我在网上找的代码,修改之后,有点错误,每页我想显示10条数据,但现在全都显示出来了,麻烦高手指点一下,谢谢!下面是代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44 Support .Net Framework Beta 2
*/
Service ser = new Service();
OleDbConnection MyConn;
int PageSize, RecordCount, PageCount, CurrentPage;
protected void Page_Load(object sender, EventArgs e)
{
//设定PageSize
PageSize = 10; //连接语句
//this.oC.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + HttpContext.Current.Request.PhysicalApplicationPath + "\\App_Data\\" +
// "Data.mdb\";Persist Security Info=True";
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + HttpContext.Current.Request.PhysicalApplicationPath + "\\App_Data\\" +
"Data.mdb\";Persist Security Info=True";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open(); //第一次请求执行 if (!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 新闻表_";
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 新闻表_";
DataSet1 ds = new DataSet1(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "新闻表_"); return ds.Tables["新闻表_"].DefaultView;
}
public void ListBind()
{
DataList1.DataSource = ser.Select("select * from 新闻表_ order by 更新时间 desc", "新闻表_").新闻表_.DefaultView;
DataList1.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 < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
} ViewState["PageIndex"] = CurrentPage; ListBind(); }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="Form2" runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:DataList
ID="DataList1" runat="server" Height="170px" Width="298px">
<ItemTemplate>
<table style="vertical-align: top; width: 472px; height: 28px;">
<tr>
<td colspan="1" style="width: 14px; height: 22px; text-align: left">
<asp:Image ID="Image3" runat="server" ImageUrl="~/Images/jiantou.gif" /></td>
<td colspan="3" style="width: 250px; height: 22px; text-align: left">
<asp:LinkButton ID="LinkButton9" runat="server" CommandName="lb新闻题目" Font-Names="Tahoma"
Font-Size="8pt" ForeColor="Black" Text='<%# bind("新闻题目") %>' Width="284px"></asp:LinkButton></td>
<td colspan="1" style="width: 131px; height: 22px; text-align: left">
<asp:Label ID="Label2" runat="server" Font-Names="Tahoma" Font-Size="8pt" Text='<%# bind("更新时间") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
</form>
</body>
</html>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44 Support .Net Framework Beta 2
*/
Service ser = new Service();
OleDbConnection MyConn;
int PageSize, RecordCount, PageCount, CurrentPage;
protected void Page_Load(object sender, EventArgs e)
{
//设定PageSize
PageSize = 10; //连接语句
//this.oC.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + HttpContext.Current.Request.PhysicalApplicationPath + "\\App_Data\\" +
// "Data.mdb\";Persist Security Info=True";
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + HttpContext.Current.Request.PhysicalApplicationPath + "\\App_Data\\" +
"Data.mdb\";Persist Security Info=True";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open(); //第一次请求执行 if (!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 新闻表_";
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 新闻表_";
DataSet1 ds = new DataSet1(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "新闻表_"); return ds.Tables["新闻表_"].DefaultView;
}
public void ListBind()
{
DataList1.DataSource = ser.Select("select * from 新闻表_ order by 更新时间 desc", "新闻表_").新闻表_.DefaultView;
DataList1.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 < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
} ViewState["PageIndex"] = CurrentPage; ListBind(); }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="Form2" runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:DataList
ID="DataList1" runat="server" Height="170px" Width="298px">
<ItemTemplate>
<table style="vertical-align: top; width: 472px; height: 28px;">
<tr>
<td colspan="1" style="width: 14px; height: 22px; text-align: left">
<asp:Image ID="Image3" runat="server" ImageUrl="~/Images/jiantou.gif" /></td>
<td colspan="3" style="width: 250px; height: 22px; text-align: left">
<asp:LinkButton ID="LinkButton9" runat="server" CommandName="lb新闻题目" Font-Names="Tahoma"
Font-Size="8pt" ForeColor="Black" Text='<%# bind("新闻题目") %>' Width="284px"></asp:LinkButton></td>
<td colspan="1" style="width: 131px; height: 22px; text-align: left">
<asp:Label ID="Label2" runat="server" Font-Names="Tahoma" Font-Size="8pt" Text='<%# bind("更新时间") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
</form>
</body>
</html>
不过,现在我都用aspnetpager了,免费的分页控件