分页控件
Posted 哈喽!树先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分页控件相关的知识,希望对你有一定的参考价值。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageApart.ascx.cs" Inherits="WebApplication2.PageApart" %> <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">首页</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">上一页</asp:LinkButton> <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">下一页</asp:LinkButton> <asp:LinkButton ID="LinkButton4" runat="server" onclick="LinkButton4_Click">尾页</asp:LinkButton> <asp:Label ID="Label1" runat="server" Text="页码提示"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" Width="28px">1</asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Go" />
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; namespace WebApplication2 { public partial class PageApart : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //第一次加载 //显示默认页面的数据 GoPage(); } } int pageSize = 10; //页码大小 public int PageSize { get { return pageSize; } set { pageSize = value; } } string table = ""; //要分页的表名 public string Table { get { return table; } set { table = value; } } string primaryKey = ""; //表中主键名 public string PrimaryKey { get { return primaryKey; } set { primaryKey = value; } } string fileds = "*";//stuid,sex,age,name //要查询的字段 public string Fileds { get { return fileds; } set { fileds = value; } } //当前显示的页码 public int PageIndex { get { return Convert.ToInt32( TextBox1.Text); } set { TextBox1.Text=value.ToString(); } } #region 分页数据的绑定控件 GridView gv; public GridView Gv { get { return gv; } set { gv = value; } } DataList dl; public DataList Dl { get { return dl; } set { dl = value; } } Repeater rp; public Repeater Rp { get { return rp; } set { rp = value; } } #endregion //分页的sql,30~40 string sql = "select top {1} {4} from {0} where {3} not in (select top {2} {3} from {0})"; string connStr = System.Configuration.ConfigurationManager.AppSettings["connStr"]; public int GetCount(string str)//获取总记录数 { SqlConnection conn = new SqlConnection(connStr); conn.Open(); SqlCommand cmd = new SqlCommand(str,conn); object obj = cmd.ExecuteScalar(); conn.Close(); return Convert.ToInt32(obj); } public DataTable GetTable(string str)//得到列表 { SqlConnection conn = new SqlConnection(connStr); conn.Open(); SqlCommand cmd = new SqlCommand(str, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); conn.Close(); return dt; } //显示页码对应的数据 public void GoPage() { //1.查询总记录数 string sqlCount = "select count(1) from " + table; int count = GetCount(sqlCount); int pageCount = (count % pageSize == 0) ? (count / pageSize) : (count / pageSize + 1); ViewState["pageCount"] = pageCount;//把总页数存放到viewState中 //检测要显示的页面是不是合理 if (PageIndex > pageCount) { PageIndex = pageCount; } if (PageIndex < 1) { PageIndex = 1; } Label1.Text = "共" + pageCount + "页/总记录:" + count; //2.显示数据 sql = string.Format(sql, table, pageSize, (PageIndex-1) * pageSize, primaryKey, fileds); DataTable dt = GetTable(sql); if (gv!=null) { gv.DataSource = dt; gv.DataBind(); } if (dl!=null) { dl.DataSource = dt; dl.DataBind(); } if (rp!=null) { rp.DataSource = dt; rp.DataBind(); } } //首页 protected void LinkButton1_Click(object sender, EventArgs e) { PageIndex = 1; GoPage(); } //上一页 protected void LinkButton2_Click(object sender, EventArgs e) { PageIndex--; GoPage(); } //下一页 protected void LinkButton3_Click(object sender, EventArgs e) { PageIndex++; GoPage(); } //尾页,最后一页 protected void LinkButton4_Click(object sender, EventArgs e) { PageIndex = Convert.ToInt32(ViewState["pageCount"]); GoPage(); } //跳转到某页 protected void Button1_Click(object sender, EventArgs e) { PageIndex = Convert.ToInt32(TextBox1.Text); GoPage(); } } }
以上是关于分页控件的主要内容,如果未能解决你的问题,请参考以下文章