溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在asp.net中自定義一個分頁控件

發(fā)布時間:2021-04-15 17:30:17 來源:億速云 閱讀:120 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在asp.net中自定義一個分頁控件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、.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" >
    記錄總數(shù):<asp:Label ID="LRecords" runat="server"></asp:Label>
    總頁數(shù):<asp:Label ID="LPages" runat="server"></asp:Label>
    當(dāng)前頁:<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>
    轉(zhuǎn)到第<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)            {              
            }            // 只輸入數(shù)字            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的參數(shù)值            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>        /// 設(shè)定頁面信息        /// </summary>        /// <param name="dsCount">DataSet的紀(jì)錄總數(shù)</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('對不起,頁數(shù)超過索引范圍!');</script>");                    }                }                catch                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<script. language='javascript'>alert('對不起,只能輸入數(shù)字!');</script>");                }            }            PageButtonClick(sender, e);        }    }}

上述就是小編為大家分享的如何在asp.net中自定義一個分頁控件了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI