新闻内容页分页的简单做法

该例子只是提供了基本思路 

 

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

 另外,如果喜欢折腾数据库的朋友可以使用如下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个字符止的字符串. sql中substring函数索引从1开始

 前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnPrevious" runat="server" Text="上一页" Enabled="False" 
            οnclick="btnPrevious_Click" />
        <asp:Button ID="btnNext" runat="server" Text="下一页" Enabled="False" 
            οnclick="btnNext_Click" />
        <asp:Panel ID="Panel1" runat="server" Height="175px">
            <asp:Label ID="lblPaginationInfo" runat="server" Text="Label"></asp:Label><br />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

 

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    private String content = "1wwwwwwwww2eeeeeeeee3rrrrrrrrr4ttttttttt5ggggggggg6bbbbbbbbb7ddddddddd";
    private int i;
    private int size = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindtxt(0);
            Session["index"] = 0;
        }
    }

    private void bindtxt(int i)
    {
        Label lblContent = new Label();
        lblContent.ID = "lbl" + i.ToString();
        lblContent.Text = content.Substring(i * size, size);
        Panel1.Controls.Add(lblContent);

        int contentLen = content.Length;
        int contentCount = contentLen / size;
        if ((contentLen % size) > 0)
        {
            contentCount++;
        }

        if (contentCount > 1)//如果总页数只有一页就不需要进行任何操作了
        {
            if (i < contentCount - 1)//如果不小于总页数,那么他就是最后一页
            {
                btnNext.Enabled = true;

                if (i >= 1) //判断是否为第1页
                {
                    btnPrevious.Enabled = true;
                }
                else
                {
                    btnPrevious.Enabled = false;
                }
            }
            else
            {
                btnPrevious.Enabled = true;
                btnNext.Enabled = false;
            }
        }

        lblPaginationInfo.Text = "总页数:" + contentCount + "当前页:" + (i + 1).ToString();

    }

    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        i = Convert.ToInt32(Session["index"]) - 1;
        Session["index"] = i;
        bindtxt(i);
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        i = Convert.ToInt32(Session["index"]) + 1;
        Session["index"] = i;
        bindtxt(i);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值