此用户控件是同事写的,方法都封装好了,虽然没有存储过程分页那么有效率,但对于数据量不是太大的项目还是可以用的,最重要的就是界面比较好看,还有就是调用方便
新建用户控件MyGVPage.ascx
前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyGVPage.ascx.cs" Inherits="MyGVPage" %>
<script language="javascript">
function callButtonEvent<%=btnGo.ClientID%>()
{
var keycode =window.event.keyCode;
if(keycode==13)
{
if(check<%=btnGo.ClientID%>()==true)
{
event.cancelBubble=true;
event.returnValue=false;
document.getElementById('<%=btnGo.ClientID%>').click();
}
}
}
function check<%=btnGo.ClientID%>()
{
var count = parseInt(document.getElementById('<%=lblTotal.ClientID%>').outerText);
var txt = document.getElementById('<%=txtCurrentPage.ClientID%>').value;
var cur = parseInt(txt);
if ((cur | NaN) ==0)
{
alert('请输入1到<%=lblTotal.Text%>之间的数字!');
event.cancelPostBack=true;
return false;
}
if (cur > count || cur < 1)
{
alert('请输入1到<%=lblTotal.Text%>之间的数字!');
event.cancelPostBack=true;
return false;
}
}
</script>
<table id="tbJump" cellSpacing="0" cellPadding="0" width="100%" border="0" class="table">
<TR align="right">
<TD>
<asp:linkbutton id="btnFirstPage" runat="server" CommandArgument="First">首页</asp:linkbutton>
<asp:linkbutton id="btnPrevPage" runat="server" CommandArgument="Prev">上一页</asp:linkbutton>
<asp:linkbutton id="btnNextPage" runat="server" CommandArgument="Next">下一页</asp:linkbutton>
<asp:linkbutton id="btnLastPage" runat="server" CommandArgument="Last">尾页</asp:linkbutton>
页次:<asp:Label ID="lblCurrentPage" Runat="server">0</asp:Label>/<ASP:LABEL id="lblTotal" RUNAT="server">0</ASP:LABEL>页
共<asp:Label ID="lblTotalPage" Runat="server">0</asp:Label>条 每页<asp:Label ID="lblRecordOfPage" Runat="server">0</asp:Label>条
第<ASP:TEXTBOX id="txtCurrentPage" RUNAT="server" Width="20px" CssClass="text">1</ASP:TEXTBOX>页
<ASP:BUTTON id="btnGo" RUNAT="server" TEXT="跳转" COMMANDARGUMENT="Go" ToolTip="跳转" CssClass="button"></ASP:BUTTON>
</TD>
</TR>
</table>
后台:
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;
public partial class MyGVPage : System.Web.UI.UserControl
{
#region 控件声明
protected System.Web.UI.HtmlControls.HtmlTable Table2;
public event System.EventHandler NavigationClick;
#endregion
#region 窗体初始化
private void Page_Load(object sender, System.EventArgs e)
{
//设置控件宽度
if (!this.IsPostBack)
{
this.txtCurrentPage.Attributes.Add("onkeypress", "callButtonEvent" + btnGo.ClientID + "();");
this.btnGo.Attributes.Add("onclick", "check" + btnGo.ClientID + "();");
SetStyle();
SetEnable();
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnFirstPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnPrevPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnNextPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnLastPage.Click += new System.EventHandler(this.btnGo_Click);
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region 窗体事件
#region 跳转按钮
private void btnGo_Click(object sender, System.EventArgs e)
{
LinkButton linkbtn = sender as LinkButton;
if (null == linkbtn)//button
{
Button btn = sender as Button;
if (null == btn)
{
return;
}
else
{
int selPage = -1;
try
{
selPage = Int32.Parse(txtCurrentPage.Text);
}
catch
{
selPage = -1;
}
if (selPage > 0 && selPage <= PageCount)
{
ViewState["CurrentPageIndex"] = selPage;
}
else
{
return;
}
}
}
else//linkbutton
{
switch (linkbtn.CommandArgument.Trim())
{
case "First":
ViewState["CurrentPageIndex"] = 1;
break;
case "Prev":
ViewState["CurrentPageIndex"] = (CurrentPageIndex > 1) ? CurrentPageIndex - 1 : 1;
break;
case "Next":
ViewState["CurrentPageIndex"] = (PageCount > CurrentPageIndex) ? CurrentPageIndex + 1 : PageCount;
break;
case "Last":
ViewState["CurrentPageIndex"] = PageCount;
break;
default:
break;
}
}
if (NavigationClick != null)//调用事件
{
NavigationClick(sender, e);
}
}
#endregion
#endregion
#region 共有方法
#region 设置要控制的DataGrid的格式
public void SetPageGrid(GridView gv)
{
gv.PageIndex = this.CurrentPageIndex - 1;
gv.PageSize = this.PageSize;
}
#endregion
#endregion
#region 私有方法
#region 样式控制
private void SetStyle()
{
this.btnFirstPage.Attributes["style"] = "CURSOR: hand";
this.btnLastPage.Attributes["style"] = "CURSOR: hand";
this.btnNextPage.Attributes["style"] = "CURSOR: hand";
this.btnPrevPage.Attributes["style"] = "CURSOR: hand";
}
#endregion
#region 可用性设置
// 应根据当前的CurrentPageIndex和pageCount设定哪些按钮可用
private void SetEnable()
{
this.lblTotal.Text = PageCount.ToString();
txtCurrentPage.Text = CurrentPageIndex.ToString();
lblCurrentPage.Text = CurrentPageIndex.ToString();
lblTotalPage.Text = RecordCount.ToString();
lblRecordOfPage.Text = PageSize.ToString();
btnPrevPage.Enabled = false;
btnNextPage.Enabled = false;
if (PageCount > 1)
{
btnFirstPage.Enabled = btnPrevPage.Enabled = (CurrentPageIndex > 1);
btnNextPage.Enabled = btnLastPage.Enabled = (CurrentPageIndex < PageCount);
}
else
{
btnFirstPage.Enabled = false;
btnLastPage.Enabled = false;
btnPrevPage.Enabled = false;
btnNextPage.Enabled = false;
}
}
#endregion
#endregion
#region 属性声明
#region 获取或设置当前显示页的索引
public int CurrentPageIndex
{
get
{
object cpage = ViewState["CurrentPageIndex"];
int pindex = (cpage == null) ? 1 : (int)cpage;
if (pindex > PageCount && PageCount > 0)
return PageCount;
else if (pindex < 1)
return 1;
return pindex;
}
set
{
int cpage = value;
if (cpage < 1)
cpage = 1;
else if (cpage > this.PageCount)
cpage = this.PageCount;
ViewState["CurrentPageIndex"] = cpage;
}
}
#endregion
#region 获取或设置需要分页的所有记录的总数
public int RecordCount
{
get
{
object obj = ViewState["Recordcount"];
return (obj == null) ? 0 : (int)obj;
}
set
{
ViewState["Recordcount"] = value;
SetEnable();
}
}
#endregion
#region 获取当前页之后的页的总数
public int PagesRemain
{
get
{
return PageCount - CurrentPageIndex;
}
}
#endregion
#region 获取每页显示的项数
private int intPageSize = 0;
public int PageSize
{
get
{
if (this.intPageSize == 0)
{
this.intPageSize = 15;
}
return this.intPageSize;
}
set
{
this.intPageSize = value;
}
}
#endregion
#region 获取在当前页之后还未显示的剩余记录的项数
public int RecordsRemain
{
get
{
if (CurrentPageIndex < PageCount)
{
return RecordCount - (CurrentPageIndex * PageSize);
}
else
{
return 0;
}
}
}
#endregion
#region 获取所有要分页的记录需要的总页数
public int PageCount
{
get { return (RecordCount > 0) ? (int)Math.Ceiling((double)RecordCount / (double)PageSize) : 1; }
}
#endregion
#endregion
}
调用:
新建页面Test.aspx
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<%@ Register TagPrefix="uc1" TagName="MyGVPage" Src="~/MyGVPage.ascx" %>
<!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">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="180px"
Width="867px" AllowPaging="true">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="isbn" HeaderText="isbn" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:BoundField DataField="num" HeaderText="num" />
<asp:BoundField DataField="time" HeaderText="time" />
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
<uc1:MyGVPage runat="server" ID="mypage" />
</div>
</form>
</body>
</html>
后台:
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;
public partial class Test : System.Web.UI.Page
{
VEFLibrary.aa a = new VEFLibrary.aa();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.mypage.NavigationClick += new EventHandler (mypage_NavigationClick);
}
private void mypage_NavigationClick(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
DataTable dt = a.GetInfo2().Tables[0];
this.mypage.RecordCount = dt.Rows.Count;
this.mypage.SetPageGrid(this.GridView1);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
}