程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 原創的自定義分頁UserControl 分享及探討

原創的自定義分頁UserControl 分享及探討

編輯:.NET實例教程
----------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" %>
------------------------------------------------------------------------------------ -----------調用頁面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); } }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved