`

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

阅读更多

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

 

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

 另外,如果喜欢折腾数据库的朋友可以使用如下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" 
            onclick="btnPrevious_Click" />
        <asp:Button ID="btnNext" runat="server" Text="下一页" Enabled="False" 
            onclick="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);
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics