ASP.NET中分页显示

分页详解,ASP.NET书本  “数据空间应用”

(1)使用GridView控件分页显示 步奏:1.添加GridView控件 2.将GrideView控件的AllowPaging属性设置为True,表示允许分页  3.将PageSize属性设置一个数字,用来控制每页中显示的记录数 4.最后在GridView控件的PageIndexChanging事件中设置GridView控件的PageIndex属性为当前页的索引值,并重新绑定GridView控件

(2)DataList控件中的数据

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();//数据绑定
        }
    }
    //进行数据绑定的方法
    public void Bind()
    {
        int CurrentPage = Convert.ToInt32(labNowPage.Text);  ///labNowPage标签  记录当前页码
        PagedDataSource ps = new PagedDataSource();//生成PagedDataSource的实例
        //实例化SqlConnection对象
        SqlConnection sqlCon = new SqlConnection();
        //实例化SqlConnection对象连接数据库的字符串
        sqlCon.ConnectionString = "server=.;uid=sa;pwd=;database=db_09";
        //定义SQL语句
        string SqlStr = "select * from tb_Student";
        //实例化SqlDataAdapter对象
        SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
        //实例化数据集DataSet
        DataSet ds = new DataSet();
        da.Fill(ds, "tb_Student");
        ps.DataSource = ds.Tables["tb_Student"].DefaultView;
        ps.AllowPaging = true; //是否可以分页
        ps.PageSize = 2; //显示的数量
        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;//不显示最后一页
        }
        this.labCount.Text = Convert.ToString(ps.PageCount);//记录总页数
        this.DataList1.DataSource = ps;
        this.DataList1.DataKeyField = "ID";
        this.DataList1.DataBind();
    }
    //首页
    protected void lnkbtnFirst_Click(object sender, EventArgs e)
    {
        this.labNowPage.Text = "1";
        this.Bind();
    }
    //上一页
    protected void lnkbtnFront_Click(object sender, EventArgs e)
    {
        this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) - 1);
        this.Bind();
    }
    //下一页
    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) + 1);
        this.Bind();
    }
    //尾页
    protected void lnkbtnLast_Click(object sender, EventArgs e)
    {
        this.labNowPage.Text = this.labCount.Text;
        this.Bind();
    }
}

ASP.NET中分页显示,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。