- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class UserControl_UserControlPage : System.Web.UI.UserControl
- {
- //protected System.Web.UI.WebControls.Label lblTotal;
- //protected System.Web.UI.WebControls.Label labOf;
- //protected System.Web.UI.WebControls.TextBox txtCurrentPage;
- //protected System.Web.UI.WebControls.Button btnGo;
- //protected System.Web.UI.WebControls.LinkButton btnFirstPage;
- //protected System.Web.UI.WebControls.LinkButton btnPrevPage;
- //protected System.Web.UI.WebControls.LinkButton btnNextPage;
- //protected System.Web.UI.WebControls.LinkButton btnLastPage;
- int size = 10;//可以在web.config中配置
- public event System.EventHandler NavigationClick;
- protected void Page_Load(object sender, EventArgs e)
- {
- this.txtCurrentPage.Attributes.Add("onkeypress", "callButtonEvent();");
- this.btnGo.Attributes.Add("onclick", "check();");
- if (!this.IsPostBack)
- {
- SetStyle();
- SetEnable();
- }
- }
- #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);
- this.btnFirstPage.Click += new System.EventHandler(this.btnGo_Click);
- this.btnPrevPage.Click += new System.EventHandler(this.btnGo_Click);
- this.btnNextPage.Click += new System.EventHandler(this.btnGo_Click);
- this.btnLastPage.Click += new System.EventHandler(this.btnGo_Click);
- this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- #region btnGo_Click
- private void btnGo_Click(object sender, System.EventArgs e)
- {
- LinkButton linkbtn = sender as LinkButton;
- if( null == linkbtn )//button
- {
- Button btn = sender as Button;
- if( null == btn )
- {
- return;
- }
- else
- {
- int selPage = -1;
- try
- {
- selPage =Int32.Parse(txtCurrentPage.Text);
- }
- catch
- {
- selPage = -1;
- }
- if (selPage > 0 && selPage <= PageCount)
- {
- ViewState["CurrentPageIndex"] = selPage;
- }
- else
- {
- return;
- }
- }
- }
- else//linkbutton
- {
- switch ( linkbtn.CommandArgument.Trim() )
- {
- case "First":
- ViewState["CurrentPageIndex"] = 1;
- break;
- case "Prev":
- ViewState["CurrentPageIndex"] = (CurrentPageIndex > 1) ? CurrentPageIndex - 1 : 1;
- break;
- case "Next":
- ViewState["CurrentPageIndex"] = (PageCount > CurrentPageIndex) ? CurrentPageIndex + 1 : PageCount;
- break;
- case "Last":
- ViewState["CurrentPageIndex"] = PageCount;
- break;
- case "Go":
- int selPage = -1;
- try
- {
- selPage = Int32.Parse(txtCurrentPage.Text);
- }
- catch
- {
- selPage = -1;
- }
- if (selPage > 0 && selPage <= PageCount)
- {
- ViewState["CurrentPageIndex"] = selPage;
- }
- else
- {
- return;
- }
- break;
- default:
- break;
- }
- }
- SetEnable();//设置显示样式
- if (NavigationClick!=null)//调用事件
- {
- NavigationClick( sender,e );
- }
- }
- #endregion
- #region SetStyle
- private void SetStyle()
- {
- this.btnFirstPage.Attributes["style"] = "CURSOR: hand";
- this.btnLastPage.Attributes["style"] = "CURSOR: hand";
- this.btnNextPage.Attributes["style"] = "CURSOR: hand";
- this.btnPrevPage.Attributes["style"] = "CURSOR: hand";
- }
- #endregion
- #region SetEnable
- // 应根据当前的CurrentPageIndex和pageCount设定哪些按钮可用
- private void SetEnable()
- {
- this.ltTotal.Text = PageCount.ToString();
- this.lbCurrent.Text = CurrentPageIndex.ToString();
- txtCurrentPage.Text =CurrentPageIndex.ToString();
- btnPrevPage.Enabled = false;
- btnNextPage.Enabled = false;
- if( PageCount >1 )
- {
- btnFirstPage.Enabled = btnPrevPage.Enabled = ( CurrentPageIndex >1 );
- btnNextPage.Enabled = btnLastPage.Enabled = ( CurrentPageIndex < PageCount );
- }
- else
- {
- btnFirstPage.Enabled = false;
- btnLastPage.Enabled = false;
- btnPrevPage.Enabled = false;
- btnNextPage.Enabled = false;
- }
- }
- #endregion
- #region Property
- //获取或设置当前显示页的索引。
- public int CurrentPageIndex
- {
- get
- {
- object cpage = ViewState["CurrentPageIndex"];
- int pindex = (cpage == null) ? 1 : (int)cpage;
- if (pindex > PageCount && PageCount > 0)
- return PageCount;
- else if (pindex < 1)
- return 1;
- return pindex;
- }
- set
- {
- int cpage = value;
- if (cpage < 1)
- cpage = 1;
- else if (cpage > this.PageCount)
- cpage = this.PageCount;
- ViewState["CurrentPageIndex"] = cpage;
- }
- }
- // 获取或设置需要分页的所有记录的总数。
- public int RecordCount
- {
- get
- {
- object obj = ViewState["Recordcount"];
- return (obj == null) ? 0 : (int)obj;
- }
- set
- {
- ViewState["Recordcount"] = value;
- SetEnable();
- }
- }
- // 获取当前页之后的页的总数。
- public int PagesRemain
- {
- get
- {
- return PageCount - CurrentPageIndex;
- }
- }
- // 获取或设置每页显示的项数。
- public int PageSize
- {
- get
- {
- object obj = ViewState["PageSize"];
- if (obj == null)
- {
- obj = size;
- }
- return (obj == null) ? size : (int)obj;
- }
- set
- {
- int pageSize = value;
- if (Math.Abs(pageSize) == 0)
- pageSize = size;
- ViewState["PageSize"] = pageSize;
- }
- }
- // 获取在当前页之后还未显示的剩余记录的项数。
- public int RecordsRemain
- {
- get
- {
- if (CurrentPageIndex < PageCount)
- {
- return RecordCount - (CurrentPageIndex * PageSize);
- }
- else
- {
- return 0;
- }
- }
- }
- // 获取所有要分页的记录需要的总页数。
- public int PageCount
- {
- get { return (RecordCount > 0) ? (int)Math.Ceiling((double)RecordCount / (double)PageSize) : 1; }
- }
- public int XRecord
- {
- get
- {
- return int.Parse(System.Configuration.ConfigurationSettings.AppSettings["XRecord"].Trim());
- }
- }
- #endregion Property
- }