asp.net 文章內(nèi)容分頁(yè)顯示的代碼
更新時(shí)間:2009年01月07日 23:18:25 作者:
有種文章分頁(yè)的思路是用截取文本字符數(shù)的方法來(lái)處理,這個(gè)方法當(dāng)文章內(nèi)容是html代碼的話,分頁(yè)后會(huì)引起排版問(wèn)題。
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArticlePage.aspx.cs" Inherits="ArticlePage" %>
<!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>文章分頁(yè)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="text-align: center;" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" style="width: 400px; background-color: #ffff99;">
<table style="width: 100%">
<tr>
<td style="width: 100%; border-top: #ff3300 2px dotted; border-left-width: 2px; border-left-color: #ff3300; border-bottom: #ff3300 2px dotted; border-right-width: 2px; border-right-color: #ff3300; height: 25px; text-align: center;">
<%=ArticleTitle %></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="width: 400px; height: 100%;" align="left">
<p style="background-color: oldlace">
<%=Article %>
</p>
</td>
</tr>
<tr>
<td style="width: 400px; background-color: #ffff99; height: 48px;">
<table style="width: 100%">
<tr>
<td style="width: 100%; border-top: #ff3300 2px dotted; border-left-width: 2px; border-left-color: #ff3300; border-bottom: #ff3300 2px dotted; border-right-width: 2px; border-right-color: #ff3300;" align="left">
<p>
<asp:HyperLink ID="firstLink" runat="server" Visible="False">首頁(yè)</asp:HyperLink>
<asp:HyperLink ID="preLink" runat="server" Visible="False">上一頁(yè)</asp:HyperLink>
<asp:HyperLink ID="nextLink" runat="server" Visible="False">下一頁(yè)</asp:HyperLink>
<asp:HyperLink ID="lastLink" runat="server" Visible="False">末頁(yè)</asp:HyperLink>
</p>
<p>
<%
if(pageSum>1)
{
for (int i = 1; i <= pageSum; i++)
{
if (pageNo == i)
{
%>
<%=i%>
<%
}
else
{
%>
<a href="?page=<%=i %>"><%=i%></a>
<%
}
}
}
%>
頁(yè)數(shù):<%=pageNo %>/<%=pageSum %>
</p></td>
</tr>
</table>
</td>
</tr>
</table>
<br />
</div>
</form>
</body>
</html>
//==========================
aspx.cs:
(C#)
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 ArticlePage : System.Web.UI.Page
{
protected string Article = "", ArticleTitle="";
protected int pageNo = 1, pageSum = 1;
protected void Page_Load(object sender, EventArgs e)
{
//實(shí)際應(yīng)用中,此處的數(shù)據(jù)通過(guò)操作數(shù)據(jù)庫(kù)來(lái)獲取
ArticleTitle = "文章標(biāo)題";
string filename = "20091795819.html";
string mPath = Server.MapPath("h3g/");
string filepath = mPath + filename;
ShowArticle(filepath);
}
protected void ShowArticle(string filepath)
{
string page = Request.Params["page"];
int perPageLine = 5;//每頁(yè)行數(shù)
ArrayList al = fileOpr.ReadFileContentToArrayList(filepath);//按行讀取文件內(nèi)空到數(shù)組中
int contentLine = al.Count;
pageSum = (int)System.Math.Ceiling((double)contentLine / perPageLine);//總頁(yè)數(shù),進(jìn)1取整
if (page == null || page == "" || page == "1")
{
pageNo = 1;
if (contentLine <= perPageLine)
{
for (int i = 0; i < contentLine; i++)
{
Article += al[i].ToString();
}
}
else
{
for (int i = 0; i < perPageLine; i++)
{
Article += al[i].ToString();
}
firstLink.Visible = false;
preLink.Visible = false;
nextLink.NavigateUrl = "?page=" + (pageNo + 1);
nextLink.Visible = true;
lastLink.NavigateUrl = "?page=" + pageSum;
lastLink.Visible = true;
}
}
else
{
pageNo = int.Parse(page);
if (pageNo < pageSum)
{
for (int i = perPageLine * (pageNo - 1); i < perPageLine * pageNo; i++)
{
Article += al[i].ToString();
}
firstLink.NavigateUrl = "?page=1";
firstLink.Visible = true;
preLink.NavigateUrl = "?page=" + (pageNo - 1);
preLink.Visible = true;
nextLink.NavigateUrl = "?page=" + (pageNo + 1);
nextLink.Visible = true;
lastLink.NavigateUrl = "?page=" + pageSum;
lastLink.Visible = true;
}
else
{
for (int i = contentLine - perPageLine * (pageSum - 1); i < contentLine; i++)
{
Article += al[i].ToString();
}
firstLink.NavigateUrl = "?page=1";
firstLink.Visible = true;
preLink.NavigateUrl = "?page=" + (pageNo - 1);
preLink.Visible = true;
nextLink.Visible = false;
lastLink.Visible = false;
}
}
}
}
重用類fileOpr.cs:
fileOpr.ReadFileContentToArrayList(filepath);中的方法:
public static ArrayList ReadFileContentToArrayList(string filepath)
{
ArrayList al = new ArrayList();
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamReader srd = new StreamReader(fs, Encoding.Default);
//使用StreamReader類來(lái)讀取文件
srd.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = srd.ReadLine();
while (strLine != null)
{
strLine = srd.ReadLine();
al.Add(strLine + "\n");
}
//關(guān)閉此StreamReader對(duì)象
srd.Close();
fs.Dispose();
fs.Close();
return al;
}
注:有種文章分頁(yè)的思路是用截取文本字符數(shù)的方法來(lái)處理,這個(gè)方法當(dāng)文章內(nèi)容是html代碼的話,分頁(yè)后會(huì)引起排版問(wèn)題。
上面代碼的方法思路是按行數(shù)來(lái)處理,這個(gè)方法個(gè)人認(rèn)為相對(duì)更好些。在后臺(tái)管理文章內(nèi)容文件時(shí),保證html代碼的良好排版換行即可。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArticlePage.aspx.cs" Inherits="ArticlePage" %>
<!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>文章分頁(yè)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="text-align: center;" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" style="width: 400px; background-color: #ffff99;">
<table style="width: 100%">
<tr>
<td style="width: 100%; border-top: #ff3300 2px dotted; border-left-width: 2px; border-left-color: #ff3300; border-bottom: #ff3300 2px dotted; border-right-width: 2px; border-right-color: #ff3300; height: 25px; text-align: center;">
<%=ArticleTitle %></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="width: 400px; height: 100%;" align="left">
<p style="background-color: oldlace">
<%=Article %>
</p>
</td>
</tr>
<tr>
<td style="width: 400px; background-color: #ffff99; height: 48px;">
<table style="width: 100%">
<tr>
<td style="width: 100%; border-top: #ff3300 2px dotted; border-left-width: 2px; border-left-color: #ff3300; border-bottom: #ff3300 2px dotted; border-right-width: 2px; border-right-color: #ff3300;" align="left">
<p>
<asp:HyperLink ID="firstLink" runat="server" Visible="False">首頁(yè)</asp:HyperLink>
<asp:HyperLink ID="preLink" runat="server" Visible="False">上一頁(yè)</asp:HyperLink>
<asp:HyperLink ID="nextLink" runat="server" Visible="False">下一頁(yè)</asp:HyperLink>
<asp:HyperLink ID="lastLink" runat="server" Visible="False">末頁(yè)</asp:HyperLink>
</p>
<p>
<%
if(pageSum>1)
{
for (int i = 1; i <= pageSum; i++)
{
if (pageNo == i)
{
%>
<%=i%>
<%
}
else
{
%>
<a href="?page=<%=i %>"><%=i%></a>
<%
}
}
}
%>
頁(yè)數(shù):<%=pageNo %>/<%=pageSum %>
</p></td>
</tr>
</table>
</td>
</tr>
</table>
<br />
</div>
</form>
</body>
</html>
//==========================
aspx.cs:
(C#)
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 ArticlePage : System.Web.UI.Page
{
protected string Article = "", ArticleTitle="";
protected int pageNo = 1, pageSum = 1;
protected void Page_Load(object sender, EventArgs e)
{
//實(shí)際應(yīng)用中,此處的數(shù)據(jù)通過(guò)操作數(shù)據(jù)庫(kù)來(lái)獲取
ArticleTitle = "文章標(biāo)題";
string filename = "20091795819.html";
string mPath = Server.MapPath("h3g/");
string filepath = mPath + filename;
ShowArticle(filepath);
}
protected void ShowArticle(string filepath)
{
string page = Request.Params["page"];
int perPageLine = 5;//每頁(yè)行數(shù)
ArrayList al = fileOpr.ReadFileContentToArrayList(filepath);//按行讀取文件內(nèi)空到數(shù)組中
int contentLine = al.Count;
pageSum = (int)System.Math.Ceiling((double)contentLine / perPageLine);//總頁(yè)數(shù),進(jìn)1取整
if (page == null || page == "" || page == "1")
{
pageNo = 1;
if (contentLine <= perPageLine)
{
for (int i = 0; i < contentLine; i++)
{
Article += al[i].ToString();
}
}
else
{
for (int i = 0; i < perPageLine; i++)
{
Article += al[i].ToString();
}
firstLink.Visible = false;
preLink.Visible = false;
nextLink.NavigateUrl = "?page=" + (pageNo + 1);
nextLink.Visible = true;
lastLink.NavigateUrl = "?page=" + pageSum;
lastLink.Visible = true;
}
}
else
{
pageNo = int.Parse(page);
if (pageNo < pageSum)
{
for (int i = perPageLine * (pageNo - 1); i < perPageLine * pageNo; i++)
{
Article += al[i].ToString();
}
firstLink.NavigateUrl = "?page=1";
firstLink.Visible = true;
preLink.NavigateUrl = "?page=" + (pageNo - 1);
preLink.Visible = true;
nextLink.NavigateUrl = "?page=" + (pageNo + 1);
nextLink.Visible = true;
lastLink.NavigateUrl = "?page=" + pageSum;
lastLink.Visible = true;
}
else
{
for (int i = contentLine - perPageLine * (pageSum - 1); i < contentLine; i++)
{
Article += al[i].ToString();
}
firstLink.NavigateUrl = "?page=1";
firstLink.Visible = true;
preLink.NavigateUrl = "?page=" + (pageNo - 1);
preLink.Visible = true;
nextLink.Visible = false;
lastLink.Visible = false;
}
}
}
}
重用類fileOpr.cs:
fileOpr.ReadFileContentToArrayList(filepath);中的方法:
public static ArrayList ReadFileContentToArrayList(string filepath)
{
ArrayList al = new ArrayList();
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamReader srd = new StreamReader(fs, Encoding.Default);
//使用StreamReader類來(lái)讀取文件
srd.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = srd.ReadLine();
while (strLine != null)
{
strLine = srd.ReadLine();
al.Add(strLine + "\n");
}
//關(guān)閉此StreamReader對(duì)象
srd.Close();
fs.Dispose();
fs.Close();
return al;
}
注:有種文章分頁(yè)的思路是用截取文本字符數(shù)的方法來(lái)處理,這個(gè)方法當(dāng)文章內(nèi)容是html代碼的話,分頁(yè)后會(huì)引起排版問(wèn)題。
上面代碼的方法思路是按行數(shù)來(lái)處理,這個(gè)方法個(gè)人認(rèn)為相對(duì)更好些。在后臺(tái)管理文章內(nèi)容文件時(shí),保證html代碼的良好排版換行即可。
您可能感興趣的文章:
- ASPNETPAGER分頁(yè)控件的使用方法[圖文]
- Asp.Net中的三種分頁(yè)方式總結(jié)
- Asp.net GridView使用大全(分頁(yè)實(shí)現(xiàn))
- ASP.NET 高性能分頁(yè)代碼
- ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁(yè)教程(PagedList.Mvc)
- Asp.Net數(shù)據(jù)控件引用AspNetPager.dll分頁(yè)實(shí)現(xiàn)代碼
- asp.net Datalist控件實(shí)現(xiàn)分頁(yè)功能
- asp.net分頁(yè)控件AspNetPager的樣式美化
- AspNetPager分頁(yè)控件源代碼(Version 4.2)
- 基于Dapper實(shí)現(xiàn)分頁(yè)效果 支持篩選、排序、結(jié)果集總數(shù)等
相關(guān)文章
ASP.NET Core擴(kuò)展庫(kù)之實(shí)體映射使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫(kù)之實(shí)體映射使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-03-03
Asp.net core中實(shí)現(xiàn)自動(dòng)更新的Option的方法示例
這篇文章主要介紹了Asp.net core中實(shí)現(xiàn)自動(dòng)更新的Option的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Asp.net(C#)實(shí)現(xiàn)驗(yàn)證碼功能代碼
asp.net驗(yàn)證碼的實(shí)現(xiàn)方法2008-10-10
.net搜索查詢并實(shí)現(xiàn)分頁(yè)實(shí)例
.net搜索查詢并實(shí)現(xiàn)分頁(yè)實(shí)例,需要的朋友可以參考一下2013-03-03
.net core如何在網(wǎng)絡(luò)高并發(fā)下提高JSON的處理效率詳解
這篇文章主要給大家介紹了關(guān)于.net core如何在網(wǎng)絡(luò)高并發(fā)下提高JSON的處理效率的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
ASP.NET session.timeout設(shè)置案例詳解
這篇文章主要介紹了ASP.NET session.timeout設(shè)置案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
asp.net 刪除MFC單文檔默認(rèn)菜單欄的兩種方法
新建一個(gè)MFC單文檔程序,默認(rèn)都有四個(gè)菜單欄:文件、編輯、視圖和幫助。怎么把這四個(gè)菜單欄刪除掉呢?2010-03-03

