分页控件:

PageConten.ascx

 

 1<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageConten.ascx.cs" Inherits="test_01.PageConten" %>
 2 <br />
 3总共有 <%=row_count %> 页
 4<br />
 5总共有 <%=rows %> 行记录
 6<%=page_foot %>
 7<br />
 8当前是第 <%=pageIndex%> 页
 9<input type="hidden" id="hidSub" name="hidSub" value="0" />
10<script language="javascript" type="text/javascript">
11function selectPage()
12{
13    var id = document.getElementById("sltPage").value;
14    submit(id);
15}

16function bindSlt(row_count)
17{
18    for(var i=1;i<=row_count;i++)
19    {
20        document.getElementById("sltPage").options.add(new Option(i,i));
21    }

22}

23function submit(page)
24{
25    document.getElementById("hidSub").value = page;
26    document.forms[0].submit();
27}

28</script>

 

PageConten.ascx.cs

 

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using System.Text;
 12
 13namespace test_01
 14{
 15    public partial class PageConten : System.Web.UI.UserControl
 16    {
 17        public string page_foot;
 18        public int row_count = 0;
 19        public string pageIndex;
 20        public int page_info;  //每页要显示多少页码出来
 21        public int rows;  //总行数
 22        public int page_rows; //每页分多少行
 23        public int isRequest; //是否带url参数,1为带,2为不带
 24        public string pageName; //页面名称,去掉.aspx的部分
 25
 26        protected void Page_Load(object sender, EventArgs e)
 27        {
 28            row_count = rows / page_rows;
 29            int indexPage = 0//当前页
 30            if (IsPostBack)
 31            {
 32                indexPage = int.Parse("0" + Request["hidSub"]);
 33                if (indexPage == 0)
 34                {
 35                    pageIndex = "1";
 36                }

 37                else
 38                {
 39                    pageIndex = indexPage.ToString();
 40                }

 41                Session["index"= indexPage.ToString();
 42                PageInfo(indexPage);
 43            }

 44            else
 45            {
 46                if (isRequest == 1)
 47                {
 48                    indexPage = int.Parse("0" + Request["page"]);
 49                    if (indexPage == 0)
 50                    {
 51                        pageIndex = "1";
 52                    }

 53                    else
 54                    {
 55                        pageIndex = indexPage.ToString();
 56                    }

 57                    PageRequestInfo(indexPage);
 58                }

 59                else
 60                {
 61                    indexPage = int.Parse("0" + Request["hidSub"]);
 62                    if (indexPage == 0)
 63                    {
 64                        pageIndex = "1";
 65                    }

 66                    else
 67                    {
 68                        pageIndex = indexPage.ToString();
 69                    }

 70                    Session["index"= indexPage.ToString();
 71                    PageInfo(indexPage);
 72                }

 73            }

 74        }

 75
 76        /// <summary>
 77        /// 分页摸板方法1,不带url参数
 78        /// </summary>
 79        /// <param name="index_page">当前页</param>

 80        private void PageInfo(int index_page)
 81        {
 82            int back_page = index_page - 1;  //上一页
 83            int next_page = index_page + 1;  //下一页
 84            int last_page;     //页码能显示的最后一页
 85            int j;
 86            int r = page_info / 2;
 87            if (index_page == 0)
 88            {
 89                last_page = page_info;
 90                j = 1;
 91            }

 92            else
 93            {
 94                if (index_page > r)
 95                {
 96                    if (index_page > row_count - r)
 97                    {
 98                        last_page = row_count;
 99                    }

100                    else
101                    {
102                        last_page = index_page + r;
103                    }

104                }

105                else
106                {
107                    last_page = page_info;
108                }

109            }

110            j = last_page - (page_info - 1);
111            StringBuilder foot = new StringBuilder();
112            foot.Append("<table><tr><td>");
113            foot.Append("&nbsp;");
114            if (index_page != 1 && index_page != 0)
115            {
116                foot.Append("<a href='javascript:submit(1);' style='text-decoration:none'>[首页]</a>");
117                foot.Append("<a href='javascript:submit(" + back_page + ")' style='text-decoration:none'>[上一页]</a>");
118            }

119            else
120            {
121                foot.Append("[首页]");
122                foot.Append("[上一页]");
123            }

124            foot.Append("&nbsp;");
125            if (row_count > page_info)
126            {
127                for (int i = j; i <= last_page; i++)  //----------------------总页数大于10的情况
128                {
129                    if (i == 1 && index_page == 0)
130                    {
131                        foot.Append("[" + i + "]");
132                    }

133                    else
134                    {
135                        if (i == index_page)
136                        {
137                            foot.Append("[" + i + "]");
138                        }

139                        else
140                        {
141                            foot.Append("<a href='javascript:submit(" + i + ");' style='text-decoration:none'>[" + i + "]</a>");
142                        }

143                    }

144                    foot.Append("&nbsp;");
145                }

146            }

147            else
148            {
149                for (int i = 1; i <= row_count; i++)
150                {
151                    if (i == 1 && index_page == 0)
152                    {
153                        foot.Append("[" + i + "]");
154                    }

155                    else
156                    {
157                        if (i == index_page)
158                        {
159                            foot.Append("[" + i + "]");
160                        }

161                        else
162                        {
163                            foot.Append("<a href='javascript:submit(" + i + ");' style='text-decoration:none'>[" + i + "]</a>");
164                        }

165                    }

166                    foot.Append("&nbsp;");
167                }

168            }

169            if (index_page != row_count)
170            {
171                if (index_page == 0)
172                {
173                    foot.Append("<a href='javascript:submit(2);' style='text-decoration:none'>[下一页]</a>");
174                }

175                else
176                {
177                    foot.Append("<a href='javascript:submit(" + next_page + ");' style='text-decoration:none'>[下一页]</a>");
178                }

179                foot.Append("<a href='javascript:submit(" + row_count + ");' style='text-decoration:none'>[尾页]</a>");
180            }

181            else
182            {
183                foot.Append("[下一页]");
184                foot.Append("[尾页]");
185            }

186            foot.Append("&nbsp;");
187            foot.Append("转到<select id='sltPage' onchange='selectPage()'>");
188            for (int i = 1; i <= row_count; i++)
189            {
190                foot.Append("<option value=" + i + ">" + i + "</option>");
191            }

192            foot.Append("</select>");
193            foot.Append("</td>");
194            foot.Append("</tr></table>");
195            page_foot = foot.ToString();
196        }

197
198        /// <summary>
199        /// 分页摸板方法2,带url参数
200        /// </summary>
201        /// <param name="indexPage">当前页</param>

202        public void PageRequestInfo(int index_page)
203        {
204            int back_page = index_page - 1;  //上一页
205            int next_page = index_page + 1;  //下一页
206            int last_page;     //页码能显示的最后一页
207            int j;
208            int r = page_info / 2;
209            if (index_page == 0)
210            {
211                last_page = page_info;
212                j = 1;
213            }

214            else
215            {
216                if (index_page > r)
217                {
218                    if (index_page > row_count - r)
219                    {
220                        last_page = row_count;
221                    }

222                    else
223                    {
224                        last_page = index_page + r;
225                    }

226                }

227                else
228                {
229                    last_page = page_info;
230                }

231            }

232            j = last_page - (page_info - 1);
233            StringBuilder foot = new StringBuilder();
234            foot.Append("<table><tr><td>");
235            foot.Append("&nbsp;");
236            if (index_page != 1 && index_page != 0)
237            {
238                foot.Append("<a href='" + pageName + ".aspx?page=1' style='text-decoration:none'>[首页]</a>");
239                foot.Append("<a href='" + pageName + ".aspx?page=" + back_page + "' style='text-decoration:none'>[上一页]</a>");
240            }

241            else
242            {
243                foot.Append("[首页]");
244                foot.Append("[上一页]");
245            }

246            foot.Append("&nbsp;");
247            if (row_count > page_info)
248            {
249                for (int i = j; i <= last_page; i++)  //----------------------总页数大于10的情况
250                {
251                    if (i == 1 && index_page == 0)
252                    {
253                        foot.Append("[" + i + "]");
254                    }

255                    else
256                    {
257                        if (i == index_page)
258                        {
259                            foot.Append("[" + i + "]");
260                        }

261                        else
262                        {
263                            foot.Append("<a href='" + pageName + ".aspx?page=" + i + "' style='text-decoration:none'>[" + i + "]</a>");
264                        }

265                    }

266                    foot.Append("&nbsp;");
267                }

268            }

269            else
270            {
271                for (int i = 1; i <= row_count; i++)
272                {
273                    if (i == 1 && index_page == 0)
274                    {
275                        foot.Append("[" + i + "]");
276                    }

277                    else
278                    {
279                        if (i == index_page)
280                        {
281                            foot.Append("[" + i + "]");
282                        }

283                        else
284                        {
285                            foot.Append("<a href='" + pageName + ".aspx?page=" + i + "' style='text-decoration:none'>[" + i + "]</a>");
286                        }

287                    }

288                    foot.Append("&nbsp;");
289                }

290            }

291            if (index_page != row_count)
292            {
293                if (index_page == 0)
294                {
295                    foot.Append("<a href='" + pageName + ".aspx?page=2' style='text-decoration:none'>[下一页]</a>");
296                }

297                else
298                {
299                    foot.Append("<a href='" + pageName + ".aspx?page=" + next_page + "' style='text-decoration:none'>[下一页]</a>");
300                }

301                foot.Append("<a href='" + pageName + ".aspx?page=" + row_count + "' style='text-decoration:none'>[尾页]</a>");
302            }

303            else
304            {
305                foot.Append("[下一页]");
306                foot.Append("[尾页]");
307            }

308            foot.Append("&nbsp;");
309            foot.Append("转到<select id='sltPage' onchange='selectPage()'>");
310            for (int i = 1; i <= row_count; i++)
311            {
312                foot.Append("<option value=" + i + ">" + i + "</option>");
313            }

314            foot.Append("</select>");
315            foot.Append("</td>");
316            foot.Append("</tr></table>");
317            page_foot = foot.ToString();
318        }

319    }

320}

 

使用说明

在页面的Page_Load方法体里输入以下属性
PageConten1.rows = 368;  //总行数
            PageConten1.page_rows = 10; //每页分多少行
            PageConten1.page_info = 10; //每页要显示多少页码出来
            PageConten1.isRequest = 1; //是否带url参数,1为带,2为不带
            if (PageConten1.isRequest == 1)
            {
                index_page = int.Parse(Request["page"] == null ? ("1" + Request["page"]) : Request["page"]);
                PageConten1.pageName = "Test_4";
            }
            else
            {
                index_page = int.Parse(Request["hidSub"] == null ? ("1" + Request["hidSub"]) : Request["hidSub"]);
            }

posted on 2009-03-24 12:25  Cien  阅读(428)  评论(0编辑  收藏  举报