主页 > 知识库 > asp.net自定义分页控件示例

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

热门标签:400电话办理信任翰诺科技 怎样给陕西地图标注颜色 广州销售外呼系统定制 电销机器人 数据 地图标注多少钱一张 云狐人工智能电话机器人 宿迁智能外呼系统排名 福州人工智能电销机器人加盟 ai电销机器人对贷款有帮助吗

一、.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>=48event.keyCode=57)
                    ||(event.keyCode>=96event.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 webform自定义分页控件
  • asp.net分页控件使用详解【附实例下载】
  • 解析asp.net的分页控件
  • 关于asp.net 自定义分页控件
  • asp.net中使用自定义控件的方式实现一个分页控件的代码
  • 分享一个asp.net pager分页控件
  • asp.net分页控件AspNetPager的样式美化
  • asp.net下Repeater使用 AspNetPager分页控件
  • AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
  • asp.net web页面自定义分页控件使用详解

标签:黄南 绵阳 焦作 大兴安岭 曲靖 新疆 宜春 延安

巨人网络通讯声明:本文标题《asp.net自定义分页控件示例》,本文关键词  asp.net,自定义,分页,控件,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《asp.net自定义分页控件示例》相关的同类信息!
  • 本页收集关于asp.net自定义分页控件示例的相关信息资讯供网民参考!
  • 推荐文章