<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GridViewUserControl.ascx.cs" Inherits="PagerSet.GridViewUserControl" %>
<TABLE id="Table1" cellSpacing="0" cellPadding="0" border="0">
<TR>
<TD vAlign="bottom" style="height: 25px">
每页<font color="blue"><asp:label id="PageBulk" runat="server">0</asp:label></font>条
共<font color="blue"><asp:Label id="RecordNumber" runat="server">0</asp:Label></font>条记录
</TD>
<TD vAlign="bottom" align="center" style="height: 25px">
<asp:ImageButton id="First" runat="server" ImageUrl="../images/tohome.gif" ImageAlign="AbsMiddle" CausesValidation="False"></asp:ImageButton>
<asp:ImageButton id="Previous" runat="server" ImageUrl="../images/priv.gif" ImageAlign="AbsMiddle"
CausesValidation="False"></asp:ImageButton><font color="blue"><asp:label id="PageNumber" runat="server"></asp:label></font>
<asp:ImageButton id="Next" runat="server" ImageUrl="../images/next.gif" ImageAlign="AbsMiddle" CausesValidation="False"></asp:ImageButton>
<asp:ImageButton id="Lastly" runat="server" ImageUrl="../images/end.gif" ImageAlign="AbsMiddle" CausesValidation="False"></asp:ImageButton>
转到<asp:TextBox id="SelectPage" runat="server" Width="30px" CssClass="input"></asp:TextBox>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="../images/qd.gif" ImageAlign="AbsMiddle"
CausesValidation="False" OnClick="Go_Click"></asp:ImageButton>
</TD>
</TR>
</TABLE>
namespace PagerSet
{
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// 实现GridView控件的分页显示
/// </summary>
public delegate void BindDataDelegate();//事件委托
public abstract partial class GridViewUserControl : System.Web.UI.UserControl
{
//事件委托绑定数据
private BindDataDelegate BindData;
private GridView _gv;//要操作的GridView控件
private int pagesize = 1;//默认的分页数
private int pageNum = 0;//默认的页数
private int _tempPageIndex = 1;
private int nStartNum = 0, nEndNum = 0;
private string _strRecordInfo;
public int RecordAmount
{
get
{
object obj = ViewState["recordAmount"];
return (obj == null) ? 0 : (int)obj;
}
set { ViewState["recordAmount"] = value; }
}
public int PageIndex
{
get
{
object obj = ViewState["pageIndex"];
return (obj == null) ? 0 : (int)obj;
}
set { ViewState["pageIndex"] = value; }
}
public string StrRecordInfo
{
get { return _strRecordInfo; }
}
private void Page_Load(object sender, System.EventArgs e)
{
}
/// <summary>
/// 主方法
/// </summary>
/// <param name="adg">GridView控件名</param>
/// <param name="NewBind">数据绑定事件委托</param>
/// <param name="iPageSize">每页显示数</param>
public void SetTarget(GridView agv, BindDataDelegate NewBind, int iPageSize)
{
_gv = agv;
pageNum = iPageSize;
BindData = new BindDataDelegate(NewBind);
this.First.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
this.Previous.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
this.Next.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
this.Lastly.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
_gv.DataBinding += new System.EventHandler(this.BeforeDataBinding);
_gv.AllowPaging = true;
_gv.PagerSettings.Visible = false;
pagesize = iPageSize;
_gv.PageSize = pagesize;
_gv.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
}
/// <summary>
/// 主方法
/// </summary>
/// <param name="adg">GridView控件名</param>
/// <param name="NewBind">数据绑定事件委托</param>
public void SetTarget(GridView agv, BindDataDelegate NewBind)
{
_gv = agv;
BindData = new BindDataDelegate(NewBind);
this.First.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
this.Previous.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
this.Next.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
this.Lastly.Click += new System.Web.UI.ImageClickEventHandler(this.NavigationButton_Click);
_gv.DataBinding += new System.EventHandler(this.BeforeDataBinding);
_gv.AllowPaging = true;
_gv.PagerSettings.Visible = false;
_gv.PageSize = pagesize;
_gv.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
}
//分页按钮事件
private void NavigationButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
string strCommandName = ((ImageButton)sender).ID;//取得事件的对象名
if (RecordAmount % pagesize == 0)
{
pageNum = RecordAmount / pagesize;
}
else
{
pageNum = RecordAmount / pagesize + 1;
}
_tempPageIndex = PageIndex+1 ;
switch (strCommandName)
{
case "First":
PageIndex = 0;//跳转到首页
break;
case "Previous":
PageIndex = Math.Max(PageIndex - 1, 0);//跳转到上一页
break;
case "Next":
PageIndex = Math.Min(PageIndex + 1, pageNum - 1);//跳转到下一页
break;
case "Lastly":
PageIndex = pageNum - 1;//跳转到最后一页
break;
}
BindData();//绑定数据
SelectPage.Text = _tempPageIndex.ToString();
}
//页面跳转
public void Go_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if (SelectPage.Text.Trim() != "")
{
int iPage = int.Parse(SelectPage.Text.Trim()) - 1;
if (iPage >= 0 && iPage <= pageNum - 1)
{
_tempPageIndex = PageIndex+1 ;
PageIndex = iPage;
}
else if (iPage > pageNum - 1)
{
_tempPageIndex = PageIndex+1 ;
PageIndex = pageNum - 1;
}
else
{
_tempPageIndex = PageIndex+1 ;
PageIndex = 0;
}
BindData();
}
else
ImageButton1.EnableViewState = true;
SelectPage.Text = _tempPageIndex.ToString();
}
/// <summary>
/// 设置导航条(根据委托的执行顺序应该是先绑定了Gridview然后执行该方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeforeDataBinding(object sender, System.EventArgs e)
{
int newCount = RecordAmount;
pageNum = (int)((newCount - 1) / pagesize + 1);
PageNumber.Text = (PageIndex + 1).ToString() + "/" + pageNum.ToString();
PageBulk.Text = pagesize.ToString();
RecordNumber.Text = newCount.ToString();
nStartNum = PageIndex * pagesize + 1;
nEndNum = nStartNum + pagesize - 1;
if (nEndNum > newCount)
{
nEndNum = newCount;
}
if (newCount == 0)
{
_strRecordInfo = "共0条记录!";
}
else
{
_strRecordInfo = "共" + newCount.ToString() + "条记录,本页显示从第" + nStartNum.ToString() + "条到第" + nEndNum.ToString() + "条!";
}
}
}
}