怎样使用DataList 进行分页?

2024-12-02 18:04:47
推荐回答(1个)
回答1:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DataLi
{
///


/// WebForm1 的摘要说明。
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
FillData();
}
}

static PagedDataSource Pds;
void FillData()
{
SqlConnection Conn = new SqlConnection("Data Source=.;Initial catalog=northwind;integrated security=true;");
Conn.Open();
SqlDataAdapter Sda = new SqlDataAdapter("select * from Employees", Conn);
DataSet Ds = new DataSet();
Sda.Fill(Ds);
Pds = new PagedDataSource();
Pds.DataSource = Ds.Tables[0].DefaultView;
Pds.AllowPaging = true;
Pds.PageSize = 4;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
Conn.Close();
}
//下一页
protected void LinkButton2_Click(object sender, EventArgs e)
{
Pds.CurrentPageIndex++;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
}
//上一页
protected void LinkButton1_Click(object sender, EventArgs e)
{
Pds.CurrentPageIndex--;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.LinkButton2.Click += new System.EventHandler(this.LinkButton2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}