62,074
社区成员
发帖
与我相关
我的任务
分享
建三个Lable控件,ID分别为lb_page,lb_count,lb_currentpage,分别表示共有多少页,共有多少条记录,当前页是多少页
建四个LinkButton控件,ID分别为lbtn_frist,lbtn_up,lbtn_down,lbtn_last,分别表示首页,上一页,下一页,尾页
建一个DropDownList控件,表于选择要跳转到的页
当然肯定要建一个datalist控件,不用设置数据源
前台代码
<p> 共<asp:Label ID="lb_count" runat="server" Text="Label"></asp:Label>记录
共
<asp:Label ID="lb_page" runat="server" Text="Label"></asp:Label>
页 当前第
<asp:Label ID="lb_currentpage" runat="server" Text="1"></asp:Label>页
<asp:LinkButton ID="lbtn_frist" runat="server" OnClick="lbtn_frist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbtn_up" runat="server" OnClick="lbtn_up_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_down" runat="server" OnClick="lbtn_down_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_last" runat="server" OnClick="lbtn_last_Click">尾页</asp:LinkButton>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList></p>
后台代码
public partial class _Default : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
dlbind();
}
public string GetSub(string str, int length)
{
if (str.Length > length)
{
return str.Substring(0, length) + "。。。";
}
else
{
return str;
}
}
//
public void dlbind() //分页主函数
{
int curpage = Convert.ToInt32(lb_currentpage.Text);
DataTable dt=new DataTable();
DAL.ExecuteSql exec=new DAL.ExecuteSql();
exec.run(dt,"select *from myNews"); //在此设置SQL语句
PagedDataSource ps = new PagedDataSource();
ps.DataSource =dt.DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3; //设置每页显示条数
ps.CurrentPageIndex = curpage - 1;
lb_page.Text = Convert .ToString ( ps.PageCount);
lb_count.Text = ps.DataSourceCount.ToString();
if (!IsPostBack)
{
for (int i = 1; i <= ps.PageCount; i++)
{
DropDownList1.Items.Add(i.ToString());
}
DropDownList1.SelectedItem.Text = curpage.ToString();
}
lbtn_frist.Enabled = true;
lbtn_up.Enabled = true;
lbtn_down.Enabled = true;
lbtn_last.Enabled = true;
if (curpage == 1)
{
lbtn_frist.Enabled = false;
lbtn_up.Enabled = false;
}
if (curpage == ps.PageCount)
{
lbtn_down.Enabled = false;
lbtn_last.Enabled = false;
}
DataList1.DataSource = ps; //绑定数据源
DataList1.DataKeyField = "ArticleID";
DataList1.DataBind();
}
protected void lbtn_frist_Click(object sender, EventArgs e)
{
lb_currentpage.Text = "1";
dlbind();
}
protected void lbtn_up_Click(object sender, EventArgs e)
{
lb_currentpage.Text = Convert.ToString(Convert.ToInt32(lb_currentpage.Text) - 1);
dlbind();
}
protected void lbtn_down_Click(object sender, EventArgs e)
{
lb_currentpage.Text = Convert.ToString(Convert.ToInt32(lb_currentpage.Text) + 1);
dlbind();
}
protected void lbtn_last_Click(object sender, EventArgs e)
{
lb_currentpage.Text = lb_page.Text;
dlbind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int page = Convert.ToInt32(DropDownList1.SelectedItem.Value);
lb_currentpage.Text = page.ToString();
dlbind();
}
}
<% @ Page Language= "C# " %>
<% @ Import Namespace= "System.Data " %>
<% @ Import Namespace= "System.Data.OleDb " %>
<Script Language= "C# " Runat= "Server ">
/*
Create By 飞刀
http://www.aspcn.com
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)
{
CurrentPage = 0;
ViewState[ "PageIndex "] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
if((RecordCount%PageSize)> 0)
PageCount = RecordCount/PageSize+1;
else
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState[ "PageCount "] = PageCount;
ListBind();
}
}
//计算总共有多少条记录
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 <(PageCount-1)) CurrentPage++;
break;
case "prev ":
if(CurrentPage> 0) CurrentPage--;
break;
}
ViewState[ "PageIndex "] = CurrentPage;
ListBind();
}
[code=HTML]
<html>
<head>
<title> </title>
</head>
<body>
<form 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= "score " runat= "server "
HeaderStyle-BackColor= "#aaaadd "
AlternatingItemStyle-BackColor= "Gainsboro "
EditItemStyle-BackColor= "yellow "
>
<ItemTemplate>
姓名: <%# DataBinder.Eval(Container.DataItem, "Name ") %>
<asp:LinkButton id= "btnSelect " Text= "编辑 " CommandName= "edit " runat= "server " />
</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>
参考:http://topic.csdn.net/t/20030108/23/1340730.html
服务器端分页 - 存储过程
- 应用程序层
- LINQ
- 界面
客户端分页 - JS JQuery ...