asp.net 长内容文章自动分页前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="articleview.aspx.cs" Inherits="articleview" %> <%@ Register Src="~/head.ascx" TagName="head" TagPrefix="uc1" %> <%@ Register Src="~/right.ascx" TagName="right" TagPrefix="uc2" %> <%@ Register Src="~/foot.ascx" TagName="foot" TagPrefix="uc3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title><%# strTitle %>-评选动态-校园十大人物评选活动-山东建筑大学法政学院</title> <link href="css.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div class="warper"> <uc1:head ID="head" runat="server" /> <div class="content"> <div class="left_cont"> <div class="daohang">当前位置:<a href="default.aspx">首页</a> >> <a href="dongtai.aspx"> 评选动态</a> >> 正文</div> <div class="pxdt third"> <div class="cont_title"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div> <div class="a_page f_color">文章发表时间:<span><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></span> 来源:<span><asp:Label ID="Label3" runat="server" Text="Label"></asp:Label></span></div> <div class="body_cont"> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> <asp:Label ID="labPageNumber" Font-Size="14px" runat="server"> </asp:Label> </div> </div> </div> <uc2:right ID="right" runat="server" /> <div class="clear"></div> </div> <uc3:foot ID="foot" runat="server" /> </div> </form> </body> </html> 后台代码: 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 articleview : System.Web.UI.Page { DbClass.DbAccesscontrol conn = new DbClass.DbAccesscontrol(); public string strTitle = ""; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string strid = Request.QueryString["id"].ToString(); int fid = 1; try { fid = int.Parse(strid); } catch { } string strsql = "select * from YS_NewsInfo where id=" + fid; DataTable dt = (DataTable)conn.gettable(strsql); strTitle = dt.Rows[0]["title"].ToString(); Label1.Text = dt.Rows[0]["title"].ToString(); Label2.Text = dt.Rows[0]["datetime"].ToString(); Label3.Text = dt.Rows[0]["from"].ToString(); Label4.Text = OutputBySize(Server.HtmlDecode(dt.Rows[0]["info"].ToString()),fid); DataBind();//必需吗? } } //public string OutputArticle(string strcontent) //{ // string strid = Request.QueryString["id"].ToString(); // int fid = 1; // try // { // fid = int.Parse(strid); // } // catch // { } // string strsql = "select * from YS_NewsInfo where id=" + fid; // DataTable dt = (DataTable)conn.gettable(strsql); // //strTitle = dt.Rows[0]["title"].ToString(); // //Label1.Text = dt.Rows[0]["title"].ToString(); // //Label2.Text = dt.Rows[0]["datetime"].ToString(); // //Label3.Text = dt.Rows[0]["from"].ToString(); // //Label4.Text = OutputBySize(Server.HtmlDecode(dt.Rows[0]["info"].ToString())); // //DataBind(); // //return conn.NoHTML(Server.HtmlDecode(dt.Rows[0]["info"].ToString())); // return Server.HtmlDecode(dt.Rows[0]["info"].ToString()); //} public string OutputBySize(string p_strContent,int pageid) { string m_strRet = p_strContent; int m_intPageSize = 1000;//文章每页大小 int m_intCurrentPage = 1;//设置第一页为初始页 int m_intTotalPage = 0; int m_intArticlelength = p_strContent.Length;//文章长度 if (m_intPageSize < m_intArticlelength) {//如果每页大小大于文章长度时就不用分页了 if (m_intArticlelength % m_intPageSize == 0) {//set total pages count m_intTotalPage = m_intArticlelength / m_intPageSize; } else {//if the totalsize m_intTotalPage = m_intArticlelength / m_intPageSize + 1; } if (Request.QueryString["pages"] != null) {//set Current page number try {//处理不正常的地址栏的值 m_intCurrentPage = Convert.ToInt32(Request.QueryString["pages"]); if (m_intCurrentPage > m_intTotalPage) m_intCurrentPage = m_intTotalPage; } catch { m_intCurrentPage = m_intCurrentPage; } } //set the page content 设置获取当前页的大小 m_intPageSize = m_intCurrentPage < m_intTotalPage ? m_intPageSize : (m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1)); m_strRet = p_strContent.Substring(m_intPageSize * (m_intCurrentPage - 1), m_intPageSize); string m_strPageInfo = "<p></p>"; for (int i = 1; i <= m_intTotalPage; i++) { if (i == m_intCurrentPage) m_strPageInfo += "<b>" + i + "</b>|"; else m_strPageInfo += "<a href=articleview.aspx?id=" + pageid + "&pages=" + i + ">" + i + "</a>|"; } //输出显示各个页码 this.labPageNumber.Text = m_strPageInfo; } return m_strRet; } } |
|
来自: 悟静 > 《.net和asp.net》