不知道为什么,微软既然提供了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);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
ChangePage.ascx.cs CodeBehind代码结束
2、如何在其他的页面中使用翻页的用户自定义控件
2.1、页面中拖拽的用户自定义控件代码如下
<uc1:changepage id="tcChangePage" runat="server"></uc1:changepage>
2.2、CodeBehind代码如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Education.Web
{
/// <summary>
/// 维护页面
/// </summary>
public class TeachingCenterList : System.Web.UI.Page
{
//声明翻页用户控件 注意:名称tcChangePage应该和拖拽过来后的界面中的ChangePage控件的ID相同
protected ChangePage tcChangePage;
protected System.Web.UI.WebControls.DataGrid MyDataGrid; //要翻页的DataGrid控件的名称
public void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
//网格数据绑定
BindGrid();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
//翻页控件中的事件声明
tcChangePage.PageIndexChanged +=new ChangePage.PageIndexChangedEventHandler(this.PageButtonClick); //翻页事件
tcChangePage.GoPage +=new ChangePage.GoPageEventHandler(this.PageGoButtonClick); //GoTo事件
}
#endregion
/// 网格数据绑定
void BindGrid()
{
//读取数据库的信息,获得DataView 将DataView绑定到DataGrid上去
{....}
//显示页码
tcChangePage.ShowStats( MyDataGrid );
//判断翻页按钮Enable状态
tcChangePage.PagerButtonEnable( MyDataGrid );
}
#region 翻页处理 代码的主体部分
/// 翻页按钮事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void PageButtonClick(Object sender,System.EventArgs e)
{
//获得LinkButton的参数值
String arg = ((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("next"): //下页
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"): //上页
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("last"): //末页
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default: //首页
MyDataGrid.CurrentPageIndex = 0;
break;
}
//网格数据绑定
BindGrid();
//网格数据指定
MyDataGrid.SelectedIndex = 0 ;
//指定到网格中第一行,同时将数据显示到明细窗口(显示)
SelectItemCommand( 0 , "显示");
}
/// <summary>
/// 翻页Go按钮处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="PageIndex"></param>
public void PageGoButtonClick(Object sender , int PageIndex)
{
//获得Button的参数值
string arg = ((Button)sender).CommandArgument;
switch(arg)
{
case ("GoPage"): //指定到某一页
if (PageIndex <= MyDataGrid.PageCount)
{
MyDataGrid.CurrentPageIndex = (PageIndex-1);
}
else
{
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount-1);
}
break;
}
//网格数据绑定
BindGrid();
//网格数据指定
MyDataGrid.SelectedIndex = 0 ;
//指定到网格中第一行,同时将数据显示到明细窗口(显示)
SelectItemCommand( 0 , "显示");
}
#endregion
}
}
代码写了一大堆,看上去挺复杂,其实挺简单的,呵呵,如果觉得是垃圾,请弃之。。。。