WEB开发网
开发学院WEB开发ASP.NET 原创的自定义分页UserControl 分享及探讨 阅读

原创的自定义分页UserControl 分享及探讨

 2004-11-03 16:58:13 来源:WEB开发网   
核心提示:--Pager.ascx-<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Test.Pager.ascx.cs" Inherits="Pager" Targe
----------Pager.ascx------------------------------------- <%@ Control Language="c#" AutoEventWireup="false" Codebehind="Test.Pager.ascx.cs" Inherits="Pager" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> 还未用PagerBind()方法绑定  首页 前页 下页 尾页 转到 0 页 ----------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; /// /// Pager 的摘要说明。 /// 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); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// 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="共"+ViewState["RecTotal"].ToString()+"条信息 "+ViewState["PageSize"].ToString()+"条/页 当前"+((int)ViewState["CurPage"]+1).ToString()+"/"+ViewState["PageTotal"].ToString()+"页"; } else { lbl_PageInfo.Text="Total"+ViewState["RecTotal"].ToString()+"Info "+ViewState["PageSize"].ToString()+"/Page Current"+((int)ViewState["CurPage"]+1).ToString()+"/"+ViewState["PageTotal"].ToString()+"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" %> WebForm2
------------------------------------------------------------------------------------ -----------调用页面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 { /// /// WebForm2 的摘要说明。 /// 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); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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); } } }

Tags:原创 定义 UserControl

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接