2013-12-19
展开全部
我给你个连接access的 命名空间using System.Data.OleDb;创建4个linkbutton:id和对应的显示文本(text)分别为 lnkbtnFirst<< lnkbtnFront< lnkbtnNext> lnkbtnLast>>
在创建4个 lable:意思已经加入到句子中 自己看哈。共 labcount 页 当前页labnowpage(text=1) lblRecordCount1 隐藏 lblPageCount1 隐藏 下面的代码 是写在public partial class _Default : System.Web.UI.Page里面的 ~~ 不是pageload里 需要修改的地方是所有连接数据库的地方,。 库,表,甚至字段, 然后该pagesize是每页的大小。最后记得把4个按键<< < > >> 也可以改成相应的首页 上页什么 看个人爱好 把这2个按钮的事件绑定到对应的事件 调试·~ ok·~~ int PageSize, RecordCount, PageCount, CurrentPage;
OleDbConnection MyConn;
public void Bind()
{
CurrentPage = Convert.ToInt32(labnowpage.Text);
PagedDataSource ps = new PagedDataSource();
string ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"~/App_Data/豆豆.mdb");
OleDbConnection conn = new OleDbConnection(ConnStr);
conn.Open();
OleDbDataAdapter dr = new OleDbDataAdapter("SELECT * FROM 留言板", conn);
DataSet ds = new DataSet();
dr.Fill(ds, "留言板");
ps.DataSource = ds.Tables["留言板"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 4;
ps.CurrentPageIndex = CurrentPage - 1;
lnkbtnFront.Enabled = true;
lnkbtnFirst.Enabled = true;
lnkbtnNext.Enabled = true;
lnkbtnLast.Enabled = true;
if (CurrentPage == 1)
{
lnkbtnFirst.Enabled = false;//不显示第一页
lnkbtnFront.Enabled = false;//不显示上一页
}
if (CurrentPage == ps.PageCount)
{
lnkbtnNext.Enabled = false;//不显示下一页
lnkbtnLast.Enabled = false;//不显示最后一页 }
labcount.Text = Convert.ToString(ps.PageCount);
DataList1.DataSource = ps;
DataList1.DataBind();
}
public void LoadData()
{
string ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"~/App_Data/豆豆.mdb");
OleDbConnection conn = new OleDbConnection(ConnStr);
conn.Open();
OleDbDataAdapter dr = new OleDbDataAdapter("SELECT * FROM 留言板", conn);
DataSet ds = new DataSet();
dr.Fill(ds,"留言板");
DataList1.DataSource = ds;
DataList1.DataBind();
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
PageSize = 10; // 根据实际定义常量
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(".") + "..\\App_Data\\豆豆.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
if(!Page.IsPostBack)
{
Bind();
// ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0; //计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount1.Text = RecordCount.ToString(); //计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount1.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
} } //计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select * from 留言板";
OleDbCommand MyComm = new OleDbCommand(strCount, MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["编号"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
} //生成数据
public ICollection CreateSource()
{ int StartIndex; //设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from 留言板";
DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "留言板"); return ds.Tables["留言板"].DefaultView;
}
//public void ListBind()
//{
// DataList1.DataSource = CreateSource();
// DataList1.DataBind(); // lbnNextPage1.Enabled = true;
// lbnPrevPage1.Enabled = true;
// if (CurrentPage == (PageCount - 1)) lbnNextPage1.Enabled = false;
// if (CurrentPage == 0) lbnPrevPage1.Enabled = false;
// lblCurrentPage1.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();
} protected void lnkbtnFirst_Click(object sender, EventArgs e)
{
labnowpage.Text = "1";
Bind();
}
protected void lnkbtnFront_Click(object sender, EventArgs e)
{
labnowpage.Text = Convert.ToString(Convert.ToInt32(labnowpage.Text) - 1);
Bind();
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
labnowpage.Text = Convert.ToString(Convert.ToInt32(labnowpage.Text) + 1);
Bind();
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
labnowpage.Text = labcount.Text;
Bind();
}
在创建4个 lable:意思已经加入到句子中 自己看哈。共 labcount 页 当前页labnowpage(text=1) lblRecordCount1 隐藏 lblPageCount1 隐藏 下面的代码 是写在public partial class _Default : System.Web.UI.Page里面的 ~~ 不是pageload里 需要修改的地方是所有连接数据库的地方,。 库,表,甚至字段, 然后该pagesize是每页的大小。最后记得把4个按键<< < > >> 也可以改成相应的首页 上页什么 看个人爱好 把这2个按钮的事件绑定到对应的事件 调试·~ ok·~~ int PageSize, RecordCount, PageCount, CurrentPage;
OleDbConnection MyConn;
public void Bind()
{
CurrentPage = Convert.ToInt32(labnowpage.Text);
PagedDataSource ps = new PagedDataSource();
string ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"~/App_Data/豆豆.mdb");
OleDbConnection conn = new OleDbConnection(ConnStr);
conn.Open();
OleDbDataAdapter dr = new OleDbDataAdapter("SELECT * FROM 留言板", conn);
DataSet ds = new DataSet();
dr.Fill(ds, "留言板");
ps.DataSource = ds.Tables["留言板"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 4;
ps.CurrentPageIndex = CurrentPage - 1;
lnkbtnFront.Enabled = true;
lnkbtnFirst.Enabled = true;
lnkbtnNext.Enabled = true;
lnkbtnLast.Enabled = true;
if (CurrentPage == 1)
{
lnkbtnFirst.Enabled = false;//不显示第一页
lnkbtnFront.Enabled = false;//不显示上一页
}
if (CurrentPage == ps.PageCount)
{
lnkbtnNext.Enabled = false;//不显示下一页
lnkbtnLast.Enabled = false;//不显示最后一页 }
labcount.Text = Convert.ToString(ps.PageCount);
DataList1.DataSource = ps;
DataList1.DataBind();
}
public void LoadData()
{
string ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"~/App_Data/豆豆.mdb");
OleDbConnection conn = new OleDbConnection(ConnStr);
conn.Open();
OleDbDataAdapter dr = new OleDbDataAdapter("SELECT * FROM 留言板", conn);
DataSet ds = new DataSet();
dr.Fill(ds,"留言板");
DataList1.DataSource = ds;
DataList1.DataBind();
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
PageSize = 10; // 根据实际定义常量
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(".") + "..\\App_Data\\豆豆.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
if(!Page.IsPostBack)
{
Bind();
// ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0; //计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount1.Text = RecordCount.ToString(); //计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount1.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
} } //计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select * from 留言板";
OleDbCommand MyComm = new OleDbCommand(strCount, MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["编号"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
} //生成数据
public ICollection CreateSource()
{ int StartIndex; //设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from 留言板";
DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "留言板"); return ds.Tables["留言板"].DefaultView;
}
//public void ListBind()
//{
// DataList1.DataSource = CreateSource();
// DataList1.DataBind(); // lbnNextPage1.Enabled = true;
// lbnPrevPage1.Enabled = true;
// if (CurrentPage == (PageCount - 1)) lbnNextPage1.Enabled = false;
// if (CurrentPage == 0) lbnPrevPage1.Enabled = false;
// lblCurrentPage1.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();
} protected void lnkbtnFirst_Click(object sender, EventArgs e)
{
labnowpage.Text = "1";
Bind();
}
protected void lnkbtnFront_Click(object sender, EventArgs e)
{
labnowpage.Text = Convert.ToString(Convert.ToInt32(labnowpage.Text) - 1);
Bind();
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
labnowpage.Text = Convert.ToString(Convert.ToInt32(labnowpage.Text) + 1);
Bind();
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
labnowpage.Text = labcount.Text;
Bind();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-19
展开全部
一般分页都用的第三方控件,像aspnetpager,我用的就是这个,很好用的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-19
展开全部
千万级分页存储过程结合Repeater+Aspnetpager7.2实现 http://www.cnblogs.com/it560/archive/2009/11/30/1613410.html 代码很全,我自己写的,这里不方便贴代码,楼主就到我博客看吧!我测试结果 ,2千万条数据,每页读20条记录,平均时间不到2秒!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询