我是一粒细沙

每天进步一点点

使用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();
    }
}
阅读更多
版权声明:走别人没做过的路,让别人有路可走。 https://blog.csdn.net/gz153016/article/details/49894649
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭