欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net自定义分页控件示例

程序员文章站 2024-02-25 08:15:46
一、.ascx页面复制代码 代码如下:<%@ control language="c#" autoeventwireup="true" codebehind="pag...

一、.ascx页面

复制代码 代码如下:

<%@ control language="c#" autoeventwireup="true" codebehind="pagination.ascx.cs" inherits="iocs.web.usercontrol.pagination" %>
<link href="../content/css/pager.css" rel="stylesheet" type="text/css" />
<div id="tbpage" class="pager" runat="server" >
    記錄總數:<asp:label id="lrecords" runat="server"></asp:label>
    總頁數:<asp:label id="lpages" runat="server"></asp:label>
    當前頁:<asp:label id="lpage" runat="server"></asp:label>

        <asp:linkbutton id="linkfirst" runat="server" commandargument="first" nclick="pagerbuttonclick"
            text="首頁"></asp:linkbutton>

        <asp:linkbutton id="linkprevious" runat="server" commandargument="prev" nclick="pagerbuttonclick"
            text="上一頁"></asp:linkbutton>

        <asp:linkbutton id="linknext" runat="server" commandargument="next" nclick="pagerbuttonclick"
            text="下一頁"></asp:linkbutton>

        <asp:linkbutton id="linklast" runat="server" commandargument="last" nclick="pagerbuttonclick"
            text="末頁"></asp:linkbutton>
    轉到第<asp:textbox id="txtpage"  cssclass="piut" runat="server"  maxlength="5" autopostback="true" ntextchanged="txtpage_textchanged"></asp:textbox>頁

二、.ascx.cs文件

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;

namespace iocs.web.usercontrol
{
    public partial class pagination : system.web.ui.usercontrol
    {
        public event eventhandler pagebuttonclick;
        public bool firstpost = false;
        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
              

            }
            // 只輸入數字
            txtpage.attributes.add("onclick",
                @"if(!((event.keycode>=48&&event.keycode<=57)
                    ||(event.keycode>=96&&event.keycode<=105)
                    ||(event.keycode==8)))event.returnvalue=false;"
              );

        }
        gridview _gv;

        /// <summary>
        /// 需要分頁的gridview
        /// </summary>
        public gridview targetcontrolid
        {
            set
            {
                _gv = value;
            }
            get
            {
                return _gv;
            }
        }

        protected void pagerbuttonclick(object sender, eventargs e)
        {
            //獲得linkebutton的參數值
            string arg = ((linkbutton)sender).commandargument;
            switch (arg)
            {
                case ("next"):
                    {
                        if (_gv.pageindex < _gv.pagecount - 1)
                        {
                            _gv.pageindex=_gv.pageindex+1;
                        }
                        break;
                    }
                case ("prev"):
                    {
                        if (_gv.pageindex > 0)
                        {
                            _gv.pageindex--;
                        }
                        break;
                    }
                case ("first"):
                    {
                        _gv.pageindex = 0;
                        break;
                    }
                case ("last"):
                    {
                        if (_gv.pagecount > 0)
                        {
                            _gv.pageindex = _gv.pagecount - 1;
                        }
                        break;
                    }

                default:
                    {
                        _gv.pageindex = convert.toint32(arg);
                        break;
                    }
            }
            pagebuttonclick(sender, e);

        }

        

        public void setpagebutton()
        {
            if (_gv.pageindex == 0)
            {
                linkfirst.enabled = false;
                linkprevious.enabled = false;


                linkfirst.style["color"] = "gray";
                linkprevious.style["color"] = "gray";

                object s = linkfirst.style.keys;
                if (_gv.pagecount > 1)
                {
                    linknext.enabled = true;
                    linklast.enabled = true;
                    txtpage.enabled = true;
                    txtpage.enabled = true;
                    linknext.style["color"] = "#000";
                    linklast.style["color"] = "#000";
                    txtpage.style["readonly"] = "false";
                }
                else
                {
                    linknext.enabled = false;
                    linklast.enabled = false;
                    txtpage.enabled = false;
                    linknext.style["color"] = "gray";
                    linklast.style["color"] = "gray";
                    txtpage.style["readonly"] = "true";//background-color
                }
            }
            else if (_gv.pageindex == _gv.pagecount - 1)
            {
                linkfirst.enabled = true;
                linkprevious.enabled = true;
                linknext.enabled = false;
                linklast.enabled = false;
                linkfirst.style["color"] = "#000";
                linkprevious.style["color"] = "#000";
                linknext.style["color"] = "gray";
                linklast.style["color"] = "gray";
            }
            else
            {
                linkfirst.enabled = true;
                linkprevious.enabled = true;
                linknext.enabled = true;
                linklast.enabled = true;
                linkfirst.style["color"] = "#000";
                linkprevious.style["color"] = "#000";
                linknext.style["color"] = "#000";
                linklast.style["color"] = "#000";
            }
        }

        /// <summary>
        /// 設定頁面信息
        /// </summary>
        /// <param name="dscount">dataset的紀錄總數</param>

        public void setpagerecord(int dscount)
        {
            lrecords.text = dscount.tostring();
            int mod= dscount%_gv.pagesize;
            lpages.text = (mod == 0 ? dscount / _gv.pagesize : dscount / _gv.pagesize + 1).tostring();
            lpage.text = (_gv.pageindex + 1).tostring();
            tbpage.visible = true;
            setpagebutton();
        }

        protected void txtpage_textchanged(object sender, eventargs e)
        {
            if (txtpage.text != "")
            {
                try
                {
                    int index = int.parse(txtpage.text.trim());
                    if (index <= _gv.pagecount && index >= 1)
                    {
                        _gv.pageindex = index - 1;
                    }
                    else
                    {
                        page.clientscript.registerstartupscript(this.gettype(), "script", "<script. language='javascript'>alert('對不起,頁數超過索引范圍!');</script>");
                    }
                }
                catch
                {

                    page.clientscript.registerstartupscript(this.gettype(), "script", "<script. language='javascript'>alert('對不起,只能輸入數字!');</script>");
                }
            }
            pagebuttonclick(sender, e);
        }
    }
}