asp.net 文章内容分页显示的代码
程序员文章站
2022-05-22 13:43:10
aspx: <%@ page language="c#" autoeventwireup="true" codefile="articlepage.aspx.cs"...
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代码的良好排版换行即可。
<%@ 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代码的良好排版换行即可。
推荐阅读
-
http://www.hunanzhibo.com/ php文章内容分页并生成相应的htm静态页面代码
-
一个人的寂寞两个人的错 贺一航 又一个php 分页类实现代码
-
如何获取分页显示的form表单的值?
-
ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI的界面错位的解决方法_jquery
-
有关PHP分页显示的问题
-
鼠标移到div,浮层显示明细,弹出层与div的上边距左边距重合(示例代码)_javascript技巧
-
页码自动适应范围的分页代码
-
本人求一个Struts+Hibernate分页的代码 StrutsHibernate
-
本人求一个Struts+Hibernate分页的代码 StrutsHibernate
-
请大家帮我看一下我这段代码中的ul和a为什么不能显示在同一行中。_html/css_WEB-ITnose