请教如何在分页中加入分页跳转
请问怎么在分页中加入分页跳转
现在实现了分页功能.但是想做一个分页下拉框.选择直接跳转到某一页.请问怎么实现.
这是我的代码.cs文件
前台页面文件代码:
现在实现了分页功能.但是想做一个分页下拉框.选择直接跳转到某一页.请问怎么实现.
这是我的代码.cs文件
- C# code
OleDbConnection MyConn; public int comid; public int PageSize, RecordCount, PageCount, CurrentPage; public void Page_Load(Object src, EventArgs e) { //设定PageSize PageSize = 20; //连接语句 string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=True;Data Source="+Server.MapPath("book.mdb"); MyConn = new OleDbConnection(MyConnString); MyConn.Open(); //第一次请求执行 if (!Page.IsPostBack) { ListBind(); CurrentPage = 0; ViewState["PageIndex"] = 0; //计算总共有多少记录 RecordCount = CalculateRecord(); lblRecordCount.Text = RecordCount.ToString(); // lblRecordCount2.Text = RecordCount.ToString(); //计算总共有多少页 PageCount = RecordCount / PageSize; lblPageCount.Text = PageCount.ToString(); //lblPageCount2.Text = PageCount.ToString(); ViewState["PageCount"] = PageCount; } } //计算总共有多少条记录 public int CalculateRecord() { int intCount; string strCount = "select count(id) as co from content"; OleDbCommand MyComm = new OleDbCommand(strCount, MyConn); OleDbDataReader dr = MyComm.ExecuteReader(); if (dr.Read()) { //intCount = Int32.Parse(dr["co"].ToString()); intCount = (int)dr["co"]; } else { intCount = 0; } dr.Close(); return intCount; } ICollection CreateSource() { int StartIndex; //设定导入的起终地址 StartIndex = CurrentPage * PageSize; string strSel = "select * from Content"; DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn); MyAdapter.Fill(ds, StartIndex, PageSize, "Content"); return ds.Tables["Content"].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(); //lblCurrentPage2.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; case "sy": CurrentPage=0; break; case "wy": CurrentPage=PageCount-1; break; } ViewState["PageIndex"] = CurrentPage; ListBind(); MyConn.Close(); }
前台页面文件代码:
- HTML code
<form id="Form1" 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" Width="80%"> <HeaderTemplate><table width="100%"><tr><td>帐号</td><td>公司名称</td><td>注册资金</td><td>经营模式</td><td>操作</td></tr></HeaderTemplate> <ItemTemplate> <tr><td><%# DataBinder.Eval(Container.DataItem, "帐号")%></td><td><%# DataBinder.Eval(Container.DataItem, "公司名称")%></td><td><%# DataBinder.Eval(Container.DataItem, "注册资本")%></td><td><%# DataBinder.Eval(Container.DataItem, "经营模式")%></td><td><asp:HyperLink runat="server" NavigateUrl='<%# "CompanyDetail.aspx?comid=" + DataBinder.Eval(Container.DataItem, "id") %>' Target="blank">编辑</asp:HyperLink></td></tr> </ItemTemplate> <FooterTemplate></table></FooterTemplate> </asp:DataList> 共<%=this.PageCount%>页 <asp:LinkButton id="LinkButton1" Text="首页" CommandName="sy" OnCommand="Page_OnClick" runat="server" /> <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" /> <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /> <asp:LinkButton id="LinkButton2" Text="最后一页" CommandName="wy" OnCommand="Page_OnClick" runat="server" /> <asp:Label ID="Label1" runat="server" Text="跳转"></asp:Label> 共有<asp:Label id="lblRecordCount2" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage2" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount2" ForeColor="red" runat="server" />页 </form>