<div> <span style="font-size: 10pt"> 共有<asp:Label ID="lblRecordCount" runat="server" ForeColor="red"></asp:Label>条记录 当前为<asp:Label ID="lblCurrentPage" runat="server" ForeColor="Red"></asp:Label>/<asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>页 <asp:DropDownList ID="Ddl_PageNumber" runat="server" AutoPostBack="true" CssClass="lanyu"> </asp:DropDownList></span> <td style="width: 100px; height: 21px; text-align: right;"> <asp:linkbutton id="BtnFirst" runat="server" Text="首页" OnCommand="Page_OnClick" CommandName="First" Font-Size="10pt">首页</asp:linkbutton> <asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick" Text="上一页" Font-Size="10pt"></asp:LinkButton> <asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick" Text="下一页" Font-Size="10pt"></asp:LinkButton> <asp:linkbutton id="BtnLast" OnCommand="Page_OnClick" CommandName="Last" text="尾页" Runat="server" Font-Size="10pt">尾页</asp:linkbutton></div>
.aspx.cs
string constr = (ConfigurationManager.ConnectionStrings["pconn"]).ToString(); int PageSize, RecordCount, PageCount, CurrentPage, i; ArrayList Al_PageNum;
public void Page_Load(Object src, EventArgs e) { PageSize = 4; //设定PageSize SqlConnection MyConn = new SqlConnection(constr); MyConn.Open(); if (!Page.IsPostBack) //第一次请求执行 { RecordCount = CalculateRecord(); //计算总共有多少记录/
PageCount = RecordCount / PageSize; //计算总共有多少页 if (RecordCount % PageSize > 0) //取整 PageCount = PageCount + 1; lblPageCount.Text = PageCount.ToString(); lblRecordCount.Text = RecordCount.ToString(); ViewState["PageCount"] = PageCount; CurrentPage = 0; ViewState["PageIndex"] = 0;
Al_PageNum = new ArrayList();//绑定DROPDOWNLIST for (i = 1; i <= PageCount; i++) //从1开始循环,为了不出现0页码 Al_PageNum.Add(i.ToString()); Ddl_PageNumber.DataSource = Al_PageNum; Ddl_PageNumber.DataBind();
ListBind(); //绑定 } MyConn.Close(); }
public int CalculateRecord() //计算总共有多少条记录 { SqlConnection MyConn = new SqlConnection(constr); int intCount; string strCount = "select count(*) as co from v1"; SqlCommand MyComm = new SqlCommand(strCount, MyConn); MyConn.Open(); SqlDataReader dr = MyComm.ExecuteReader(); if (dr.Read()) { intCount = Int32.Parse(dr["co"].ToString()); } else { intCount = 0; } dr.Close(); MyConn.Close(); return intCount; }
ICollection CreateSource() { SqlConnection MyConn = new SqlConnection(constr);
int StartIndex; //设定导入的起终地址 StartIndex = CurrentPage * PageSize; //计算记录数的起始点 string strSel = "select * from v1 order by proid desc"; DataSet ds = new DataSet(); SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn); MyAdapter.Fill(ds, StartIndex, PageSize, "v1"); return ds.Tables["v1"].DefaultView; }
public void ListBind() { MyList.DataSource = CreateSource(); MyList.DataBind(); lbnNextPage.Enabled = true; lbnPrevPage.Enabled = true; BtnFirst.Enabled = true; BtnLast.Enabled = true; if (PageCount == 0) { lblCurrentPage.Text = "0"; lbnNextPage.Enabled = false; lbnPrevPage.Enabled = false; BtnFirst.Enabled = false; BtnLast.Enabled = false; } else { if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false; if (CurrentPage == 0) lbnPrevPage.Enabled = false; lblCurrentPage.Text = (CurrentPage + 1).ToString(); } Ddl_PageNumber.Text = lblCurrentPage.Text; }
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 "Last": CurrentPage = (PageCount - 1); break; default: CurrentPage = 0; break; }
ViewState["PageIndex"] = CurrentPage; ListBind(); } public void PageNum_SelectIndexChanged(object sender, System.EventArgs e) { ViewState["PageIndex"] = int.Parse(Ddl_PageNumber.SelectedItem.Value) - 1;//保持不出现0页码 PageSize = 4; CurrentPage = (int)ViewState["PageIndex"]; PageCount = (int)ViewState["PageCount"]; ListBind(); //MyList.DataSource = CreateSource(); //MyList.DataBind(); }
override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); }
private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); this.Ddl_PageNumber.SelectedIndexChanged += new System.EventHandler(this.PageNum_SelectIndexChanged); }
转载于:https://www.cnblogs.com/neoe1984/archive/2007/06/22/793365.html
相关资源:asp简易分页效果源代码