不知道为什么,微软既然提供了DataGrid的分页功能,为什么功能那么弱,要么只有上一页、下一页,要么就1、2、3、4、5、6页,根本不符合我们中国软件的“国情”啊。在我的印象里,好像不但要有“首页”、“上页”、“下页”、“末页”,还要有Goto到指定页等等,才算完美啊。不知道大家是否有同感。
鉴于此,我写了个关于DataGrid的分页功能的一个自定义用户控件,只要DataGrid需要翻页,就把此自定义控件过进来,在简单的Copy一下代码,就OK了。以下是自定义控件的代码。和如何使用它的代码,供大家参考,不妥之处,望批之。
1、自定义用户控件名称为 ChangePage.ascx
1.1、界面段的代码如下
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ChangePage.ascx.cs" Inherits="Education.Web.ChangePage"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<script language="javascript">
<!--
function val_num(field) {
if (field.value=="")
{
alert("错误!\n\n请输入正确数字!");
field.focus();
field.select();
return false;
}
else if (field.value.indexOf(" ")>=0)
{
alert("错误!\n\n请输入正确数字!\n勿包含空格!");
field.focus();
field.select();
return false;
}
else if (isNaN(field.value))
{
alert("错误!\n\n请输入正确数字!");
field.focus();
field.select();
return false;
}
else if (field.value.indexOf('.')>=0)
{
alert("错误!\n\n请输入整数!")
field.focus();
field.select();
return false;
}
else if (parseInt(field.value)<=0)
{
alert("错误!\n\n请输入正确数字!");
field.focus();
field.select();
return false;
}
return true;
}
//-->
</script>
<TABLE id="Table11" cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td align="right"><asp:linkbutton id="btnFirst" runat="server" CssClass="Linkbutton" Text="首页" CommandArgument="0">首页
</asp:linkbutton><asp:linkbutton id="btnPrev" runat="server" CssClass="Linkbutton" Text="前页" CommandArgument="prev">
前页</asp:linkbutton><asp:linkbutton id="btnNext" runat="server" CssClass="Linkbutton" Text="后页" CommandArgument="next">
后页</asp:linkbutton><asp:linkbutton id="btnLast" runat="server" CssClass="Linkbutton" Text="末页" CommandArgument="last">
末页</asp:linkbutton>
<asp:label id="lblGoPage" runat="server">转到:</asp:label><asp:textbox id="txtGoPage" runat="server" Width="25px"
CssClass="text"></asp:textbox> <asp:label id="lblPage" runat="server">页</asp:label>
<asp:button id="btnGo" runat="server" CssClass="buttonShort" Text="Go" CommandArgument="GoPage"></asp:button> <asp:label
id="lblCurrentIndex" runat="server"></asp:label></td>
</tr>
</TABLE>
界面段代码结束
1.2、ChangePage.ascx.cs CodeBehind代码如下
namespace Education.Web
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// 自定义用户控件,用来显示Grid分页
/// </summary>
public abstract class ChangePage : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.Label lblGoPage;
protected System.Web.UI.WebControls.TextBox txtGoPage;
protected System.Web.UI.WebControls.Label lblPage;
protected System.Web.UI.WebControls.Button btnGo;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
//声明翻页事件
public delegate void PageIndexChangedEventHandler(object sender, System.EventArgs e);
public event PageIndexChangedEventHandler PageIndexChanged;
//声明GoTo事件
public delegate void GoPageEventHandler(object sender, int PageIndex);
public event GoPageEventHandler GoPage;
protected virtual void OnPageIndexChanged(object sender,System.EventArgs e)
{
if (PageIndexChanged != null)
PageIndexChanged(sender, e);
}
protected virtual void OnGoPage(object sender,int PageIndex)
{
if (GoPage != null)
GoPage(sender, PageIndex);
}
private void Page_Load(object sender, System.EventArgs e)
{
//判断txtGoPage输入的是否为数字
this.btnGo.Attributes.Add("onclick","return val_num("+this.txtGoPage.ClientID.ToString()+")");
}
//设置翻页按钮的Enabled属性
public void PagerButtonEnable(System.Web.UI.WebControls.DataGrid dgPage)
{
btnFirst.Enabled = (dgPage.CurrentPageIndex == 0 ? false : true);
btnPrev.Enabled = (dgPage.CurrentPageIndex == 0 ? false : true);
btnNext.Enabled = (dgPage.CurrentPageIndex == (dgPage.PageCount - 1) ? false : true);
btnLast.Enabled = (dgPage.CurrentPageIndex == (dgPage.PageCount - 1) ? false : true);
}
//显示页面信息
public void ShowStats(System.Web.UI.WebControls.DataGrid dgPage)
{
lblCurrentIndex.Text = "页码:[ " + ((int)dgPage.CurrentPageIndex+1) + " / "+ dgPage.PageCount + " ]" ;
}
//首页
private void btnFirst_Click(object sender, System.EventArgs e)
{
OnPageIndexChanged( sender , e );
}
//上页
private void btnPrev_Click(object sender, System.EventArgs e)
{
OnPageIndexChanged( sender , e );
}
//下页
private void btnNext_Click(object sender, System.EventArgs e)
{
OnPageIndexChanged( sender , e );
}
//末页
private void btnLast_Click(object sender, System.EventArgs e)
{
OnPageIndexChanged( sender , e );
}
//GoTo到某一页
private void btnGo_Click(object sender, System.EventArgs e)
{
OnGoPage( sender ,Convert.ToInt32(this.txtGoPage.Text) );
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
&n