自定义分页UserControl 分享及探讨!
发表于:2007-06-30来源:作者:点击数:
标签:
----------Pager.ascx------------------------------------- %@ Control Language=c# AutoEventWireup=false Codebehind=Test.Pager.ascx.cs Inherits=Pager TargetSchema=http://schemas.microsoft.com/intellisense/ie5 % asp:label id=lbl_PageInfo runat
----------Pager.ascx-------------------------------------
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Test.Pager.ascx.cs" Inherits="Pager" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:label id="lbl_PageInfo" runat="server">还未用PagerBind()方法绑定</asp:label>
<asp:linkbutton id="btn_First" runat="server" Visible="False">首页</asp:linkbutton> <asp:linkbutton id="btn_Pre" runat="server" Visible="False">前页</asp:linkbutton> <asp:linkbutton id="btn_Next" runat="server" Visible="False">下页</asp:linkbutton> <asp:linkbutton id="btn_Last" runat="server" Visible="False">尾页</asp:linkbutton> <asp:label id="lbl_1" runat="server" Visible="False">转到</asp:label>
<asp:dropdownlist id="drop_CurPage" runat="server" AutoPostBack="True" Visible="False">
<asp:ListItem Value="0">0</asp:ListItem>
</asp:dropdownlist>
<asp:Label id="lbl_2" runat="server" Visible="False">页</asp:Label>
----------end--------------------------------------------
----------Pager.ascx.cs-------------------------------------
namespace Test
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Pager 的摘要说明。
/// </summary>
public class Pager : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.LinkButton btn_First;
protected System.Web.UI.WebControls.LinkButton btn_Pre;
protected System.Web.UI.WebControls.LinkButton btn_Next;
protected System.Web.UI.WebControls.LinkButton btn_Last;
protected System.Web.UI.WebControls.DropDownList drop_CurPage;
protected System.Web.UI.WebControls.Label lbl_1;
protected System.Web.UI.WebControls.Label lbl_2;
protected System.Web.UI.WebControls.Label lbl_PageInfo;
public event EventHandler MyClick;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
RecTotal = 0;
PageSize = 10;
CurPage = 0;
Lang = 0; //中文版
}
}
#region Pager控件使用帮助及参数说明
/*
* 版本:V1.0
本控件带有三个自定义属性和一个方法.具体如下:
属性:
RecTotal 记录总数 默认值:0
PageSize 每页记录数 默认值:0
CurPage 当前页 默认值:0
Lang 版本语言 默认值:0 注:0为中文版,1为英文版
方法:
PagerBind 绑定当前自定义控件,如不绑定,该控件则不能显示到页面上
*/
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_First.Click += new System.EventHandler(this.btn_First_Click);
this.btn_Pre.Click += new System.EventHandler(this.btn_Pre_Click);
this.btn_Next.Click += new System.EventHandler(this.btn_Next_Click);
this.btn_Last.Click += new System.EventHandler(this.btn_Last_Click);
this.drop_CurPage.SelectedIndexChanged += new System.EventHandler(this.drop_CurPage_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region 共用参数传递
public int RecTotal //记录总数
{
get { return (int)ViewState["RecTotal"]; }
set { ViewState["RecTotal"] = value; }
}
public int PageSize //每页记录数
{
get { return (int)ViewState["PageSize"]; }
set { ViewState["PageSize"] = value; }
}
public int CurPage //当前页
{
get { return (int)ViewState["CurPage"]; }
set { ViewState["CurPage"] = value; }
}
public int Lang //语言选择
{
get { return (int)ViewState["Lang"]; }
set { ViewState["Lang"] = value; }
}
#endregion
#region 相关方法及事件
public void PagerBind()
{
ViewState["PageTotal"]=((int)ViewState["RecTotal"]%(int)ViewState["PageSize"]>0) ? (int)ViewState["RecTotal"]/(int)ViewState["PageSize"]+1 : (int)ViewState["RecTotal"]/(int)ViewState["PageSize"]; //页总数
if ((int)ViewState["Lang"]==0)
{
lbl_PageInfo.Text="共<font color=red>"+ViewState["RecTotal"].ToString()+"</font>条信息 <font color=red>"+ViewState["PageSize"].ToString()+"</font>条/页 当前<font color=red>"+((int)ViewState["CurPage"]+1).ToString()+"</font>/<font color=red>"+ViewState["PageTotal"].ToString()+"</font>页";
}
else
{
lbl_PageInfo.Text="Total<font color=red>"+ViewState["RecTotal"].ToString()+"</font>Info <font color=red>"+ViewState["PageSize"].ToString()+"</font>/Page Current<font color=red>"+((int)ViewState["CurPage"]+1).ToString()+"</font>/<font color=red>"+ViewState["PageTotal"].ToString()+"</font>Page";
lbl_1.Text = "To";
lbl_2.Text = "Page";
btn_First.Text = "First";
btn_Pre.Text = "Forward";
btn_Next.Text = "Next";
btn_Last.Text = "Last";
}
drop_CurPage.Items.Clear();
for (int i=0;i<(int)ViewState["PageTotal"];i++)
{
drop_CurPage.Items.Add(Convert.ToString(i+1));
}
drop_CurPage.SelectedIndex = drop_CurPage.Items.IndexOf(drop_CurPage.Items.FindByValue(Convert.ToString((int)ViewState["CurPage"]+1)));
btn_First.Visible = true;
btn_Pre.Visible = true;
btn_Next.Visible = true;
btn_Last.Visible = true;
lbl_1.Visible = true;
lbl_2.Visible = true;
drop_CurPage.Visible = true;
btn_First.Enabled = true;
btn_Pre.Enabled = true;
btn_Next.Enabled = true;
btn_Last.Enabled = true;
if ((int)ViewState["CurPage"]==0)
{
btn_First.Enabled = false;
btn_Pre.Enabled = false;
btn_Next.Enabled = true;
btn_Last.Enabled = true;
}
if ((int)ViewState["CurPage"]==( (int)ViewState["PageTotal"]-1) )
{
btn_First.Enabled = true;
btn_Pre.Enabled = true;
btn_Next.Enabled = false;
btn_Last.Enabled = false;
}
if (( (int)ViewState["PageTotal"]-1) == 0 )
{
btn_First.Enabled = false;
btn_Pre.Enabled = false;
btn_Next.Enabled = false;
btn_Last.Enabled = false;
}
}
private void drop_CurPage_SelectedIndexChanged(object sender, System.EventArgs e)
{
ViewState["CurPage"] = drop_CurPage.SelectedIndex;
PagerBind();
if (MyClick != null)
MyClick(this, EventArgs.Empty);
}
private void btn_First_Click(object sender, System.EventArgs e)
{
ViewState["CurPage"] = 0;
PagerBind();
if (MyClick != null)
MyClick(this, EventArgs.Empty);
}
private void btn_Pre_Click(object sender, System.EventArgs e)
{
ViewState["CurPage"] = (int)ViewState["CurPage"]-1;
PagerBind();
if (MyClick != null)
MyClick(this, EventArgs.Empty);
}
private void btn_Next_Click(object sender, System.EventArgs e)
{
ViewState["CurPage"] = (int)ViewState["CurPage"]+1;
PagerBind();
if (MyClick != null)
MyClick(this, EventArgs.Empty);
}
private void btn_Last_Click(object sender, System.EventArgs e)
{
ViewState["CurPage"] = (int)ViewState["PageTotal"]-1;
PagerBind();
if (MyClick != null)
MyClick(this, EventArgs.Empty);
}
#endregion
}
}
------------------------------------------------------------------------------
-----------调用页面WebForm1.aspx--------------------------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Test.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="Pager" Src="Pager.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//D
TD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="
JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid></FONT>
<uc1:Pager id="Pager1" runat="server" OnMyClick="GetPage"></uc1:Pager>
</form>
</body>
</HTML>
------------------------------------------------------------------------------------
-----------调用页面WebForm1.aspx.cs--------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Test
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected Test.Pager Pager1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataGrid1Bind(0);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1Bind(int Page)
{
DataGrid1.DataSource = myDataSource; //此处请自行绑定当前页数据集的数据源
DataGrid1.DataBind();
Pager1.RecTotal = MyTotal; //此处请自行绑定数据集总记录数
Pager1.PageSize = 10;
Pager1.CurPage = Page;
Pager1.Lang = 0; //0为中文版,1为英文版
Pager1.PagerBind();
}
public void GetPage(Object sender, EventArgs e)
{
DataGrid1Bind(Pager1.CurPage);
}
}
}
-------------------------------------------------------------------------
原文转自:http://www.ltesting.net