Web 上一页下一页 用超链接 用按钮

         

 

 

方法一超链接

 

 

Default.aspx.cs html代码*************************************************************************************

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><ul></HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li><%# Eval("Name") %></li>
</ItemTemplate>
</asp:Repeater>
</div>
<a href="Default.aspx" runat="server" id="lnkPrev">
<input id="Button1" type="button" value="上一页" /></a>
<a href="Default.aspx" runat="server" id="lnkNext">
<input id="Button2" type="button" value="下一页" /></a>
</form>
</body>
</html>
复制代码

 

C#代码*************************************************************************************

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
private MyDBDataContext _Context = new MyDBDataContext();
private const int PAGESIZE = 3;
private int _PageNo = 1; //当前的页号
//获取总页数
public int GetPageCount()
{
//取总行数
int rowsCount = _Context.Car.Count();
//算出总页数
int pageCount = (int)Math.Ceiling( 1.0*rowsCount / PAGESIZE);

return pageCount;
}
public List<Car> GetPagedCar()
{
var query = _Context.Car.Skip(PAGESIZE*(_PageNo-1)).Take(PAGESIZE);
return query.ToList();
}

protected void Page_Load(object sender, EventArgs e)//加载页面
{
if (!IsPostBack)
{
if (Request["pn"] != null)
{
_PageNo = Convert.ToInt32(Request["pn"]);
}
Repeater1.DataSource = GetPagedCar();
Repeater1.DataBind();

if (_PageNo == 1)//如果是第一页
{
lnkPrev.HRef = "Default.aspx?pn=1";//链接到第一页
}
else
{
lnkPrev.HRef = "Default.aspx?pn=" + (_PageNo - 1).ToString();//如果不是-1
}
if (_PageNo == GetPageCount())//下一页等于获得的页面
{
lnkNext.HRef = "Default.aspx?pn=" + GetPageCount().ToString();
}
else
{
lnkNext.HRef = "Default.aspx?pn=" + (_PageNo + 1).ToString();//否则+1
}

}
}
}

 

 
复制代码

 

 

 

 

 

Default2.aspx.cs html代码*************************************************************************************

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><ul></HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li><%# Eval("Name") %></li>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="下一页" />
<asp:DropDownList ID="PageList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="PageList_SelectedIndexChanged">
</asp:DropDownList>
&nbsp;一共<asp:Label ID="lblAll" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
页,当前是第<asp:Label ID="lblNow" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
页</form>
</body>
</html>
复制代码

 

C#代码*************************************************************************************

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
private MyDBDataContext _Context = new MyDBDataContext();
private const int PAGESIZE = 3;
private int _PageNo = 1; //当前的页号
//获取总页数
public int GetPageCount()
{
//取总行数
int rowsCount = _Context.Car.Count();
//算出总页数
int pageCount = (int)Math.Ceiling(1.0 * rowsCount / PAGESIZE);

return pageCount;
}
public List<Car> GetPagedCar()
{
_PageNo = Convert.ToInt32( PageList.SelectedValue);
var query = _Context.Car.Skip(PAGESIZE * (_PageNo - 1)).Take(PAGESIZE);
return query.ToList();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillPageList();
ShowCars();
}
}

private void ShowCars()
{
Repeater1.DataSource = GetPagedCar();
Repeater1.DataBind();

//给当前页和一共几页赋值
lblNow.Text = PageList.SelectedValue;
lblAll.Text = PageList.Items.Count.ToString();
}

private void FillPageList()
{
PageList.Items.Clear();

int pageCount = GetPageCount();
for (int i = 1; i <= pageCount; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
PageList.Items.Add(li);
}
}
protected void PageList_SelectedIndexChanged(object sender, EventArgs e)
{
ShowCars();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (PageList.SelectedIndex == 0)
{
return;
}
PageList.SelectedIndex--;
ShowCars();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (PageList.SelectedIndex == GetPageCount() - 1)
{
return;
}

PageList.SelectedIndex++;
ShowCars();
}
}

 
复制代码
posted @ 2015-08-18 11:09  tianqi123  阅读(617)  评论(0编辑  收藏  举报