(转载)新闻内容页分页的简单做法

本文介绍了一种在ASP.NET中实现字符串分页显示的方法,通过设置每页显示的字符数量,利用前后翻页按钮控制内容展示,并展示了具体的HTML及C#代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很简单的做法,这个是假设数据已经拿出来了,不用再去数据库折腾了

另外,如果喜欢折腾数据库的朋友可以使用如下SQL语句得到指定位置的指定字数的内容

这个是指定位置的select a from b where len(a)>=100 and len(a)<=400 --查字段a的字符串长度在100到400之间的
select substring(a,100,400) from b --查询a字段从第100个字符起到400个字符止的字符串.

前台代码:

前台HTML页代码
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7    <title>无标题页</title>
8</head>
9<body>
10    <form id="form1" runat="server">
11    <div>
12        <asp:Button ID="nbtn" runat="server" Enabled="False" Height="23px"
13            Style="z-index: 100; left: 301px; position: absolute; top: 241px" Text="下一页" />
14        <asp:Button ID="pbtn" runat="server" Enabled="False" Height="23px"
15            Style="z-index: 101; left: 222px; position: absolute; top: 241px" Text="上一页" />
16        <aspanel ID="Panel1" runat="server" Height="201px" Style="z-index: 103; left: 12px;
17            position: absolute; top: 8px" Width="654px">
18            <aspabel ID="Label1" runat="server" Style="z-index: 100; left: 261px; position: absolute;
19                top: 278px" Text="Label"></aspabel>
20        </aspanel>
21   
22    </div>
23    </form>
24</body>
25</html>
26

后台CS代码:

using 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;
11
12public partial class Default2 : System.Web.UI.Page
13{
14    /**//// <summary>
15    /// 天轰穿网站地址  www.thc123.com  也可以直接搜索 “学趣”
16    /// 在下面我们设置了三个变量
17    /// concent  是我们要显示的字符串
18    /// size    每页显示多少个字符
19    /// i        当前的页码
20    /// </summary>
21    private string c;
22    private int size = 10;
23    private int i;
24    protected void Page_Load(object sender, EventArgs e)
25    {
26        if (!IsPostBack)
27        {
28            bindtxt(0);//因为是第一次显示页,所以要最前面的内容
29            Label1.Text = "1";//显示当前页码
30        }
31    }
32    /**//// <summary>
33    /// 显示指定位置的指定长度的内容,并且控制翻页按纽和页码
34    /// </summary>
35    /// <param name="i">需要显示的页码</param>
36    protected void bindtxt(int i)
37    {
38        Label lbl = new Label();    //new一个Label对象
39        lbl.ID = "lbl" + i.ToString();//设置新对象的ID
40        lbl.Text = concent.Substring(i * size, size);//设置他的TEXT属性,注意下这里给Substring的参数
41        Panel1.Controls.Add(lbl);//将LABEL对象添加到PANEL中去
42
43        int count = concent.Length / size;//得出总页数
44        //下面的算法自己去琢磨吧,呵呵
45        if (count > 1)
46        {
47            if (i < count-1)
48            {
49                nbtn.Enabled = true;
50                if (i >= 1)
51                    pbtn.Enabled = true;
52                else
53                    pbtn.Enabled = false;
54            }
55            else
56            {
57                nbtn.Enabled = false;
58                pbtn.Enabled = true;
59            }
60        }
61        Label1.Text = (i+1).ToString();
62    }
63    protected void pbtn_Click(object sender, EventArgs e)
64    {//这里用到Session来保存旧的页码
65        i = Convert.ToInt32(Session["index"]) - 1;
66        Session["index"] = i;//操作完以后把新的再给Session
67        bindtxt(i);//直接调用就OK 了
68    }
69    protected void nbtn_Click(object sender, EventArgs e)
70    {
71        i = Convert.ToInt32(Session["index"]) + 1;
72        Session["index"] = i;
73        bindtxt(i);
74    }
75}
76

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值