简单Asp.Net分页控件

 

因为项目中要用, 而AspNetPager又必须要用商业授权, 所以干脆自己先写个用了.

[DefaultProperty("Text")]
[ToolboxData("<{0}:WebFormsPager runat=server></{0}:WebFormsPager>")]
public class WebFormsPager : WebControl, IPostBackEventHandler
{
    #region Property
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("10")]
    [Localizable(true)]
    public int PageSize
    {
        get
        {
            int pageSize = 0;
            object o = ViewState["PageSize"];
            if (o == null)
                pageSize = 1;
            else
                int.TryParse(o.ToString(), out pageSize);
            return pageSize;
        }

        set
        {
            ViewState["PageSize"] = value;
        }
    }

    [DefaultValue("1")]
    public int CurrentPageIndex
    {
        get
        {
            int curPageIndex = 0;
            object o = ViewState["CurrentPageIndex"];
            if (o == null)
                curPageIndex = 1;
            else
                int.TryParse(o.ToString(), out curPageIndex);
            return curPageIndex;
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }

    public int RecordCount
    {
        get
        {
            int curPageIndex = 0;
            object o = ViewState["RecordCount"];
            if (o == null)
                curPageIndex = 1;
            else
                int.TryParse(o.ToString(), out curPageIndex);
            return curPageIndex;
        }

        set
        {
            ViewState["RecordCount"] = value;
        }
    }
    public int PageCount
    {
        get
        {

            return (RecordCount / PageSize) + (RecordCount % PageSize > 0 ? 1 : 0);
        }
    }
    #endregion

    private static readonly object ClickKey = new object();

    public event EventHandler PageChanged
    {
        add
        {
            Events.AddHandler(ClickKey, value);
        }
        remove
        {
            Events.RemoveHandler(ClickKey, value);
        }
    }

    protected virtual void OnClick(EventArgs e)
    {
        EventHandler clickEventDelegate = (EventHandler)Events[ClickKey];
        if (clickEventDelegate != null)
        {
            clickEventDelegate(this, e);
        }
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        if (this.CurrentPageIndex != 1)
        {
            AddHypeLink(output, "<<", LinkButtonDescriminate.FirstButton);
            AddHypeLink(output, "<", LinkButtonDescriminate.PreviousButton);
        }

        if (this.CurrentPageIndex != this.PageCount)
        {
            AddHypeLink(output, ">", LinkButtonDescriminate.NextButton);
            AddHypeLink(output, ">>", LinkButtonDescriminate.LastButton);
        }

        output.Write(string.Format("{0}/{1}", this.CurrentPageIndex, this.PageCount));
    }

    void AddHypeLink(HtmlTextWriter output, string linkName, LinkButtonDescriminate discriminate)
    {
        output.Write(string.Format("<a href=\"{0}\" title=\"Move to {2} page\">&nbsp;&nbsp;&nbsp;{1}&nbsp;&nbsp;&nbsp;</a>", Page.ClientScript.GetPostBackClientHyperlink(this, discriminate.ToString()), linkName, discriminate.ToString().Replace("Button", string.Empty)));
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        LinkButtonDescriminate eventArg = LinkButtonDescriminate.DefaultValue;
        Enum.TryParse(eventArgument, out eventArg);
        switch (eventArg)
        {
            case LinkButtonDescriminate.FirstButton:
                this.CurrentPageIndex = 1;
                break;
            case LinkButtonDescriminate.PreviousButton:
                this.CurrentPageIndex--;
                break;
            case LinkButtonDescriminate.NextButton:
                this.CurrentPageIndex++;
                break;
            case LinkButtonDescriminate.LastButton:
                this.CurrentPageIndex = this.PageCount;
                break;
            default:
                break;
        }
        OnClick(EventArgs.Empty);
    }
}

public enum LinkButtonDescriminate
{
    DefaultValue,
    FirstButton,
    PreviousButton,
    NextButton,
    LastButton
}

调用方式:

前台代码

<%@ Register Assembly="DemoControl" Namespace="DemoControl" TagPrefix="cc1" %>

 

<cc1:WebFormsPager ID="WebFormsPager1" runat="server" PageSize="10"
         onpagechanged="WebFormsPager1_PageChanged" />

后台代码

private void BindGridView()
{
    this.WebFormsPager1.RecordCount = MockData.GetData().Count;
    this.gvTest.DataSource = MockData.GetData().Skip(this.WebFormsPager1.PageSize * (this.WebFormsPager1.CurrentPageIndex - 1)).Take(this.WebFormsPager1.PageSize);
    this.gvTest.DataBind();
}

protected void WebFormsPager1_PageChanged(object sender, EventArgs e)
{
    BindGridView();
}

因为时间的原因,文章未排版。

 

参考文章: http://www.cnblogs.com/clingingboy/archive/2006/08/02/466180.html

posted @ 2011-08-30 21:57  DukeCheng  阅读(521)  评论(1编辑  收藏  举报