简单写点ASP.NET文章内容分页
作者:互联网
原文链接:http://www.cnblogs.com/haylim/articles/4173812.html
最近有点闲,写了个文章内容分页
前台
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="newsContent.aspx.cs" Inherits="newsContent" %> 2 <%@ Register Src="UserControls/search.ascx" TagName="search" TagPrefix="uc3" %> 3 <%@ Register Src="UserControls/UserFoot.ascx" TagName="UserFoot" TagPrefix="uc1" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <html xmlns="http://www.w3.org/1999/xhtml" > 8 <head runat="server"> 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 10 <title>物流资讯-内页</title> 11 <link href="css/newsContent.css" rel="stylesheet" type="text/css" /> 12 <link href="css/style.css" rel="stylesheet" type="text/css" /> 13 <link href="css/common.css" rel="stylesheet" type="text/css" /> 14 <link href="../css/style.css" rel="stylesheet" type="text/css" /> 15 </head> 16 17 <body> 18 <iframe scrolling="no" width="100%"; height="165" src="include/head.html" frameborder="0"></iframe> 19 <form id="form1" runat="server"> 20 <div class="blank3"></div> 21 <div class="content"> 22 <div class="blank3"></div> 23 <div class="area"> 24 <div class="i_left"><uc3:search ID="Search1" runat="server" /> 25 <%-- <div class="rightⅠ"> 26 <div class="blank20"></div> 27 <div class="rightS"> 28 <div class="title"> 29 <span class="Blue fB left_tilte"><img src="img/index_30.gif" width="66" height="32" /></span> 30 <div class=" clear"></div> 31 </div> 32 <div class="blank9"></div> 33 <input type="text" name="textfield" class="texts" id="textfield" /> 34 <div class="blank9"></div> 35 <div class=" right"><img src="img/index_38.gif" width="56" height="17" /></div> 36 </div> 37 </div>--%> 38 <div class="blank9"></div> 39 <div class="percent"> 40 <div class="left_bor"> 41 <div class="left_menu">物流资讯</div> 42 <div class="menu_bg"> 43 <ul > 44 <li id="about1"><a href="newslist.aspx?ClassName=企业新闻" >企业新闻</a></li> 45 <li id="about2"><a href="newslist.aspx?ClassName=行业动态" >行业动态</a></li> 46 <li id="about3"><a href="newslist.aspx?ClassName=物流知识" >物流知识</a></li> 47 <li id="about4"><a href="newslist.aspx?ClassName=物流法规" >物流法规</a></li> 48 </ul> 49 </div> 50 <div class="blank9"></div> 51 <div class="blank9"></div> 52 <div class="blank9"></div> 53 </div> 54 </div> 55 <div class="blank9"></div> 56 </div> 57 <div class=" i_right"> 58 <div class="percent"> 59 <div class="ad1"><img src="ad_img/join_us_03.jpg" width="760" height="116" /></div> 60 </div> 61 <div class="blank9"></div> 62 <div class="right_BG"> 63 <div class="blank9"></div> 64 <div class="mag_center"> 65 <div class="news_title"> 66 <span class="more"><a class=" cOrange">>>更多</a></span> 67 <span class=" news_titS">物流资讯 </span></div> 68 69 <div id="endMain"> 70 <div class="theTitle"> 71 <h1 id="endTitle"><%=strTitle%></h1> 72 <div class="text">发布日期: <%=strDate%> 点击次数:<%=strHits%> </div> 73 </div> 74 <div id="endText"> 75 <%=strContent%> 76 77 </div> 78 </div> 79 </div> 80 <div class="blank9"></div> 81 </div> 82 83 </div> 84 <!-- 分类 --> 85 <div class="clear"></div> 86 </div> 87 88 </div> 89 <div class="blank9"></div> 90 <uc1:UserFoot ID="UserFoot1" runat="server" /> 91 </form> 92 </body> 93 </html>
实现后台
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 using NewAuto.Data; 12 using NewAuto.Entities; 13 14 using System.Collections; 15 16 public partial class newsContent : System.Web.UI.Page 17 { 18 protected string strTitle = string.Empty; 19 protected string strDate = string.Empty; 20 protected string strHits = string.Empty; 21 protected string strContent = string.Empty; 22 protected string strClass = string.Empty; 23 protected string page = string.Empty; 24 protected void Page_Load(object sender, EventArgs e) 25 { 26 page = Request.Params["page"]; 27 string NewsID = Request.Params["id"]; 28 if (!this.Page.IsPostBack) 29 { 30 GetNewsDesc(NewsID); 31 Page.Title = "捷利物流-" + strTitle; 32 } 33 } 34 35 #region 获取新闻信息 36 /// <summary> 37 /// 根据ID获取新闻信息 38 /// </summary> 39 /// <param name="ID"></param> 40 private void GetNewsDesc(string ID) 41 { 42 if (!string.IsNullOrEmpty(ID)) 43 { 44 Hashtable ht = new Hashtable(); 45 46 NewAuto.Entities.News objNews = new NewAuto.Entities.News(); 47 objNews = DataRepository.NewsProvider.GetById(Convert.ToInt32(ID)); 48 strTitle = objNews.Title; 49 strDate = objNews.AddDate.ToString(); 50 strHits = objNews.Hits.ToString(); 51 //strContent =substr(objNews.Contents); 52 ht = SeparatePages(objNews.Contents, page, 600, "newscontent.aspx?id=" + ID); 53 54 foreach (Object obj in ht.Keys) 55 { 56 //Console.WriteLine("\t{0}:\t{1}", (string)obj, myHT[obj]); 57 strContent += "<span style='text-align: center'>" + ht[obj] + "</span><br /><br />"; 58 } 59 //strContent=SeparatePages(objNews.Contents, page, 500, "newscontent.aspx?id=" + ID).Values.ToString(); 60 //更新打开该新闻次数 61 objNews.Hits = objNews.Hits + 3; 62 DataRepository.NewsProvider.Update(objNews); 63 } 64 } 65 #endregion 66 67 68 #region 长新闻分页 69 /**/ 70 /// <summary> 71 /// 长内容分页 哈希表需要引用命名空间System.Collections 72 /// </summary> 73 /// <param name="n_content">新闻内容</param> 74 /// <param name="page">是新闻当前页数</param> 75 /// <param name="size">每页显示字符长度</param> 76 /// <param name="linkurl">页码链接地址</param> 77 /// <returns></returns> 78 private System.Collections.Hashtable SeparatePages(string n_content, string page, int size, string linkurl) 79 { 80 //在此处放置初始化页的用户代码 81 System.Collections.Hashtable returnHash = new System.Collections.Hashtable(); 82 int start, stops, t, stat, pp, pagecount, pagesize; 83 string pa, articletxt, articletext, html; 84 int pig = 0; 85 86 //变量初始值 87 stat = 0; 88 start = 0; //开始查询的字符串位置,初始为0 89 stops = 0; 90 pagesize = size;//定义每页至少显示字符串数 91 pagecount = 0; 92 html = ""; 93 94 //获得当前的页数 95 pa = page; 96 if (pa == "" || pa == null) 97 { 98 pa = "1"; 99 } 100 pp = Convert.ToInt32(pa); 101 102 //获得内容 103 articletxt = n_content; 104 105 //判断页面的内容长度是否大于定义的每页至少显示字符串数 106 if (articletxt.Length >= pagesize) // 如果大于字符串数,则我们可以分页显示 107 { 108 t = articletxt.Length / pagesize; //获得大致的总页数 109 for (int j = 0; j <= t; j++) 110 { //如果查询开始位置到查询的范围超出整个内容的长度,那么就不用寻找断点(分页点);反之,查找 111 if (start + pagesize < articletxt.Length) 112 { 113 stat = articletxt.ToLower().IndexOf("</p>", start + pagesize); //查找</P>分页点的位置 114 if (stat == -1) 115 stat = articletxt.ToLower().IndexOf("<br>", start + pagesize); //查找</P>分页点的位置 116 if (stat == -1) 117 stat = articletxt.ToLower().IndexOf("<br/>", start + pagesize); //查找</P>分页点的位置 118 } 119 if (stat <= 0)//如果找不到 120 { 121 122 } 123 else 124 { 125 stops = stat; //分页点的位置也就作为这一页的终点位置 126 127 if (start < articletxt.Length) 128 { 129 if ((articletxt.Length - start) < pagesize) 130 { 131 if (pig == 0) 132 { 133 pagecount = pagecount + 1; 134 } 135 pig = 1; 136 } 137 else 138 { 139 pagecount = pagecount + 1; 140 } 141 } 142 if (start + pagesize >= articletxt.Length) //如果起始位置到查询的范围超出整个内容的长度,那么这一页的终点位置为内容的终点 143 { 144 stops = articletxt.Length; 145 } 146 if (pp == j + 1) //如果是当前,那么输出当前页的内容 147 { 148 articletext = articletxt.Substring(start, stops - start); //取内容的起始位置到终点位置这段字符串输出 149 returnHash["content"] = articletext; 150 } 151 start = stat; //将终点位置作为下一页的起始位置 152 } 153 }// pagecount = pagecount - 1; 154 } 155 else 156 { 157 returnHash["content"] = n_content; 158 } 159 //分页部分(这里就简单多了) 160 //定义分页代码变量 161 if (pagecount > 1) //当页数大于1的时候我们显示页数 162 { 163 if (pp - 1 > 0) //显示上一页,方便浏览 164 { html = html + "<a href=\"" + linkurl + "&page=" + (pp - 1) + "\">[上一页]</a> "; } 165 else 166 { 167 if (pp == 1) 168 { html = html + "[<font color=#cccccc>上一页</font>] "; } 169 else 170 { html = html + "<a href=\"" + linkurl + "&page=\"" + (1) + "\">[上一页]</a> "; } 171 } 172 for (int i = 1; i <= pagecount; i++) 173 { 174 if (i == pp) //如果是当前页,加粗显示 175 { html = html + "<b>[" + i + "]</b> "; } 176 else 177 { html = html + "<a href=\"" + linkurl + "&page=" + i + "\">[" + i + "]</a> "; } 178 } 179 if (pp + 1 > pagecount) //显示下一页,方便浏览 180 { 181 if (pp == pagecount) 182 { html = html + "[<font color=#cccccc>下一页</font>] "; } 183 else 184 { html = html + "<a href=\"" + linkurl + "&page=" + (pagecount) + "\">[下一页]</a></p>"; } 185 } 186 else 187 { 188 html = html + "<a href=\"" + linkurl + "&page=" + (pp + 1) + "\">[下一页]</a></p>"; 189 } 190 } 191 returnHash["pagetxt"] = html; 192 return returnHash; 193 } 194 195 #endregion 196 197 }
就这么简单
转载于:https://www.cnblogs.com/haylim/articles/4173812.html
标签:ASP,string,articletxt,System,写点,html,objNews,using,NET 来源: https://blog.csdn.net/weixin_30932215/article/details/97377249