这里我们把前面的代码重构为用户控件的方式来使用,提高代码的利用率。
用户控件PagerControl.ascx的页面的设计图如下:
用户控件PagerControl.ascx的页面代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PagerControl.ascx.cs" Inherits="_Controls_PagerControl" %>
<div class="contentstyle" style="text-align:center;margin:20px 0 20px 0;">
<asp:Label ID="lblMessage" runat="server" Text="msg" Height="25px" Font-Size="15" ></asp:Label>
<asp:Button ID="btnFirstPage" runat="server" Text="首页" Width="48px"
Height="25px" onclick="btnFirstPage_Click" />
<asp:Button ID="btnPreviousPage" runat="server" Text="上一页" Width="48px"
Height="25px" onclick="btnPreviousPage_Click" />
<asp:Button ID="btnNextPage" runat="server" Text="下一页" Width="48px"
Height="25px" onclick="btnNextPage_Click" />
<asp:Button ID="btnLastPage" runat="server" Text="末页" Width="48px"
Height="25px" onclick="btnLastPage_Click" />
<span style="font-size:12px; font-style:italic; font-weight:bold;">页大小:</span>
<asp:TextBox ID="txtPageSize" runat="server" Width="25px" Height="25px"></asp:TextBox>
<span style="font-size:12px; font-style:italic; font-weight:bold;">
<asp:RequiredFieldValidator ID="rfvTxtPageSize" runat="server"
ControlToValidate="txtPageSize" ErrorMessage="页面记录大小不能为空!">*</asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvTxtPageSize" runat="server"
ControlToValidate="txtPageSize" ErrorMessage="页面记录大小必须大于零且不大于当前记录数!"
MinimumValue="1" Type="Integer">*</asp:RangeValidator>
跳到:</span>
<asp:TextBox ID="txtCurrentPageIndex" runat="server" Width="25px" Height="25px"></asp:TextBox>
<span style="font-size:12px; font-style:italic; font-weight:bold;">
<asp:RequiredFieldValidator ID="rfvTxtCPI" runat="server"
ControlToValidate="txtCurrentPageIndex" ErrorMessage="跳转页不能为空!">*</asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvTxtCPI" runat="server" BorderStyle="None"
ControlToValidate="txtCurrentPageIndex" ErrorMessage="输入页必须大于零且不大于当前页面数!"
MinimumValue="1" Type="Integer">*</asp:RangeValidator>
页</span>
<asp:Button ID="btnTurnPage" runat="server" Text="GO" Width="25px"
Height="25px" onclick="btnTurnPage_Click" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False" />
</div>
对应的PagerControl.ascx.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 _Controls_PagerControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rvTxtPageSize.MaximumValue = RecordCount.ToString();
rvTxtCPI.MaximumValue = PageCount.ToString();
SetCurrentStatus();
}
else
{
rvTxtPageSize.MaximumValue = RecordCount.ToString();
rvTxtCPI.MaximumValue = PageCount.ToString();
}
}
/// <summary>
/// 页面是否改变
/// </summary>
private bool pagerChanged = false;
public bool PagerChanged
{
get { return pagerChanged; }
set { pagerChanged = value; }
}
/// <summary>
/// 当前页索引号
/// </summary>
public int Pager
{
get { return (int)ViewState["Page"]; }
set { ViewState["Page"] = value; }
}
/// <summary>
/// 分页大小
/// </summary>
public int PageSize
{
get { return (int)ViewState["PageSize"]; }
set { ViewState["PageSize"] = value; }
}
/// <summary>
/// 记录数量
/// </summary>
public int RecordCount
{
get
{
return (int)ViewState["RecordCount"];
}
set
{
ViewState["RecordCount"] = value;
}
}
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
return (int)Math.Ceiling((double)RecordCount / PageSize);
}
}
protected void btnFirstPage_Click(object sender, EventArgs e)
{
pagerChanged = true;
PageSize = Convert.ToInt32(txtPageSize.Text.Trim());
Pager = 1;
SetCurrentStatus();
}
protected void btnPreviousPage_Click(object sender, EventArgs e)
{
pagerChanged = true;
PageSize = Convert.ToInt32(txtPageSize.Text.Trim());
Pager--;
SetCurrentStatus();
}
protected void btnNextPage_Click(object sender, EventArgs e)
{
pagerChanged = true;
PageSize = Convert.ToInt32(txtPageSize.Text.Trim());
Pager++;
SetCurrentStatus();
}
protected void btnLastPage_Click(object sender, EventArgs e)
{
pagerChanged = true;
PageSize = Convert.ToInt32(txtPageSize.Text.Trim());
Pager = PageCount;
SetCurrentStatus();
}
protected void btnTurnPage_Click(object sender, EventArgs e)
{
pagerChanged = true;
Pager = Convert.ToInt32(txtCurrentPageIndex.Text.Trim());
PageSize = Convert.ToInt32(txtPageSize.Text.Trim());
SetCurrentStatus();
}
private void SetCurrentStatus()
{
if (Pager == 1 && Pager == PageCount)
{
btnFirstPage.Enabled = false;
btnPreviousPage.Enabled = false;
btnNextPage.Enabled = false;
btnLastPage.Enabled = false;
}
else
{
if (Pager == 1)
{
btnFirstPage.Enabled = false;
btnPreviousPage.Enabled = false;
btnNextPage.Enabled = true;
btnLastPage.Enabled = true;
}
else if (Pager == PageCount)
{
btnFirstPage.Enabled = true;
btnPreviousPage.Enabled = true;
btnNextPage.Enabled = false;
btnLastPage.Enabled = false;
}
else
{
btnFirstPage.Enabled = true;
btnPreviousPage.Enabled = true;
btnNextPage.Enabled = true;
btnLastPage.Enabled = true;
}
}
txtCurrentPageIndex.Text = Pager.ToString();
txtPageSize.Text = PageSize.ToString();
lblMessage.Text = "当前页:" + Pager + "共" + PageCount + "页";
}
}
使用分页用户控件的.aspx页面代码如下:
<%@ Register src="_Controls/PagerControl.ascx" tagname="PagerControl" tagprefix="uc1" %>
<div class="contentstyle" style="text-align:center;margin:20px 0 20px 0;">
<uc1:PagerControl ID="PagerControl1" runat="server" />
</div>
使用分页用户控件的.aspx.cs业务逻辑文件代码如下:
public partial class BookList_UserPagerControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//首次加载,赋初值
try
{
ViewState["typeid"] = Convert.ToInt32(Request.QueryString["typeid"]);
}
catch
{
ViewState["typeid"] = -1;
}
this.PagerControl1.RecordCount = BookManager.GetBooksCountByCategoryId(Convert.ToInt32(ViewState["typeid"]));
btnChangeOrder.Enabled = false;
this.PagerControl1.Pager = 1;
this.PagerControl1.PageSize = 10;
BindData();
}
}
/// <summary>
/// 获得封面的url
/// </summary>
/// <param name="isbn"></param>
/// <returns></returns>
public string GetUrl(string isbn)
{
return StringHandler.CoverUrl(isbn);
}
/// <summary>
/// 截断图书显示内容
/// </summary>
/// <param name="content"></param>
/// <param name="num"></param>
/// <returns></returns>
public string GetCut(string content, int num)
{
return StringHandler.CutString(content, num);
}
#region 排序
protected void btnDate_Click(object sender, EventArgs e)
{
if (btnPrice.Enabled)
{
btnPrice.Enabled = false;
btnPrice.BackColor = Color.Gray;
if (!btnChangeOrder.Enabled)
{
btnChangeOrder.Enabled = true;
}
}
string publishDateOrder = (string)ViewState["Order"];
if (publishDateOrder == null)
{
ViewState["Order"] = "PublishDate";
btnDate.Text = "出版日期↑";
}
else
{
if (publishDateOrder.Equals("PublishDate"))
{
ViewState["Order"] = "PublishDate Desc";
btnDate.Text = "出版日期↓";
}
else
{
ViewState["Order"] = "PublishDate";
btnDate.Text = "出版日期↑";
}
}
this.PagerControl1.Pager = 1;
BindData();
}
protected void btnPrice_Click(object sender, EventArgs e)
{
if (btnDate.Enabled)
{
btnDate.Enabled = false;
btnDate.BackColor = Color.Gray;
if (!btnChangeOrder.Enabled)
{
btnChangeOrder.Enabled = true;
}
}
string unitPriceOrder = (string)ViewState["Order"];
if (unitPriceOrder == null)
{
ViewState["Order"] = "UnitPrice";
btnPrice.Text = "价格↑";
}
else
{
if (unitPriceOrder.Equals("UnitPrice"))
{
ViewState["Order"] = "UnitPrice Desc";
btnPrice.Text = "价格↓";
}
else
{
ViewState["Order"] = "UnitPrice";
btnPrice.Text = "价格↑";
}
}
this.PagerControl1.Pager = 1;
BindData();
}
#endregion
private void BindData()
{
int currentPageIndex = this.PagerControl1.Pager;
int pageSize = this.PagerControl1.PageSize;
IList<Book> books;
if (this.PagerControl1.Pager == this.PagerControl1.PageCount)
{
books = BookManager.GetCurrentPageBooksByCategoryId((currentPageIndex - 1) * pageSize + 1, this.PagerControl1.RecordCount, Convert.ToInt32(ViewState["typeid"]), (string)ViewState["Order"]);
}
else
{
books = BookManager.GetCurrentPageBooksByCategoryId((currentPageIndex - 1) * pageSize + 1, currentPageIndex * pageSize, Convert.ToInt32(ViewState["typeid"]), (string)ViewState["Order"]);
}
dlBooks.DataSource = books;
dlBooks.DataBind();
}
protected void btnChangeOrder_Click(object sender, EventArgs e)
{
if (btnDate.Enabled)
{
btnDate.Enabled = false;
btnDate.BackColor = Color.Gray;
}
else
{
btnDate.Enabled = true;
btnDate.BackColor = Color.FromArgb(192, 255, 192);
if (btnDate.Text.Trim().Equals("出版日期↑"))
{
ViewState["Order"] = "PublishDate";
}
else
{
ViewState["Order"] = "PublishDate Desc";
}
this.PagerControl1.Pager = 1;
BindData();
}
if (btnPrice.Enabled)
{
btnPrice.Enabled = false;
btnPrice.BackColor = Color.Gray;
}
else
{
btnPrice.Enabled = true;
btnPrice.BackColor = Color.FromArgb(192, 255, 192);
if (btnPrice.Text.Trim().Equals("价格↑"))
{
ViewState["Order"] = "UnitPrice";
}
else
{
ViewState["Order"] = "UnitPrice Desc";
}
this.PagerControl1.Pager = 1;
BindData();
}
}
protected void dlBooks_PreRender(object sender, EventArgs e)
{
if (this.PagerControl1.PagerChanged)
{
this.PagerControl1.PagerChanged = false;
this.PagerControl1.RecordCount = BookManager.GetBooksCountByCategoryId(Convert.ToInt32(ViewState["typeid"]));
BindData();
}
}
}
DAL获得记录数量方法代码如下:
public static int GetBooksCountByCategoryId(int categoryId)
{
string sql = string.Empty;
if (categoryId > 0)
{
sql = "select count(*) from Books where CategoryId=" + categoryId;
}
else
{
sql = "select count(*) from Books";
}
int bookCount = DBHelper.GetScalar(sql);
return bookCount;
}
好了,这一篇就到这里吧!这几篇的叙述少了些,源码多了些,相信大家对这些代码能够看懂,有什么不懂的地方给我留言吧!下一篇准备把基于SQL语句的数据获取方式重构为存储过程来实现。【待续...】