repeater分页显示数据

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: |||超大  

表名:ChinaStates

控件:Repeater

查询代码DA:

public class ChinaStatesDA

{

PRivate DataClassesDataContext Context; // 构建LINQ

public ChinaStatesDA()

{

Context = new DataClassesDataContext();

}

public List<ChinaStates> Select(int nowye,int numbers)

// 输入当前页,页面数据条数,查询数据库信息

{

return Context.ChinaStates.Skip((nowye-1)*numbers).Take(numbers).ToList();

// .Skip 是跳过多少条数据查询 .Take查询前几条数据

}

public int Select() // 查询数据库有多少条数据

{

return Context.ChinaStates.Count();

}

}

Cs代码、;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e) // 运行界面

{

if (!IsPostBack)

{

bindchinadata(1,5); // 绑定数据①

int count = new ChinaStatesDA().Select(); //获取数据库多少条数据

yeshu = (int)(Math.Ceiling(count / 5.0));

// 计算数据库一页5条数据,能显示多少页

}

}

private static int yeshu; //构建总页数类型 static 表示一直有这个值。

private void bindchinadata(int nowye,int numbers) //绑定数据函数①

nowye现在第几页,numbers每页显示数据的个数

{

Repeater1.DataSource = new ChinaStatesDA().Select(nowye,numbers);

Repeater1.DataBind();

TextBox1.Text = nowye.ToString();

// 调用DA中的查询函数 select 绑定到Repeater 中

// TextBox1.Text 当前的页数

}

protected void Button3_Click(object sender, EventArgs e) // 下一页

{

int nowye = int.Parse(TextBox1.Text); // 构建当前页面nowye 并赋值

if (yeshu!= nowye)

// 判断 当前页面 nowye 是否等于总页数 yeshu , 不等于 从新绑定Repeater

{

bindchinadata(nowye + 1, 5);

}

}

protected void Button2_Click(object sender, EventArgs e) // 上一页

{

int nowye = int.Parse(TextBox1.Text); // 构建当前页面nowye 并赋值

if (nowye != 1)

// 判断 当前页面 nowye 是否等于第一页 , 不等于 从新绑定Repeater

{

bindchinadata(nowye - 1, 5);

}

}

protected void Button5_Click(object sender, EventArgs e) // 跳转页面

{

int nowye = int.Parse(TextBox1.Text); // 构建当前页面nowye 并赋值

if(nowye>0 && nowye<=yeshu) //判断大于0小于总页数从新绑定Repeater

{

bindchinadata(nowye, 5);

}

}

protected void Button1_Click(object sender, EventArgs e) //首页

{

bindchinadata(1, 5);

}

protected void Button4_Click(object sender, EventArgs e) //末页

{

bindchinadata(yeshu, 5);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航