黑马程序员_repeater分页实现

来源:互联网 发布:美国地缘政治知乎 编辑:程序博客网 时间:2024/06/11 19:05
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

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.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
         {  //如果是页面首次加载,执行此方法
            this.lblCurrentPage.Text = "1";
            dataBind();
         }
     }
    private void dataBind()
    {
       
        DataSet ds = getDataSet("select *from tb_test");
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 5;
        repeater1.DataSource = pds;
        pds.CurrentPageIndex=Convert.ToInt32(lblCurrentPage.Text)-1;//PagedDataSource的默认起始页是0,而设置的默认起始页为1;故而-1;
        lblCurrentPage.Text =(pds.CurrentPageIndex+1).ToString();//同理,当前页要加1
        lblTotal.Text = pds.PageCount.ToString();//获取总的页数
        this.pageNext.Enabled = true;
        this.pagePri.Enabled = true;
        if (pds.CurrentPageIndex < 1)
        {
            pagePri.Enabled = false;
        }
        if (pds.CurrentPageIndex== pds.PageCount - 1)
        {
            pageNext.Enabled = false;
        }
        repeater1.DataBind();
     }
    private SqlConnection getConnection()
    {
        SqlConnection con = new SqlConnection("server=.;database=db_test;uid=sa;pwd=;");
        return con;
    }
    private DataSet getDataSet(string str)
    {
        DataSet dt = new DataSet();
        try
        {
            using (SqlConnection con = getConnection())
            {
                con.Open();
                using (SqlDataAdapter sda = new SqlDataAdapter(str, con))
                {
                    sda.Fill(dt);
                    con.Close();
                    con.Dispose();
                    return dt;
                }
            }
        }
        catch (Exception ee)
        {
            throw new Exception(ee.ToString());
        }
    }
    private int exeCommmand(string str)
    {
        try
        {
            using (SqlConnection con = getConnection())
            {
                con.Open();
                using (SqlCommand cmd= new SqlCommand(str, con))
                {
                    int k = cmd.ExecuteNonQuery();
                    con.Close();
                    con.Dispose();
                    return k;
                }
            }
        }
        catch (Exception ee)
        {
            throw new Exception(ee.ToString());
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
       int k=exeCommmand("insert into tb_test(UserName)values('"+txtUserName.Text+"')");
       dataBind();
    }
    protected void pagePri_Click(object sender, EventArgs e)
    {
        this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(lblCurrentPage.Text) - 1);
        dataBind();
    }
    protected void pageNext_Click(object sender, EventArgs e)
    {
        this.lblCurrentPage.Text = Convert.ToString(Convert.ToInt32(lblCurrentPage.Text)+1);
        dataBind();
    }
}

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------详细请查看:http://net.itheima.com/
原创粉丝点击