Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Tired of Hibernate? Try JDBI in your code JDBI Quick sample ICategoryDAO.java : create a data access interface (implement is not required) package com.prhythm.erotic.task.data....
Stats
ServerControl : GridView Pager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Bruce.Web.Controls
{
    /// <summary>
    /// GridViewPager 的摘要描述
    /// </summary>
    [ToolboxData("<{0}:GridViewPager ID=\"GridViewPager\" runat=\"server\"></{0}:GridViewPager>")]
    public class GridViewPager : Control
    {
        #region 按鈕文字
        private string _FirstPageText = "第一頁";
        public string FirstPageText
        {
            get { return _FirstPageText; }
            set { _FirstPageText = value; }
        }
        private string _PreviousPageText = "上一頁";
        public string PreviousPageText
        {
            get { return _PreviousPageText; }
            set { _PreviousPageText = value; }
        }
        private string _NextPageText = "下一頁";
        public string NextPageText
        {
            get { return _NextPageText; }
            set { _NextPageText = value; }
        }
        private string _LastPageText = "最後頁";
        public string LastPageText
        {
            get { return _LastPageText; }
            set { _LastPageText = value; }
        }
        #endregion

        /// <summary>
        /// 使用圖示按鈕
        /// </summary>
        private bool _IsImageButton;
        public bool IsImageButton
        {
            get { return _IsImageButton; }
            set { _IsImageButton = value; }
        }

        #region 按鈕圖示
        private string _FirstPageImageUrl;
        public string FirstPageImageUrl
        {
            get { return _FirstPageImageUrl; }
            set { _FirstPageImageUrl = value; }
        }
        private string _PreviousPageImageUrl;
        public string PreviousPageImageUrl
        {
            get { return _PreviousPageImageUrl; }
            set { _PreviousPageImageUrl = value; }
        }
        private string _NextPageImageUrl;
        public string NextPageImageUrl
        {
            get { return _NextPageImageUrl; }
            set { _NextPageImageUrl = value; }
        }
        private string _LastPageImageUrl;
        public string LastPageImageUrl
        {
            get { return _LastPageImageUrl; }
            set { _LastPageImageUrl = value; }
        }
        #endregion

        /// <summary>
        /// 使用下拉選單
        /// </summary>
        private bool _IsDropDownList;
        public bool IsDropDownList
        {
            get { return _IsDropDownList; }
            set { _IsDropDownList = value; }
        }

        /// <summary>
        /// 顯示頁碼個數
        /// </summary>
        private int _PagerRange = 3;
        public int PagerRange
        {
            get { return _PagerRange; }
            set
            {
                if (value < 1)
                {
                    _PagerRange = 1;
                }
                else if (value > 10)
                {
                    _PagerRange = 10;
                }
                else
                {
                    _PagerRange = value;
                }
            }
        }

        /// <summary>
        /// 頁碼間隔
        /// </summary>
        private string _PagerPadding = "3px";
        public string PagerPadding
        {
            get { return _PagerPadding; }
            set { _PagerPadding = value; }
        }

        /// <summary>
        /// 文字顏色
        /// </summary>
        private Color _ForeColor;
        public Color ForeColor
        {
            get { return _ForeColor; }
            set { _ForeColor = value; }
        }

        private Control indexer = new Control();
        private GridView container;

        protected override void OnLoad(EventArgs e)
        {
            if (this.CheckContainer())
            {
                this.Controls.Clear();
                #region 設定按鈕屬性
                if (IsImageButton)
                {
                    ImageButton btnFirstPage = CreateImageButton("btnFirstPage",
                        FirstPageText,
                        "0",
                        container.PageIndex > 0,
                        this.FirstPageImageUrl);
                    ImageButton btnPreviousPage = CreateImageButton("btnPreviousPage",
                        PreviousPageText,
                        (container.PageIndex - 1).ToString(),
                        container.PageIndex - 1 >= 0,
                        this.PreviousPageImageUrl);
                    ImageButton btnNextPage = CreateImageButton("btnNextPage",
                        NextPageText,
                        (container.PageIndex + 1).ToString(),
                        container.PageIndex + 1 < container.PageCount,
                        this.NextPageImageUrl);
                    ImageButton btnLastPage = CreateImageButton("btnLastPage",
                        LastPageText,
                        (container.PageCount - 1).ToString(),
                        container.PageIndex + 1 < container.PageCount,
                        this.LastPageImageUrl);

                    this.Controls.Add(btnFirstPage);
                    this.Controls.Add(btnPreviousPage);

                    this.Controls.Add(this.indexer);

                    this.Controls.Add(btnNextPage);
                    this.Controls.Add(btnLastPage);
                }
                else
                {
                    LinkButton btnFirstPage = CreatePageButton("btnFirstPage",
                        FirstPageText,
                        "0",
                        container.PageIndex > 0);
                    LinkButton btnPreviousPage = CreatePageButton("btnPreviousPage",
                        PreviousPageText,
                        (container.PageIndex - 1).ToString(),
                        container.PageIndex - 1 >= 0);
                    LinkButton btnNextPage = CreatePageButton("btnNextPage",
                        NextPageText,
                        (container.PageIndex + 1).ToString(),
                        container.PageIndex + 1 < container.PageCount);
                    LinkButton btnLastPage = CreatePageButton("btnLastPage",
                        LastPageText,
                        (container.PageCount - 1).ToString(),
                        container.PageIndex + 1 < container.PageCount);

                    this.Controls.Add(btnFirstPage);
                    this.Controls.Add(btnPreviousPage);

                    this.Controls.Add(this.indexer);

                    this.Controls.Add(btnNextPage);
                    this.Controls.Add(btnLastPage);
                }
                #endregion

                if (IsDropDownList)
                    this.RenderDrowDownListPager();
                else
                    this.RenderPageNumbers();
            }
            base.OnLoad(e);
        }

        /// <summary>
        /// 位置錯誤時顯示錯誤訊息
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.CheckContainer())
            {
                this.Controls.Clear();
                Literal warming = new Literal();
                warming.Text = "<span style=\"color:red;\">Pager object must be in a GridView.<span>";
                this.Controls.Add(warming);
            }
            base.OnPreRender(e);
        }

        /// <summary>
        /// 檢查container
        /// </summary>
        /// <returns></returns>
        private bool CheckContainer()
        {
            if (container == null)
            {
                Control control = this;

                while (control.Parent != null && !(control.Parent is GridView))
                {
                    control = control.Parent;

                }

                if (control.Parent != null)
                {
                    container = control.Parent as GridView;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 建立圖形按鈕
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="AlternateText"></param>
        /// <param name="CommandArgument"></param>
        /// <param name="Visible"></param>
        /// <param name="ImageUrl"></param>
        /// <returns></returns>
        private ImageButton CreateImageButton(string ID, string AlternateText, string CommandArgument, bool Visible, string ImageUrl)
        {
            ImageButton btn = new ImageButton();
            btn.ID = ID;
            btn.AlternateText = AlternateText;
            btn.CommandArgument = CommandArgument;
            btn.ImageUrl = ImageUrl;
            btn.Visible = Visible;
            btn.ImageAlign = ImageAlign.Middle;
            btn.Click += PageButton_Click;
            btn.Style["margin-left"] = this.PagerPadding;
            btn.Style["margin-right"] = this.PagerPadding;
            return btn;
        }

        /// <summary>
        /// 建立文字按鈕
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Text"></param>
        /// <param name="CommandArgument"></param>
        /// <param name="Visible"></param>
        /// <returns></returns>
        private LinkButton CreatePageButton(string ID, string Text, string CommandArgument, bool Visible)
        {
            LinkButton btn = new LinkButton();
            btn.ForeColor = this.ForeColor;
            btn.ID = ID;
            btn.Text = Text;
            btn.CommandArgument = CommandArgument;
            btn.Visible = Visible;
            btn.Click += PageButton_Click;
            btn.Style["margin-left"] = this.PagerPadding;
            btn.Style["margin-right"] = this.PagerPadding;
            return btn;
        }

        /// <summary>
        /// 處理頁碼
        /// </summary>
        private void RenderPageNumbers()
        {
            int start = Math.Max(0, this.container.PageIndex - this.PagerRange);
            int end = Math.Min(this.container.PageCount - 1, this.container.PageIndex + this.PagerRange);
            for (int i = start; i <= end; i++)
            {
                if (i == this.container.PageIndex)
                {
                    Literal currentPage = new Literal();
                    currentPage.Text = (i + 1).ToString();
                    this.indexer.Controls.Add(currentPage);
                }
                else
                {
                    this.indexer.Controls.Add(CreatePageButton("p" + i, (i + 1).ToString(), i.ToString(), true));
                }
            }
        }

        /// <summary>
        /// 處理下拉選單
        /// </summary>
        private void RenderDrowDownListPager()
        {
            DropDownList ddl = this.indexer.FindControl("ddlPageSelector") as DropDownList;
            if (ddl == null)
            {
                ddl = new DropDownList();
                ddl.ID = "ddlPageSelector";
                ddl.AutoPostBack = true;
                ddl.SelectedIndexChanged += PageList_Select;

                Literal txt = new Literal();
                txt.Text = "第";
                this.indexer.Controls.Add(txt);
                this.indexer.Controls.Add(ddl);
                txt = new Literal();
                txt.Text = "頁";
                this.indexer.Controls.Add(txt);
            }

            if (ddl.Items.Count == 0)
            {
                for (int i = 0; i < this.container.PageCount; i++)
                {
                    ddl.Items.Add(new ListItem((i + 1).ToString(), i.ToString()));
                }
            }
            ddl.SelectedValue = this.container.PageIndex.ToString();
        }

        /// <summary>
        /// 換頁動作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PageButton_Click(object sender, EventArgs e)
        {
            if (this.CheckContainer())
            {

                LinkButton btn = sender as LinkButton;
                if (btn != null)
                    this.container.PageIndex = int.Parse(btn.CommandArgument);

                ImageButton ibtn = sender as ImageButton;
                if (ibtn != null)
                    this.container.PageIndex = int.Parse(ibtn.CommandArgument);
            }
        }
        /// <summary>
        /// 換頁動作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PageList_Select(object sender, EventArgs e)
        {
            if (this.CheckContainer())
            {
                DropDownList ddl = sender as DropDownList;
                this.container.PageIndex = int.Parse(ddl.SelectedValue);
            }
        }
    }
}