长文章分页程序(根据设定标记)

本文介绍了一种在ASP.NET中实现文章内容分页显示的方法,通过检查内容中是否存在特定分页标记来决定是否进行分页,并展示了如何计算总页数、当前页以及生成页面导航元素。

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

//分页类
public static string PageInfo(int count, string pageInfo, int pageList, int nowPage, string pageStr, string unit)
{
    int pageCount = 1;
    string temp = "<font color='red'>" + pageInfo + "共有" + count + unit + "</font>&nbsp;" + "每页" + pageList + unit + pageInfo;
    if (count > 0)
    {
        string firstPage = "";//首页
        string lastPage = "";//上一页
        string nextPage = "";//下一页
        string endPage = "";//未页
        pageCount = count / pageList;
        string selectStr = "跳转:<select name='PageSelect' οnchange='window.location.href=value' id='PageSelect'>";//下拉单
        if ((count % pageList) > 0)
        {
            pageCount++;
        }
        for (int i = 1; i <= pageCount; i++)
        {
            if (i == nowPage)
            {
                selectStr += "<option selected='selected' value='" + pageStr + "page=" + i + "'>" + i + "</option>";
            }
            else
            {
                selectStr += "<option value='" + pageStr + "page=" + i + "'>" + i + "</option>";
            }
        }
        selectStr += "</select>";
        if (pageCount == 1)
        {
            firstPage = "<font color='#cccccc'>首页</font>";
            lastPage = "<font color='#cccccc'>上一页</font>";
            nextPage = "<font color='#cccccc'>下一页</font>";
            endPage = "<font color='#cccccc'>尾页</font>";
        }
        else
        {
            if (nowPage == 1)
            {
                firstPage = "<font color='#cccccc'>首页</font>";
                lastPage = "<font color='#cccccc'>上一页</font>";
                nextPage = "<a href='" + pageStr + "page=" + (nowPage + 1) + "'>下一页</a>";
                endPage = "<a href='" + pageStr + "page=" + pageCount + "'>尾页</a>";
            }
            else if (nowPage == pageCount)
            {
                firstPage = "<a href='" + pageStr + "page=1'>首页</a>";
                lastPage = "<a href='" + pageStr + "page=" + (pageCount - 1) + "'>上一页</a>";
                nextPage = "<font color='#cccccc'>下一页</font>";
                endPage = "<font color='#cccccc'>尾页</font>";
            }
            else
            {
                nextPage = "<a href='" + pageStr + "page=" + (nowPage + 1) + "'>下一页</a>";
                lastPage = "<a href='" + pageStr + "page=" + (nowPage - 1) + "'>上一页</a>";
                firstPage = "<a href='" + pageStr + "page=1'>首页</a>";
                endPage = "<a href='" + pageStr + "page=" + pageCount + "'>尾页</a>";
            }
        }
        temp += "&nbsp;&nbsp;" + firstPage + "&nbsp;&nbsp;" + lastPage + "&nbsp;&nbsp;" + nextPage + "&nbsp;&nbsp;" + endPage + "&nbsp;&nbsp;" + selectStr;
    }
    else
    {
        temp = "";
    }
    return temp;
}

//实现代码

int page = 1;
if (Request.QueryString["page"] != null)
{
    page = Convert.ToInt32(Request.QueryString["page"].Trim());
}

int ArticleID = Convert.ToInt16(Request["ID"]);
dr = MyclassDA.Index_Article_Show(ArticleID);  //SqlDataReader Index_Article_Show(int ID) 创建对象
dr.Read();

show1.InnerHtml = dr["Title"].ToString();
string content = dr["Content"].ToString();
ArrayList body = new ArrayList();

string PageStr = "a_read.aspx?id=" + ArticleID + "&";

if (content.IndexOf("{#NextPage#}") > -1)//存在分页符号则分页
{
    //string[] tempList = content.Split(new string[] { "{@Next@}" }, StringSplitOptions.RemoveEmptyEntries);//String.Split 2.0新增特性
    //string[] tempList = SplitPage(content,"{@Next@}");//内容分页
    string[] tempList = Regex.Split(content, "{#NextPage#}", RegexOptions.IgnoreCase);
    for (int i = 0; i < tempList.Length; i++)
    {
        body.Add(tempList[i]);
    }

}
else
{
    body.Add(content);//直接输出内容
}

if (page < 1 || page > body.Count)
{
    page = 1;
}

if (body.Count == 1)
{
    l_content.InnerHtml = (string) body[0];
}
else
{
    if (page <= body.Count)
    {
        l_content.InnerHtml = (string) body[page - 1];
    }
    else
    {
        l_content.InnerHtml = (string) body[0];
    }
}

MyCutePage.InnerHtml = CommonClass.PageInfo(body.Count, "新闻", 1, page, PageStr, "篇");

dr.Close();

 

作者:阿良.net
转自:http://www.chenjiliang.com/Article/View.aspx?ArticleID=2652&TypeID=84

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值