翻页

来源:互联网 发布:傅园慧走红网络 编辑:程序博客网 时间:2024/04/30 04:29
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Label1.Text = "1";
            Session.Add("currentpage", 1);
            DataListBind();
        }
        if (Session["currentpage"].ToString() != "")
        {
            DataListBind();
        }
    }
    protected void DataListBind()
    {
        string str = "select * from tb_goods";
        ConnHelper.Sqlconn.connOpen();
        SqlDataAdapter dr=new SqlDataAdapter(str,ConnHelper.Sqlconn.conn);
        DataTable dt=new DataTable();
        dr.Fill(dt);

        PagedDataSource ps = new PagedDataSource();
        ps.AllowPaging = true;
        ps.PageSize =8;
        ps.DataSource = dt.DefaultView;
        ps.CurrentPageIndex = Convert.ToInt32(Session["currentpage"]) - 1;
        if (Convert.ToInt32(Session["currentpage"]) == 1)
        {
            this.Button1.Enabled = false;
        }
        else
        {
            this.Button1.Enabled = true;
        }
        if (Convert.ToInt32(Session["currentpage"]) == ps.PageCount)
        {
            this.Button2.Enabled = false;
        }
        else
        {
            this.Button2.Enabled = true;
        }
        this.DataList1.DataSource = ps;
        this.DataList1.DataBind();
        ConnHelper.Sqlconn.connClose();
    }
    /// <summary>
    /// 上页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["currentpage"] = Convert.ToInt32(Session["currentpage"]) - 1;
        this.Label1.Text = Session["currentpage"].ToString();//显示第几页
        this.DataListBind();
    }
    /// <summary>
    /// 下页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["currentpage"] = Convert.ToInt32(Session["currentpage"]) + 1;
        this.Label1.Text = Session["currentpage"].ToString();
        this.DataListBind();
    }
}
原创粉丝点击