asp.net 文章内容分页显示的代码
发布时间:2020-12-15 20:45:09 所属栏目: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="h
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代码的良好排版换行即可。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – MVC应用程序的布局/设计模板?
- 使用嵌套类的ASP.NET MVC3 JSON模型绑定
- 在ASP.NET MVC 3中上载大于2GB的文件
- asp.net-mvc – 如何在ASP.net MVC 4 RouteConfig.cs中使用
- asp.net-mvc-3 – 发布到列表MVC3
- asp.net – 根据DropDownList选择验证TextBox
- 项目中出现多个域名下的Cookie
- asp.net-mvc-3 – 剃刀不能轻松地使用强类型的Html.ActionL
- asp.net – 可以回收IIS应用程序池,而不会丢失用户会话?
- asp.net-mvc – 带有html实体的MVC标签
推荐文章
站长推荐
- asp.net-mvc – 如何建立“后退”链接?
- asp.net-mvc-3 – 如何在ASP.NET MVC3控制器中访
- asp.net – 以下部分已定义,但尚未针对布局页面“
- ASP.NET MVC 3布局所有子视图中的ViewBag数据
- asp.net – 无法获得typeahead.js基本示例
- Asp.Net MVC验证 – 依赖字段
- asp.net-mvc – 如何使用ASP.NET Web API的Ninje
- asp.net – 获取在page_init中触发回发的控件
- asp.net – Visual Studio可以在新的调试会话中重
- asp.net-mvc – 有没有办法为MVC控制器中的每个动
热点阅读