ASP.NET中分页显示

时间:2014-05-11 17:35:08   收藏:0   阅读:346

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

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

(2)DataList控件中的数据

bubuko.com,布布扣
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();
    }
}
bubuko.com,布布扣

bubuko.com,布布扣

ASP.NET中分页显示,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!