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

 更新时间:2014年01月28日 10:30:56   转载 作者:  
这篇文章主要介绍了asp.net自定义分页控件示例,大家参考使用吧

一、.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);
        }
    }
}

相关文章

  • ASP.NET MVC学习教程之Razor语法

    ASP.NET MVC学习教程之Razor语法

    这篇文章主要给大家介绍了关于ASP.NET MVC学习教程之Razor语法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • 在ASP.NET中重写URL的代码

    在ASP.NET中重写URL的代码

    在ASP.NET中重写URL的代码...
    2007-03-03
  • 浅析.net策略模式

    浅析.net策略模式

    这篇文章主要介绍了浅析.net策略模式,需要的朋友可以参考下
    2014-12-12
  • Asp.Net 程序错误Runtime Error原因与解决

    Asp.Net 程序错误Runtime Error原因与解决

    提示这个,不管怎么改配置文件的设置都不行,下面是修正方法,大家可以试试。
    2010-03-03
  • 无法将类型为“System.Web.UI.WebControls.HiddenField”的对象强制转换为类型的错误的解决

    无法将类型为“System.Web.UI.WebControls.HiddenField”的对象强

    无法将类型为“System.Web.UI.WebControls.HiddenField”的对象强制转换为类型的错误的解决方法
    2011-12-12
  • asp.net中资源文件的使用

    asp.net中资源文件的使用

    .Net是一个丰富的平台,在它的结构中,程序集Assembly是自我描述的安装单元,它可以只包括一个PE(可移植可执行)格式的Dll或exe文件,也可以由多个文件组成,例如资源、Dll和exe等
    2011-12-12
  • asp.net web.config加密解密方法

    asp.net web.config加密解密方法

    你还能够使用aspnet_regiis.exe命令行工具来加密和解密Web.config文件配置部分,你可以在"%WINDOWSDIR%\Microsoft.Net\Framework\version"目录下找到这个工具。
    2009-11-11
  • 网页(aspx)与用户控件(ascx)交互逻辑处理实现

    网页(aspx)与用户控件(ascx)交互逻辑处理实现

    为了以后好维护,把几个页面(ASPX)相同的部分抽取放在一个用户控件(ASCX)上,现在把逻辑分享下,感兴趣的各位可以参考下哈
    2013-03-03
  • ASP.NET中repeater嵌套实现代码(附源码)

    ASP.NET中repeater嵌套实现代码(附源码)

    repeater嵌套经常会在一些特殊效果显示下会用到,新手朋友们可以详细看下本文,希望对你有所帮助,代码很整洁同时附有源码
    2013-03-03
  • VS2015正式版安装教程

    VS2015正式版安装教程

    这篇文章主要为大家详细介绍了VS2015正式版安装教程,初次体验VS2015正式版,安装详细过程分给大家,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12

最新评论