ASP.NET提供的Repeater模板并不带有分页功能,如果是少量数据的话利用Repeater模板来实现分页还是不错的,毕竟Repeater模板较为灵活。
WebForm1
编号
名称
描述
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HTMLControls;
namespace WebApplication1
{
/**////
/// WebForm1 的摘要说明。
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater Repeater1;
protected SqlConnection con = null;
protected string CON_STR="server=192.168.0.99;database=Northwind;user id=sa;password=jabby";
protected string SQL_QUERY="select * from Categories";
protected SqlDataAdapter da = null;
protected DataSet ds = null;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData(0,4);
}
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**////
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Repeater1.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.Repeater1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BindData(int start,int end)
{
try
{
con = new SqlConnection(CON_STR);
}
catch(SqlException sqlexp)
{
Response.Write("Connect to database Error! "+sqlexp.Message);
}
da = new SqlDataAdapter(SQL_QUERY,con);
ds = new DataSet();
try
{
da.Fill(ds,start,end,"Categories");
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
}
catch(Exception exp)
{
Response.Write(exp.Message);
}
finally
{
con.Close();
}
}
private void Repeater1_ItemCommand(object sender,System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if(e.CommandName.Equals("next"))
{
BindData(4,7);
}
else if(e.CommandName.Equals("previous"))
{
BindData(0,4);
}
}
}
}
此分页利用的是DataAdapter的Fill功能,但它提供的这种方式效率较低,因此只适用于少量数据,在大量数据情况下效率明显降低。 截图: 编号 名称 描述
1 Beverages Soft drinks, coffees, teas, beers, and ales
2 Condiments Sweet and savory sauces, relishes, spreads, and seasonings
3 Confections Desserts, candies, and sweet breads
4 Dairy Products Cheeses
编号 名称 描述
5 Grains/Cereals Breads, crackers, pasta, and cereal
6 Meat/Poultry Prepared meats
7 Produce Dried fruit and bean curd
8 Seafood Seaweed and fish

