假分页:从数据库一次性取出所有数据绑定到控件上,再将所有数据根据每页显示记录条数进行分页。
使用GridView控件来达到分页的功能:
<span style="font-size:18px;"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="牛腩新闻发布系统.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> </title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridViewText" runat="server" AllowPaging="True"
onpageindexchanging="GridViewText_PageIndexChanging" PageSize="10">
</asp:GridView>
</form>
</body>
</html>
</span>
数据绑定:
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BLL;
using DAL;
namespace 牛腩新闻发布系统
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindNews();
}
}
private void BindNews()
{
GridViewText.DataSource = new B_Page().SelectAll();
GridViewText.DataBind();
}
Protected void GridViewText_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewText.PageIndex = e.NewPageIndex;
BindNews();
}
}
}
</span>
设置一下GridView的一些属性:
真分页:在执行查询操作的时候,只查询当前页面的要显示的内容。每次跳转页面也都要查询数据库取对应数据。
真分页需要使用ASPNETPAGERT控件。
<span style="font-size:18px;"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="牛腩新闻发布系统.test" %>
<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<webdiyer:AspNetPager ID="anp" runat="server" onpagechanged="anp_PageChanged"
PageSize="10">
</webdiyer:AspNetPager>
</form>
</body>
</html>
</span>
显示记录数:
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BLL;
using DAL;
namespace 牛腩新闻发布系统
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
anp.RecordCount = 56;
}
}
protected void anp_PageChanged(object sender, EventArgs e)
{
Response.Write("开始记录数:" + anp.StartRecordIndex + " <br>结束记录数:" + anp.EndRecordIndex);
}
}
}
</span>
假分页其实就是从数据库中选择所有的记录后再进行分页,它要把所有信息都调出来后才进行分页。
真分页是只从数据中选择当前页的记录,它只要那一页的记录,其他的记录不调用,查询速度更加快速,适合数据量较大时候使用。