【twfx118 】Asp.net分頁控制元件

iDotNetSpace發表於2008-07-23
建立一個使用者控制元件:Pager.ascx,然後後臺程式碼修改為:
  1public partial class Pager : System.Web.UI.UserControl
  2    {
  3        private string _UrlFormat;
  4        private int _PageSize = 10;
  5        private int _RecordCount;
  6        private int _PageCount = 5;
  7
  8        ///
  9        /// 連線格式
 10        ///

 11        public string UrlFormat
 12        {
 13            get
 14            {
 15                return _UrlFormat;
 16            }
 17            set
 18            {
 19                _UrlFormat = value;
 20            }
 21        }
 22
 23        ///
 24        /// 頁長度
 25
 26
 27        ///

 28        public int PageSize
 29        {
 30            get
 31            {
 32                return _PageSize;
 33            }
 34            set
 35            {
 36                _PageSize = value;
 37            }
 38        }
 39
 40        ///
 41        /// 當前頁碼
 42        ///

 43        public int PageIndex
 44        {
 45            get
 46            {
 47                string Pageindex = HttpContext.Current.Request.QueryString["i"];
 48                if (Pageindex != null)
 49                {
 50                    return int.Parse(Pageindex);
 51                }
 52                return 1;
 53            }
 54        }
 55
 56        ///
 57        /// 總記錄數
 58        ///

 59        public int RecordCount
 60        {
 61            get
 62            {
 63                return _RecordCount;
 64            }
 65            set
 66            {
 67                _RecordCount = value;
 68            }
 69        }
 70
 71        ///
 72        /// 兩邊顯示個數
 73        ///

 74        public int PageCount
 75        {
 76            get
 77            {
 78                return _PageCount;
 79            }
 80            set
 81            {
 82                _PageCount = value;
 83            }
 84        }
 85
 86        protected override void Render(HtmlTextWriter writer)
 87        {
 88            if (RecordCount == 0)
 89                return;
 90            int SumPage = (RecordCount + PageSize - 1) / PageSize;
 91
 92            int start = PageIndex - PageCount;
 93            int end = PageIndex + PageCount;
 94
 95            //以PageIndex為中心,前後個顯示Page個頁碼導航
 96
 97
 98            if (SumPage > (PageCount * 2 + 1))
 99            {
100                if (start < 1)
101                {
102                    start = 1;
103                    end = start + 10;
104                }
105                else if (end > SumPage)
106                {
107                    start = SumPage - 10;
108                    end = SumPage;
109                }
110            }
111            else
112            {
113                start = 1;
114                end = SumPage;
115            }
116
117
118
119            string tmp = "[{0}]";
120            StringBuilder sb = new StringBuilder(string.Format("頁次:{0}/{1}  每頁:{2}  共計:{3} 條 ", PageIndex, SumPage, PageSize, RecordCount));
121            if (PageIndex > 1)
122            {
123                sb.Append(string.Format("首頁 ", 1));
124                sb.Append(string.Format(" 上一頁 ", PageIndex - 1));
125            }
126            for (int i = start; i <= end; i++)
127            {
128                if (i == PageIndex)
129                {
130                    sb.Append(" " + PageIndex.ToString() + " ");
131                }
132                else
133                {
134                    sb.Append(string.Format(tmp, i));
135                }
136                sb.Append(" ");
137            }
138            if (PageIndex < SumPage)
139            {
140                sb.Append(string.Format(" 下一頁 ", PageIndex + 1));
141                sb.Append(string.Format(" 尾頁", SumPage));
142            }
143            writer.Write(sb.ToString());
144        }
145        protected void Page_Load(object sender, EventArgs e)
146        {
147
148        }
149    }使用方法:
把Pager拖拽到頁面上,進入頁面後臺程式碼,設定如下:
 
1            Pager1.UrlFormat = "?i={0}";//分頁格式
2            int recordcount, pagecount;
3            Repeater1.DataSource = 資料來源;
4            Repeater1.DataBind();
5            Pager1.RecordCount = recordcount;
6

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-407072/,如需轉載,請註明出處,否則將追究法律責任。

相關文章