新寫的一個:文章內容分頁顯示的代碼

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

 

===================

測試用例文件(20091795819.html):

 

<p>計算機和軟件技術日新月異,作爲一名合格的IT工程師,必須善於學習,及時瞭解和掌握<a href="http://www.csai.cn/incsearch/search.asp?key=%D0%C2%BC%BC%CA%F5" target="_blank">新技術</a>、新方法。然而新技術、新方法總是層出不窮,常常讓IT人員無所適從,疲憊應付。下面我想談談我對新技術、新方法的一個歸類性的介紹,以使大家心中有一個全局的把握,並理清自己感興趣的方向,從而作進一步研究,專注而專業地從事IT工作。</p>
<p>  <strong>一、</strong><a href="http://book.csai.cn/viewbook.asp?id=85"><strong>信息安全新技術</strong></a></p>
<p>  主要包括密碼技術、入侵檢測系統、信息隱藏技術、身份<a href="http://www.csai.cn/incsearch/search.asp?key=%C8%CF%D6%A4" target="_blank">認證</a>技術、數據庫<a href="http://www.csai.cn/incsearch/search.asp?key=%B0%B2%C8%AB" target="_blank">安全</a>技術、 網絡容災和災難恢復、<a href="http://www.csai.cn/incsearch/search.asp?key=%CD%F8%C2%E7%B0%B2%C8%AB" target="_blank">網絡安全</a><clk></clk><nobr oncontextmenu="return false" onmousemove="kwM(1)" id="clickeyekey1" onmouseover="kwE(event,1, this)" style="color: #6600ff; border-bottom: #6600ff 1px dotted; background-color: transparent; text-decoration: underline" onclick="kwC(event,1,&quot;&quot;)" onmouseout="kwL(event,this)">設計</nobr>等。隨着網絡時代的到來,網絡已經改變了人們的生活和工作方式。<a href="http://www.csai.cn/incsearch/search.asp?key=%BB%A5%C1%AA%CD%F8" target="_blank">互聯網</a>技術、<a href="http://www.csai.cn/incsearch/search.asp?key=%CE%DE%CF%DF" target="_blank">無線</a><a href="http://www.csai.cn/incsearch/search.asp?key=%CD%F8%C2%E7%BC%BC%CA%F5" target="_blank">網絡技術</a>以及<a href="http://www.csai.cn/incsearch/search.asp?key=%D0%C5%CF%A2%BB%AF" target="_blank">信息化</a>的不斷深入和發展,已經出現了在線購物、在線炒股、<a href="http://www.csai.cn/incsearch/search.asp?key=%CA%D6%BB%FA" target="_blank">手機</a><clk></clk><nobr oncontextmenu="return false" onmousemove="kwM(0)" id="clickeyekey0" onmouseover="kwE(event,0, this)" style="color: #6600ff; border-bottom: #6600ff 1px dotted; background-color: transparent; text-decoration: underline" onclick="kwC(event,0,&quot;&quot;)" onmouseout="kwL(event,this)">銀行</nobr>及網上辦公等各種各樣的信息應用。這些技術和方法是任何一位從事信息安全相關工作的人士必須具備的知識。</p>
<p>  <strong>二、</strong><a href="http://book.csai.cn/viewbook.asp?id=84"><strong>信息化新技術</strong></a></p>
<p>  信息化新技術主要涉及<a href="http://www.csai.cn/incsearch/search.asp?key=%B5%E7%D7%D3%D5%FE%CE%F1" target="_blank">電子政務</a>、<a href="http://www.csai.cn/incsearch/search.asp?key=%B5%E7%D7%D3%C9%CC%CE%F1" target="_blank">電子商務</a>、城市信息化、企業信息化、農業信息化、服務業信息化等。例如根據國家的信息化指導方針,城市信息化必須與工業化結合進行,城市信息化必須以工業化爲基礎,在推進工業化的基礎上推進信息化,因此,工業化就成爲信息化的應用基礎。</p>
<p>  <strong>三、</strong><a href="http://book.csai.cn/viewbook.asp?id=83"><strong>軟件新技術</strong></a></p>
<p><clk></clk>  軟件新技術主要<nobr oncontextmenu="return false" onmousemove="kwM(2)" id="clickeyekey2" onmouseover="kwE(event,2, this)" style="color: #6600ff; border-bottom: #6600ff 1px dotted; background-color: transparent; text-decoration: underline" onclick="kwC(event,2,&quot;&quot;)" onmouseout="kwL(event,this)">關注</nobr><a href="http://www.csai.cn/incsearch/search.asp?key=%C7%B6%C8%EB%CA%BD" target="_blank">嵌入式</a>計算與嵌入式軟件、基於構件的軟件開發方法、中間件技術、數據中心的建設、可信網絡計算<a href="http://www.csai.cn/incsearch/search.asp?key=%C6%BD%CC%A8" target="_blank">平臺</a>、軟件架構設計、<a href="http://www.csai.cn/incsearch/search.asp?key=SOA" target="_blank">SOA</a>與RIA技術、軟件產品線技術等。隨着對象技術與<a href="http://www.csai.cn/incsearch/search.asp?key=%B7%D6%B2%BC%CA%BD" target="_blank">分佈式</a>計算技術的發展,兩者相互結合形成了分佈對象計算,並發展爲當今軟件技術的主流方向。</p>
<p>  <strong>四、</strong><a href="http://book.csai.cn/viewbook.asp?id=82"><strong>網絡新技術</strong></a></p>
<p>  網絡新技術包括寬帶無線與移動通信、光通信與智能光網絡、家庭網絡與智能終端、寬帶多媒體網絡、IPv6與下一代網絡、分佈式系統等。雖然有線的<a href="http://www.csai.cn/incsearch/search.asp?key=%D7%CA%D4%B4" target="_blank">資源</a>是無限,而無線資源是有限,但以WLAN爲代表的無線寬帶網技術將無線和有線無縫地結合起來,從而創造出無限資源和無限應用。</p>
<p>  <strong>五、</strong><a href="http://book.csai.cn/viewbook.asp?id=81"><strong>計算機新技術</strong></a></p>
<p>  計算機新技術主要關注網格計算、人機接口、高性能計算和高性能<a href="http://www.csai.cn/incsearch/search.asp?key=%B7%FE%CE%F1%C6%F7" target="_blank">服務器</a>、智能計算、磁<a href="http://www.csai.cn/incsearch/search.asp?key=%B4%E6%B4%A2" target="_blank">存儲</a>技術、光存儲技術、中文信息處理與智能人機交互、數字媒體與內容管理、音<a href="http://www.csai.cn/incsearch/search.asp?key=%CA%D3%C6%B5" target="_blank">視頻</a>編/解碼技術等。其中網格技術有一個角度是發展IPv6,將地址空間由32位擴展到128位,這樣,原來有限的IP地址將變得無限豐富;另一個角度是發展網格技術,更好地管理網上的資源,將之虛擬成爲一個空前強大的一體化信息系統,在動態變化的網絡環境中,共享資源和協同解決問題,從而讓用戶從中享受可靈活控制的、智能的、協作式的信息服務,並獲得前所未有的使用方便性和超強能力。</p>
<p>  爲高度深縮以上新技術、新方法,<a href="http://www.csai.cn/incsearch/search.asp?key=%CF%A3%C8%FC%CD%F8" target="_blank">希賽網</a>適時組織權威專家<a href="http://www.csai.cn/incsearch/search.asp?key=%CD%C5%B6%D3" target="_blank">團隊</a>組編了《IT新技術寶典系列》叢書,傾情奉獻給每一位IT從業人事,詳情可見<a href="http://book.csai.cn/">http://book.csai.cn</a>(可以在線閱讀、按需印刷)。</p>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章