ASP.NET分页方法的了解程度
【出现频率】
★★★★☆
【解答】
ASP.NET分页的常用方法有以下几种。
q 启用部分数据控件的内置分页功能,如设置GridView控件的“AllowPaging”属性为“true”,类似的数据控件还有DetailsView等。
q 通过SQL查询语句,以提取指定部分的数据的方式完成分页的功能。也包括调用数据库中内置的存储过程完成分页数据的提取。
q 调用所对应数据适配器对象(DataAdapter对象)的Fill方法时,传递分页参数以完成数据集的分页功能。
q 通过访问DataTable对象的“Rows”属性,以循环输出的方式访问结果集中指定区段的数据行,以达到分页的效果。
【分析】
分页功能在大部分WEB项目中使用比较广泛,本题考查面试者对ASP.NET中可用分页方法的了解程度。在页面布局等方面要求不高的情况下,例如WEB后台管理系统,使用数据控件内置的分页功能通常是编程者的首选。为了展示常用的这四种分页方法,笔者制作一个实例,在同一个页面中分别用这四种方法对相同的结果集分页显示。在VS 2008中添加新的WEB窗体到NetWeb3项目,并命名为Pager.aspx。在页面中添加1个GridView控件,用于展示内置分页功能,添加2个Repeater控件,用于展示SQL语句分页和数据适配器对象分页。最后通过在Pager.aspx页面导入System.Data命名空间,在页面中直接用for循环输出dt数据表的指定数量记录,以完成分页显示的效果。编写Pager.aspx如代码12.12所示。
代码12.12 多种方法分页显示页面:Pager.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>四种分页方法实例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>控件内置分页【每页3条记录】</h3>
<asp:GridView runat="server" ID="GView" AllowPaging="true" PageSize="3"></asp:GridView>
<h3>数据适配器分页【当前显示第1页,每页2条记录】</h3>
<asp:Repeater runat="server" ID="Rp1" >
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li><asp:Label runat="server" ID="UName" Text='<%# Eval("UserName") %>'></asp:Label>:<asp:Label runat="server" ID="Msg" Text='<%# Eval("Message") %>'></asp:Label></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
<h3>SQL分页【当前显示第2页,每页3条记录】</h3>
<asp:Repeater runat="server" ID="Rp2" >
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li><asp:Label runat="server" ID="UName" Text='<%# Eval("UserName") %>'></asp:Label>:<asp:Label runat="server" ID="Msg" Text='<%# Eval("Message") %>'></asp:Label></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
<h3>数据表分页【当前显示第1页,每页3条记录】</h3>
<%
Response.Write("<ul>");
int i;
string UName;
string Msg;
for(i=0;i<3;i++)
{
UName = dt.Rows[i][0].ToString();
Msg = dt.Rows[i][1].ToString();
Response.Write(String.Format("<li>{0}:{1}</li>",UName,Msg));
}
Response.Write("</ul>");
%>
</div>
</form>
</body>
</html>
编写Pager.aspx.cs如代码12.13所示。
代码12.13 多种方法分页逻辑代码:Pager.aspx.cs
using System;
………………………………
//导入必要的命名空间,使用SQL SERVER数据提供者
using System.Data.SqlClient;
namespace NetWeb3
{
public partial class Pager : System.Web.UI.Page
{
//从Web.config的AppSettings节点的第1个子节点中获取数据库连接字符串
//将连接字符串对象引用赋值给静态字符串变量CnStr
static string CnStr = ConfigurationManager.AppSettings[0];
//根据CnStr变量创建SqlConnection对象,引用为cn
SqlConnection cn = new SqlConnection(CnStr);
//声明SqlDataAdapter类型变量da
SqlDataAdapter da;
//创建DataSet对象,引用为ds
DataSet ds = new DataSet();
//声明DataTable类型的变量dt
//protected修饰符可以保证在*.aspx中可以访问dt
protected DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
this.GView.PageIndexChanging += new GridViewPageEventHandler(GView_PageIndexChanging);
//判断页面是否为首次加载
if (!IsPostBack)
{
//调用自定义的MyBind方法
MyBind();
}
}
protected void MyBind()
{
string SqlStr = "SELECT [UserName],[Message] FROM [LeaveMsg]";
//根据SqlStr和cn创建新的SqlDataAdapter对象,引用为da变量
da = new SqlDataAdapter(SqlStr, cn);
//调用da的Fill方法,将记录填充到ds,其DataTable名称为LeaveMsg1
da.Fill(ds, "LeaveMsg1");
//将LeaveMsg1数据表设置为GView控件的数据源
this.GView.DataSource = ds.Tables["LeaveMsg1"];
//调用da的Fill方法,将记录填充到ds,其DataTable名称为LeaveMsg2
//Fill方法接收的第2个参数代表从第几条记录开始填充,第3个参数代表填充多少条记录
da.Fill(ds,0,2, "LeaveMsg2");
//将LeaveMsg1数据表设置为Rp1控件的数据源
this.Rp1.DataSource = ds.Tables["LeaveMsg2"].DefaultView;
cn.Open();
//编写SQL语句,直接在数据库查询时分页获取数据
SqlStr = "Select Top 3 [UserName],[Message] from [LeaveMsg] where [id] Not IN (Select Top (3*1) [id] from [LeaveMsg])";
//根据SqlStr和cn创建新的SqlCommand对象,引用为cmd变量
SqlCommand cmd = new SqlCommand(SqlStr, cn);
//调用cmd的ExecuteReader方法,返回数据读取器,引用为dr
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//将dr设置为Rp2控件的数据源
this.Rp2.DataSource = dr;
//将LeaveMsg1数据表对象引用赋值给dt,供*.aspx中访问
dt = ds.Tables["LeaveMsg1"];
//绑定页面所有控件的数据
this.DataBind();
//关闭数据读取器,同时关闭数据库连接
dr.Close();
}
//该方法为GView控件的数据页索引变化事件的处理方法
protected void GView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//设置新的页索引
GView.PageIndex = e.NewPageIndex;
//再次调用MyBind方法,绑定页面数据
MyBind();
}
}
}
打开IE浏览器,在浏览器地址栏输入“http://localhost/Pager.aspx”,页面运行效果如图12.5所示。
图12.5 多种分页方法的展示