使用Repeater控件实现数据绑定

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
public partial class Repeater_1 : System.Web.UI.Page
{
   // MySqlConnection mysqlconnection = Conn.createCon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.Label1.Text = "1";
            databindToRepeater();
        }
        //data_Repeater();
    }


    private void databindToRepeater()
    {
        int curPage = Convert.ToInt32(this.Label1.Text);


        MySqlConnection mysqlconnection = Conn.createCon();
        MySqlDataAdapter dataAdapter2 = new MySqlDataAdapter("select * from bookinfo", mysqlconnection);
        DataSet ds = new DataSet();
        dataAdapter2.Fill(ds, "emp");


        
        //分页
        System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
        ps.DataSource = ds.Tables["emp"].DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 3;
        ps.CurrentPageIndex = curPage - 1;


        this.button1.Enabled = true;
        this.button2.Enabled = true;


        if (curPage == 1)
            this.button1.Enabled = false;


        if (curPage == ps.PageCount)
            this.button2.Enabled = false;


        this.Repeater1.DataSource = ds.Tables["emp"];


        this.Repeater1.DataSource = ps;
        this.Repeater1.DataBind();
    }


    
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {


    }
    protected void button1_Click(object sender, EventArgs e)
    {
        this.Label1.Text = Convert.ToString(Convert.ToInt32(this.Label1.Text)-1);
        databindToRepeater();
    }
    protected void button2_Click(object sender, EventArgs e)
    {
        this.Label1.Text = Convert.ToString(Convert.ToInt32(this.Label1.Text) +1);
        databindToRepeater();
    }
}

你可能感兴趣的:(使用Repeater控件实现数据绑定)