• 不错的分页类


    View Code
    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Web.UI;
    using System.Globalization;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace Demo
    {
    public interface IPageIndexChangedEventHandler
    {
    void BindData(int pageIndex, int pageSize);
    }

    public class AjaxPager : Control, ICallbackEventHandler, INamingContainer
    {
    #region Member Variables

    private int lowerBound;
    private int upperBound;
    private HyperLink previousLink;
    private HyperLink nextLink;
    private HyperLink firstLink;
    private HyperLink lastLink;
    private Control targetControl;

    #endregion

    #region Public Properties

    public int PageIndex
    {
    get
    {
    int pageIndex = 1;
    if (Page != null && Page.IsPostBack && ViewState["PageIndex"] != null)
    {
    pageIndex
    = (int)ViewState["PageIndex"];
    }
    if (pageIndex < 1)
    {
    return 1;
    }
    else
    {
    return pageIndex;
    }
    }
    set
    {
    ViewState[
    "PageIndex"] = value;
    }
    }
    public int PageSize
    {
    get
    {
    int pageSize = Convert.ToInt32(ViewState["PageSize"]);
    if (pageSize <= 0)
    {
    return 10;
    }
    return pageSize;
    }
    set
    {
    ViewState[
    "PageSize"] = value;
    }
    }
    public int LeftLength
    {
    get
    {
    return PageLength / 2;
    }
    }
    public int RightLength
    {
    get
    {
    return PageLength / 2;
    }
    }
    public int PageLength
    {
    get
    {
    int pageLength = Convert.ToInt32(ViewState["PageLength"]);

    if (pageLength <= 3)
    return 3;

    if (pageLength % 2 == 0)
    {
    return pageLength + 1;
    }

    return pageLength;
    }
    set
    {
    ViewState[
    "PageLength"] = value;
    }

    }
    public int TotalPages
    {
    get
    {
    return CalculateTotalPages(TotalRecords);
    }
    }
    public int TotalRecords
    {
    get
    {
    return Convert.ToInt32(ViewState["TotalRecords"]);
    }
    set
    {
    ViewState[
    "TotalRecords"] = value;
    }
    }
    public string DataWrapperID
    {
    get
    {
    string s = ViewState["DataWrapperID"] as string;
    if (s == null)
    {
    s
    = string.Empty;
    }
    return s;
    }
    set
    {
    ViewState[
    "DataWrapperID"] = value;
    }
    }
    public string TargetControlID
    {
    get
    {
    string s = ViewState["TargetControlID"] as string;
    if (s == null)
    {
    s
    = string.Empty;
    }
    return s;
    }
    set
    {
    ViewState[
    "TargetControlID"] = value;
    }
    }

    #endregion

    #region Overrides Methods

    protected override void OnLoad(EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    CreatePageLinks();
    RegisterClientScripts();
    }
    else
    {
    targetControl
    = FindControl(this, TargetControlID);
    }
    }
    protected override void Render(HtmlTextWriter writer)
    {
    if (TotalPages <= 1)
    {
    return;
    }
    RenderFirst(writer);
    RenderPrevious(writer);
    RenderPagingButtons(writer);
    RenderNext(writer);
    RenderLast(writer);
    }

    #endregion

    #region Public Methods

    public virtual int CalculateTotalPages(int totalRecords)
    {
    int totalPagesAvailable;

    if (totalRecords == 0)
    {
    return 0;
    }

    totalPagesAvailable
    = totalRecords / PageSize;

    if ((totalRecords % PageSize) > 0)
    {
    totalPagesAvailable
    ++;
    }

    return totalPagesAvailable;

    }

    #endregion

    #region ICallbackEventHandler Implementation

    public void RaiseCallbackEvent(string pageIndex)
    {
    int currentPageIndex = int.Parse(pageIndex);
    if (currentPageIndex != PageIndex)
    {
    ((IPageIndexChangedEventHandler)targetControl).BindData(currentPageIndex, PageSize);
    }
    PageIndex
    = currentPageIndex;
    CreatePageLinks();
    }
    public string GetCallbackResult()
    {
    return RenderControl(targetControl);
    }

    #endregion

    #region Helpers

    private void RenderFirst(HtmlTextWriter writer)
    {
    if ((PageIndex > LeftLength + 1) && (TotalPages > PageLength))
    {
    firstLink.RenderControl(writer);
    LiteralControl l
    = new LiteralControl("&nbsp;...&nbsp;");
    l.RenderControl(writer);
    }
    }
    private void RenderLast(HtmlTextWriter writer)
    {
    if (((PageIndex + RightLength) < TotalPages) && (TotalPages > PageLength))
    {
    LiteralControl l
    = new LiteralControl("&nbsp;...&nbsp;");
    l.RenderControl(writer);
    lastLink.RenderControl(writer);
    }
    }
    private void RenderPrevious(HtmlTextWriter writer)
    {
    if (PageIndex > 1)
    {
    previousLink.RenderControl(writer);
    Literal l
    = new Literal();
    l.Text
    = "&nbsp;";
    l.RenderControl(writer);
    }

    }
    private void RenderNext(HtmlTextWriter writer)
    {
    if (PageIndex < TotalPages)
    {
    Literal l
    = new Literal();
    l.Text
    = "&nbsp;";
    l.RenderControl(writer);
    nextLink.RenderControl(writer);
    }
    }
    private void RenderButtonRange(int start, int end, HtmlTextWriter writer)
    {
    for (int i = start; i <= end; i++)
    {
    if (PageIndex == i)
    {
    Literal l
    = new Literal();
    //l.Text = "<span class=\"currentPage\">[" + i.ToString() + "]</span>";
    l.Text = i.ToString();
    l.RenderControl(writer);
    }
    else
    {
    Controls[i
    - start].RenderControl(writer);
    }
    if (i < end)
    {
    writer.Write(
    " ");
    }
    }

    }
    private void RenderPagingButtons(HtmlTextWriter writer)
    {
    RenderButtonRange(lowerBound, upperBound, writer);
    }
    private string RenderControl(Control control)
    {
    StringWriter writer1
    = new StringWriter(CultureInfo.InvariantCulture);
    HtmlTextWriter writer2
    = new HtmlTextWriter(writer1);

    control.RenderControl(writer2);
    writer2.Flush();
    writer2.Close();

    return writer1.ToString();
    }
    private string CreateOnClickScript(int pageIndex)
    {
    return string.Format("javascript:turnPage({0});", pageIndex);
    }
    private void InitPageBounds()
    {
    lowerBound
    = PageIndex - LeftLength;
    upperBound
    = PageIndex + RightLength;

    if (lowerBound < 1)
    {
    upperBound
    += 1 - lowerBound;
    lowerBound
    = 1;
    }
    if (upperBound > TotalPages)
    {
    lowerBound
    -= upperBound - TotalPages;
    upperBound
    = TotalPages;
    }
    if (lowerBound < 1)
    {
    lowerBound
    = 1;
    }
    if (upperBound > TotalPages)
    {
    upperBound
    = TotalPages;
    }
    }
    private void AddPageLinks()
    {
    InitPageBounds();

    HyperLink link;
    for (int i = lowerBound; i <= upperBound; i++)
    {
    link
    = new HyperLink();
    link.EnableViewState
    = false;
    link.Text
    = i.ToString();
    link.ID
    = i.ToString();
    link.NavigateUrl
    = CreateOnClickScript(i);
    Controls.Add(link);
    }
    }
    private void AddFirstLastLinks()
    {
    firstLink
    = new HyperLink();
    firstLink.ID
    = "First";
    firstLink.Text
    = "第一页";
    firstLink.NavigateUrl
    = CreateOnClickScript(1);
    Controls.Add(firstLink);

    lastLink
    = new HyperLink();
    lastLink.ID
    = "Last";
    lastLink.Text
    = "最后一页";
    lastLink.NavigateUrl
    = CreateOnClickScript(TotalPages);
    Controls.Add(lastLink);
    }
    private void AddPreviousNextLinks()
    {
    previousLink
    = new HyperLink();
    previousLink.ID
    = "Prev";
    previousLink.Text
    = "上一页";
    previousLink.NavigateUrl
    = CreateOnClickScript(PageIndex - 1);
    Controls.Add(previousLink);

    nextLink
    = new HyperLink();
    nextLink.ID
    = "Next";
    nextLink.Text
    = "下一页";
    nextLink.NavigateUrl
    = CreateOnClickScript(PageIndex + 1);
    Controls.Add(nextLink);
    }
    private void CreatePageLinks()
    {
    Controls.Clear();
    AddPageLinks();
    AddPreviousNextLinks();
    AddFirstLastLinks();
    }
    private void RegisterClientScripts()
    {
    string turnPageScript = "function turnPage(pageIndex) { CallServer(pageIndex,'" + DataWrapperID + "');}";
    string receiveCallBackScript = "function ReceiveCallback(arg,context){ document.getElementById(context).innerHTML = arg; }";
    string callbackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveCallback", "context", false);

    Page.ClientScript.RegisterClientScriptBlock(
    this.GetType(), this.ClientID + "TurnPageScript", turnPageScript, true);
    Page.ClientScript.RegisterClientScriptBlock(
    this.GetType(), this.ClientID + "ReceiveCallBackScript", receiveCallBackScript, true);
    Page.ClientScript.RegisterClientScriptBlock(
    this.GetType(), this.ClientID + "CallServer", string.Format("function CallServer(arg,context){{ {0} }}", callbackReference), true);
    }

    static Control FindControl(Control container, string id)
    {
    if ((container == null) || string.IsNullOrEmpty(id))
    {
    return null;
    }
    if (!(container is INamingContainer) && (container.NamingContainer != null))
    {
    container
    = container.NamingContainer;
    }
    Control control
    = FindControlInternal(container, id, null);
    if (control == null)
    {
    Dictionary
    <Control, bool> dictionary = new Dictionary<Control, bool>();
    dictionary[container]
    = true;
    container
    = container.NamingContainer;
    while ((container != null) && (control == null))
    {
    control
    = FindControlInternal(container, id, dictionary);
    dictionary[container]
    = true;
    container
    = container.NamingContainer;
    }
    }
    return control;
    }
    static Control FindControlInternal(Control control1, string text1, Dictionary<Control, bool> dictionary1)
    {
    if (control1.ID == text1)
    {
    return control1;
    }
    Control control
    = control1.FindControl(text1);
    if ((control != null) && (control.ID == text1))
    {
    return control;
    }
    foreach (Control control2 in control1.Controls)
    {
    if ((dictionary1 == null) || !dictionary1.ContainsKey(control2))
    {
    control
    = FindControlInternal(control2, text1, dictionary1);
    if (control != null)
    {
    return control;
    }
    }
    }
    return null;
    }


    #endregion
    }
    }

                    <nwap:AjaxPager ID="pager" PageLength="50" TargetControlID="StudentList" DataWrapperID="container" runat="server" PageSize="10" />

    View Code
    public List<Student> GetStudents(int pageIndex, int pageSize, ref int totalCount)
    {
    int lowerIndex = (pageIndex - 1) * pageSize + 1;
    int upperIndex = pageIndex * pageSize;

    List
    <Student> students = new List<Student>();
    OleDbConnection conn
    = new OleDbConnection();
    conn.ConnectionString
    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TDOWN\源码\ASP.NET AJAX无刷新分页演示包(含控件)\codefans.net\Demo\db.mdb;Jet OLEDB:Database Password=;Persist Security Info=False";
    OleDbCommand cmd
    = conn.CreateCommand();

    conn.Open();

    cmd.CommandText
    = "select count(*) from students";
    totalCount
    = (int)cmd.ExecuteScalar();

    cmd.CommandText
    = string.Format("select * from students where id >= {0} and id <= {1}", lowerIndex, upperIndex);
    OleDbDataReader reader
    = cmd.ExecuteReader();

    Student student
    = null;
    while (reader.Read())
    {
    student
    = new Student();
    student.Id
    = (int)reader[0];
    student.Name
    = (string)reader[1];
    student.Age
    = (int)reader[2];
    students.Add(student);
    }

    reader.Close();
    reader.Dispose();
    conn.Close();
    conn.Dispose();

    return students;
    }
  • 相关阅读:
    redis数据结构-list
    reids数据结构1-string
    jedis工具类
    静态资源放行
    SpringMVC拦截器
    xinetd
    Linux-open函数
    Linux简单的文件读取
    复习ssm02
    springMVC文件上传
  • 原文地址:https://www.cnblogs.com/Mr0909/p/2051479.html
Copyright © 2020-2023  润新知