当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 文章内容分页显示的代码

asp.net 文章内容分页显示的代码

2018年04月18日  | 移动技术网IT编程  | 我要评论

e轮回txt,冲浪去,未来世界科幻画

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代码的良好排版换行即可。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网