【法一】2015.8.9html
aspx界面:後端
<body>spa
<asp:Repeater ID="Repeater1" runat="server">orm
<div>server
<HeaderTemplate>htm
<ul>ip
</HeaderTemplate>ci
<ItemTemplate>input
<li><%#Eval("Name")%>(<%#("Price")%>)</li>it
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</div>
後端代碼:
private MyDBDataContext_Context=new MyDBDataContext();
private const int PAGESIZE=3;
public List<Car>GetPagedCar(int pageNO)
{
var query=_Context.Car.Skip(PAGESIZE*(pageNO-1)).Take(PAGESIZE);
return query.ToList();
}
public int GetPageCount()
{
int rowsCount=_Context.Car.Count;//得到總行數
int pageCount=(int)Math.Ceiling(1.0*(rowsCount/PAGESIZE));//計算總頁數
return pageCount;
}
protected void Page_Load(object sender,EventArgs e)
{
int nowPage=1;//頁數從1開始
if(Request["pageno"]!=null)
{
nowPage=Convert.ToInt32(Request["pageno"]);
}
List<Car>list=GetPagedCar(nowPage);
---List<Car>list=GetPagedCar(1);顯示第一頁數據List<Car>list=GetPagedCar(2);顯示第二頁數據
//給Repeater數據
Repeater1.DataSourse=list;
Repeater1.DataBind();
//給上一頁、下一頁數據---加入超連接HyperLink實現翻頁功能
int pageCount=GetPageCount();
//控制下一頁連接
if(pageCount==nowPage)
{
linkNext.Enabled=false;
}
else
{
linkNext.Enabled=true;
linkNext.NavigateUrl="Default.aspx?pageno="+(nowPage+1).ToString();
}
//控制上一頁連接
if(nowPage==1)
{
linkPrev.Enabled=false;
}
else
{
linkPrev.Enabled=true;
linkPrev.NavigateUrl="Default.aspx?pageno="+(nowPage-1).ToString();---注意:pageno等號先後不能加空格
}
//控制首頁連接
linkFirst.NavigateUrl="Default.aspx?pageno=1"
//控制尾頁連接
linkLast.NavigateUrl="Default.aspx?pageno="+pageCount;
}
//跳轉到某一頁
protected btnGo_Click(object sender,EventArgs e)
{
int goNo=Convert.ToInt32(txtPageNo.Text);
if(goNo<1)
{
Response.Redirect("Default.aspx")
}
else if(goNo<GetPageCount())
{
Response.Redirect("Default.aspx?pageno="+GetPageCount());
}
else
{
Response.Redirect("Default.aspx?pageno="+goNo);
}
}
【法二】2015.8.18
part1---不加標註
aspx代碼:
<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>
VS代碼:
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();
}
if (_PageNo == GetPageCount())
{
lnkNext.HRef = "Default.aspx?pn=" + GetPageCount().ToString();
}
else
{
lnkNext.HRef = "Default.aspx?pn=" + (_PageNo + 1).ToString();
}
}
}
}
part2---加標註
aspx代碼:
<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>
一共<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>
VS代碼:
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();
}
}
2015.9.9
汽車表信息分頁
【Home控制器代碼】---添加引用using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
private const int PAGESIZE = 5;
public ActionResult Index(int? id)
{
if (id == null)
{
id = 1;
}
List<Car> list = new CarBF().Select(PAGESIZE,id.Value);//獲取當前頁數據(id表明當前頁頁號)
int pageCount = new CarBF().GetPageCount(PAGESIZE);//獲取總頁數
int nextPageNo = id.Value>pageCount?pageCount:id.Value + 1;//計算下一頁頁號
int prevPageNo = id.Value==1?1:id.Value - 1;//計算上一頁頁號
//使用ViewBag帶到視圖層去
ViewBag.NextPageNo = nextPageNo;
ViewBag.PrevPageNo = prevPageNo;
ViewBag.PageCount = pageCount;//總頁數
ViewBag.PageNo = id;//當前頁號
//下拉列表分頁---下拉列表按鈕代碼
List<int> listPage = new List<int>();
for (int i = 1; i <= pageCount; i++)
{
listPage.Add(i);
}
SelectList pageList = new SelectList(listPage,id);
ViewBag.PageList = pageList;
return View(list);
}
}
}
【CarBF代碼】
namespace MvcApplication1.Models
{
public class CarBF
{
private MyDBDataContext_Context=new MyDBDataContext();
//獲取總頁數
public int GetPageCount(int pageSize)
{
int rowsCount = _Context.Car.Count();
int pageCount = (int)Math.Ceiling(1.0*(rowsCount/pageSize));
return pageCount;
}
//獲取指定頁數據
public List<Car> Select(int pageSize,int pageNo)
{
var query=_Context.Car.Skip(pageSize*(pageNo-1)).Take(pageSize);
return query.ToList();
}
}
}
【Index視圖源】
@using MvcApplication1.Models;
@model List<Car>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Index</title>
</head>
<body>
<h1>分頁</h1>
<table border="0" width="100%" cellpadding="5" cellspacing="1" bgcolor="navy">
<tr style="color:white;text-align:center;font-weight:bold">
<td>代號</td>
<td>車型</td>
<td>系列</td>
<td>廠商</td>
<td>價格</td>
</tr>
@{
foreach(Car data in Model)
{
<tr bgcolor="white">
<td>@data.Code</td>
<td>@data.Name</td>
<td>@data.Brand1.Brand_Name</td>
<td>@data.Brand1.Productor.Prod_Name</td>
<td>@data.Price</td>
</tr>
}
}
</table>
@{
int nowPageNo = (int)ViewBag.PageNo;
int nextPageNo = (int)ViewBag.PageNo + 1;
if(nowPageNo==new CarBF().GetPageCount(5))
{
nextPageNo = nowPageNo;
}
int prevPageNo = (int)ViewBag.PageNo - 1;
if(nowPageNo==1)
{
prevPageNo = 1;
}
}
@Html.ActionLink("首頁","Index","Home","new{id=1}")
@Html.ActionLink("下一頁","Index","Home","{id=(int)ViewBag.NextPageNo}",null)
@Html.ActionLink("上一頁","Index","Home","{id=(int)ViewBag.PrevPageNo}",null)
@Html.ActionLink("尾頁","Index","Home","new{id=(int)ViewBag.PageCount")
<div style="display:inline-block">
@using(Html.BeginForm("Index","Home"))
{
@:轉第@Html.TextBox("id",null,new{size=2})頁<input type="submit" value="GO"/>---加GO轉頁按鈕
}
</div> 一共@ViewBag.PageCount頁,當前是第@ViewBag.PageNo頁---常見頁數顯示
</body>
【轉頁部分還可寫成】
<div style="display:inline-block">
@using(Html.BeginForm("Index","Home"))
{
@:轉第@Html.DropDownList("id",(SelectList)ViewBag.PageList)頁<input type="submit" value="GO"/>---加GO轉頁按鈕
}
</div>
或者
<div style="display:inline-block">
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new {id="form2"}))
{
@:轉第@Html.DropDownList("id",(SelectList)ViewBag.PageList,new{onchange="form2.submit()"})頁<input type="submit" value="GO"/>---加GO轉頁按鈕
}
</div>
或者
<div style="display:inline-block">@using (Html.BeginForm("Index", "Home", FormMethod.Post, new {id="form2"})){ @:轉第@Html.DropDownList("id",(SelectList)ViewBag.PageList,new{})頁<span onclick="form2.submit()"></span>}</div>