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

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

 另外,如果喜欢折腾数据库的朋友可以使用如下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个字符止的字符串.

<%@ 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="nbtn" runat="server" Enabled="False" Height="23px" OnClick="nbtn_Click"
            Style="z-index: 100; left: 301px; position: absolute; top: 241px" Text="下一页" />
        <asp:Button ID="pbtn" runat="server" Enabled="False" Height="23px" OnClick="pbtn_Click"
            Style="z-index: 101; left: 222px; position: absolute; top: 241px" Text="上一页" />
        <asp:Panel ID="Panel1" runat="server" Height="201px" Style="z-index: 103; left: 12px;
            position: absolute; top: 8px" Width="654px">
            <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 261px; position: absolute;
                top: 278px" Text="Label"></asp:Label>
        </asp:Panel>
    
    </div>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    
/**//// <summary>
    
/// 天轰穿网站地址  www.thc123.com  也可以直接搜索 “学趣”
    
/// 在下面我们设置了三个变量
    
/// concent  是我们要显示的字符串
    
/// size     每页显示多少个字符
    
/// i        当前的页码
    
/// </summary>

    private string concent = "1wwwwwwwww2eeeeeeeee3rrrrrrrrr4ttttttttt5ggggggggg6bbbbbbbbb7ddddddddd";
    
private int size = 10;
    
private int i;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            bindtxt(
0);//因为是第一次显示页,所以要最前面的内容
            Label1.Text = "1";//显示当前页码
        }

    }

    
/**//// <summary>
    
/// 显示指定位置的指定长度的内容,并且控制翻页按纽和页码
    
/// </summary>
    
/// <param name="i">需要显示的页码</param>

    protected void bindtxt(int i)
    
{
        Label lbl 
= new Label();    //new一个Label对象
        lbl.ID = "lbl" + i.ToString();//设置新对象的ID
        lbl.Text = concent.Substring(i * size, size);//设置他的TEXT属性,注意下这里给Substring的参数
        Panel1.Controls.Add(lbl);//将LABEL对象添加到PANEL中去

        
int count = concent.Length / size;//得出总页数
        
//下面的算法自己去琢磨吧,呵呵
        if (count > 1)
        
{
            
if (i < count-1)
            
{
                nbtn.Enabled 
= true;
                
if (i >= 1)
                    pbtn.Enabled 
= true;
                
else
                    pbtn.Enabled 
= false;
            }

            
else
            
{
                nbtn.Enabled 
= false;
                pbtn.Enabled 
= true;
            }

        }

        Label1.Text 
= (i+1).ToString();
    }

    
protected void pbtn_Click(object sender, EventArgs e)
    
{//这里用到Session来保存旧的页码
        i = Convert.ToInt32(Session["index"]) - 1;
        Session[
"index"= i;//操作完以后把新的再给Session
        bindtxt(i);//直接调用就OK 了
    }

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

}

原文地址:https://www.cnblogs.com/SALIN/p/776885.html