程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 文章內容分頁顯示的代碼

asp.net 文章內容分頁顯示的代碼

編輯:ASP.NET基礎
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>文章分頁</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">首頁</asp:HyperLink>
<asp:HyperLink ID="preLink" runat="server" Visible="False">上一頁</asp:HyperLink>
<asp:HyperLink ID="nextLink" runat="server" Visible="False">下一頁</asp:HyperLink>
<asp:HyperLink ID="lastLink" runat="server" Visible="False">末頁</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>
<%
}
}
}
%>
頁數:<%=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)
{
//實際應用中,此處的數據通過操作數據庫來獲取
ArticleTitle = "文章標題";
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;//每頁行數
ArrayList al = fileOpr.ReadFileContentToArrayList(filepath);//按行讀取文件內空到數組中
int contentLine = al.Count;
pageSum = (int)System.Math.Ceiling((double)contentLine / perPageLine);//總頁數,進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類來讀取文件
srd.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = srd.ReadLine();
while (strLine != null)
{
strLine = srd.ReadLine();
al.Add(strLine + "\n");
}
//關閉此StreamReader對象
srd.Close();
fs.Dispose();
fs.Close();
return al;
}
注:有種文章分頁的思路是用截取文本字符數的方法來處理,這個方法當文章內容是html代碼的話,分頁後會引起排版問題。
上面代碼的方法思路是按行數來處理,這個方法個人認為相對更好些。在後台管理文章內容文件時,保證html代碼的良好排版換行即可。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved