实现数据表内的数据分页操作:
网页源数据:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Car fenye.aspx.cs" Inherits="Car__fenye" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
#idd {
font-weight:bold;
background-color:navy;
color:black;
text-align:center;
}
.cla {
font-weight:bold;
background-color:white;
color:green;
text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table width="100%" border="1" cellpadding="5" cellspacing="1" id="idd">
<tr>
<td width="5%">代号</td>
<td width="20%">名称</td>
<td width="10%">型号</td>
<td width="25%">时间</td>
<td width="10%">油耗</td>
<td width="10%">功率</td>
<td width="10%">排量</td>
<td width="10%">单价</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="cla">
<td width="5%"><%# Eval("Code") %></td>
<td width="20%"><%# Eval("Name") %></td>
<td width="10%"><%# Eval("Branda") %></td>
<td width="25%"><%# Eval("Timea") %></td>
<td width="10%"><%# Eval("Oila") %></td>
<td width="10%"><%# Eval("Powersa") %></td>
<td width="10%"><%# Eval("Exa") %></td>
<td width="10%"><%# Eval("Pricea") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<asp:Button ID="Showye" runat="server" OnClick="Showye_Click" Text="首页" />
<asp:Button ID="Shangye" runat="server" OnClick="Shangye_Click1" Text="上一页" />
<asp:Button ID="Nextye" runat="server" OnClick="Nextye_Click1" Text="下一页" />
<asp:Button ID="Weiye" runat="server" OnClick="Weiye_Click" Text="尾页" />
第<asp:DropDownList ID="ddlye" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlye_SelectedIndexChanged">
</asp:DropDownList>
页 一共<asp:Label ID="zongye" runat="server" ForeColor="Red"></asp:Label>
页,现在在<asp:Label ID="xianye" runat="server" ForeColor="Red"></asp:Label>
页;<br />
<br />
AutoPostBack----当选定内容更改后,自动回发到服务器</form>
</body>
</html>
cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Car__fenye : System.Web.UI.Page
{
private const int pagesize=3;//每页有多少条数据
private CARSDataContext context = new CARSDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Showcar();
Fillye();
YShu();
}
}
//private void Showcar()
//{
// var qu = context.Car;
// Repeater1.DataSource = qu;
// Repeater1.DataBind();
//}
public void Fillye()
{
int Tiaoshu=context.Car.Count();//数据库表中所有的数据的条数
int yeshu=(int)Math.Ceiling(1.0*Tiaoshu/pagesize);
//按照pagesize的大小,来进行分页,一页有pagesize条数据,分了yeshu个页
for (int i = 0; i < yeshu; i++)
{
ListItem li = new ListItem((i + 1).ToString(), i.ToString());
ddlye.Items.Add(li);
}//往dropdownlist中静态添加数据
}
public void YShu()
{
int dye = Convert.ToInt32(ddlye.SelectedValue);//所在的页数-1
int shutiao = dye * pagesize;//前面的页数一共有多少条数据
var qu = context.Car.Skip(shutiao).Take(pagesize);//分页,skip取出前面的所有条数,显示这一页拥有的条数
Repeater1.DataSource = qu;
Repeater1.DataBind();
zongye.Text = ddlye.Items.Count.ToString();//一共多少页
xianye.Text = (ddlye.SelectedIndex+1).ToString();//现在在第几页
//Response.Write(dye);
//int aaa = ddlye.SelectedIndex;
//Response.Write(aaa);
}
protected void ddlye_SelectedIndexChanged(object sender, EventArgs e)
{
YShu();
}
protected void Showye_Click(object sender, EventArgs e)
{
ddlye.SelectedIndex = 0;
YShu();
}
protected void Weiye_Click(object sender, EventArgs e)
{
ddlye.SelectedIndex = ddlye.Items.Count - 1;
YShu();
}
protected void Nextye_Click1(object sender, EventArgs e)
{
if (ddlye.SelectedIndex < ddlye.Items.Count - 1)
{
ddlye.SelectedIndex++;
YShu();
}
}
protected void Shangye_Click1(object sender, EventArgs e)
{
if (ddlye.SelectedIndex > 0)
{
ddlye.SelectedIndex--;
YShu();
}
}
}
//select top 9 Code from Car;
//select top 3 * from Car where Code not in(select top 9 Code from Car);
//select ceiling(1.0*COUNT(*)/3) from Car;