asp.Netカスタムページングコントロールの例

8562 ワード

一、ascxページ
 
  



    ���� �

、.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;

        ///


        /// � GridView
        ///

        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";
            }
        }

        ///


        /// �O �
        ///

        /// DataSet �o����

        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", "alert('�Σ ��� ��^ ��!');");
                    }
                }
                catch
                {

                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "alert('�Σ � � � �!');");
                }
            }
            PageButtonClick(sender, e);
        }
    }
}