【转】在asp.net中长内容自动分页的实现-.NET教程
在一篇文章过长时,可以自动的写个小程序对其进行分页.
具体代码:
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Page : System.Web.UI.Page
13 {
14 private string str;//字符
15 private int strl;//字符总长度
16 private int pagesize;//每页显示的字符数
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 // 在此处放置用户代码以初始化页面
20 str = "1234567891234567898522555";
21 pagesize = 3;
22 strl = str.Length;
23 Response.Write(strl);
24 substr();
25 }
26
27 private void substr()
28 {
29 int ct = Int32.Parse(Request.QueryString["page"]);
30 if (strl == (strl / pagesize) * pagesize)//看看页面的总记录是否能被每页的记录数整除
31 {
32 for (int i = 1; i <= strl / pagesize; i++)
33 {
34 Response.Write("页:<a href=page.aspx?page=" + i + ">" + (i) + "</" + "a>");
35 }
36 string s = str.Substring(pagesize * ct - pagesize, pagesize);
37 Response.Write(s);
38 }
39 else if (ct * pagesize > strl)//在不被整除的情况下,最后一页的设置,如字符长13,每页3,则处理最后那一页的显示
40 {
41 for (int i = 1; i <= (strl / pagesize) + 1; i++)
42 {
43 Response.Write("页:<a href=page.aspx?page=" + i + ">" + (i) + "</" + "a>");
44 }
45 string s = str.Substring((ct - 1) * pagesize, strl - (ct - 1) * pagesize);
46 Response.Write(s);
47 }
48 else //在不被整除的情况下其他页面的显示设置
49 {
50 for (int i = 1; i <= strl / pagesize + 1; i++)
51 {
52 Response.Write("页:<a href=page.aspx?page=" + i + ">" + (i) + "</" + "a>");
53 }
54 string s = str.Substring(pagesize * ct - pagesize, pagesize);
55 Response.Write(s);
56 }
57 }
58 }
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Page : System.Web.UI.Page
13 {
14 private string str;//字符
15 private int strl;//字符总长度
16 private int pagesize;//每页显示的字符数
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 // 在此处放置用户代码以初始化页面
20 str = "1234567891234567898522555";
21 pagesize = 3;
22 strl = str.Length;
23 Response.Write(strl);
24 substr();
25 }
26
27 private void substr()
28 {
29 int ct = Int32.Parse(Request.QueryString["page"]);
30 if (strl == (strl / pagesize) * pagesize)//看看页面的总记录是否能被每页的记录数整除
31 {
32 for (int i = 1; i <= strl / pagesize; i++)
33 {
34 Response.Write("页:<a href=page.aspx?page=" + i + ">" + (i) + "</" + "a>");
35 }
36 string s = str.Substring(pagesize * ct - pagesize, pagesize);
37 Response.Write(s);
38 }
39 else if (ct * pagesize > strl)//在不被整除的情况下,最后一页的设置,如字符长13,每页3,则处理最后那一页的显示
40 {
41 for (int i = 1; i <= (strl / pagesize) + 1; i++)
42 {
43 Response.Write("页:<a href=page.aspx?page=" + i + ">" + (i) + "</" + "a>");
44 }
45 string s = str.Substring((ct - 1) * pagesize, strl - (ct - 1) * pagesize);
46 Response.Write(s);
47 }
48 else //在不被整除的情况下其他页面的显示设置
49 {
50 for (int i = 1; i <= strl / pagesize + 1; i++)
51 {
52 Response.Write("页:<a href=page.aspx?page=" + i + ">" + (i) + "</" + "a>");
53 }
54 string s = str.Substring(pagesize * ct - pagesize, pagesize);
55 Response.Write(s);
56 }
57 }
58 }