熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET實現新聞頁面的分頁功能

2013-11-13 10:32:29  來源: .NET編程 
    //生成靜態網頁
    string path = ServerMapPath(~/news/ + folder + /
    string file_template_name = ServerMapPath(~/news/    //新聞模版文件
    string file_template_content = ;
    StreamReader sr_reader = new StreamReader(file_template_name EncodingGetEncoding(gb))
    file_template_content = sr_readerReadToEnd()
    sr_readerClose()
    string[] subContent = FileSplit(content)
    string file_content = ;
    int pageNum = ;
    while (pageNum < && subContent[pageNum] != && subContent[pageNum] != null)
    pageNum++;
    for (int index = ; index < pageNum; index++)
    {
    file_content = file_template_content;
    file_content = file_contentReplace($$category list_departmentSelectedItemText)     //新聞類別
    file_content = file_contentReplace($$title title)                //新聞標題
    file_content = file_contentReplace(<!來源$$author> 來源 + author)         //作者
    file_content = file_contentReplace($$time time)                      //添加時間
    file_content = file_contentReplace($$content subContent[index])        //新聞正文
    string pageLink = ;
    int firstPage = ;
    //生成頁碼
    if (index > ) firstPage = index ;
    for (int i = firstPage; i < index + && i < pageNum; i++)
    {
    if (i == index)
    pageLink = pageLink + [ + (index + ) + ] + &nbsp; &nbsp; &nbsp; &nbsp;;
    else pageLink = pageLink + <a + htmlfilename + i + >[ + (i + ) + ]</a>&nbsp; &nbsp; &nbsp; &nbsp;;
    }
    if (index < pageNum
    pageLink = pageLink + <a + htmlfilename + (index + ) + > + 下一頁 + </a>&nbsp; &nbsp; &nbsp; &nbsp;;
    if (index >
    pageLink = <a + htmlfilename + (index ) + > + 上一頁 + </a>&nbsp; &nbsp; &nbsp; &nbsp; + pageLink;
    file_content = file_contentReplace(<! $$pageLink > pageLink)
    if (index == pageNum &&attachment_filename != null && attachment_filename != StringEmpty)
    {
    string[] attachment = attachment_filenameSplit(new char[] { | })
    string attachmenthtml = <a + attachment[] + > + attachment[] + </a>;
    for (int j = ; j < attachmentLength; j++)
    attachmenthtml = attachmenthtml + <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a + attachment[j] + > + attachment[j] + </a>;
    file_content = file_contentReplace($$attachment attachmenthtml)
    }
    StreamWriter sw = new StreamWriter(path + htmlfilename + index + false EncodingGetEncoding(gb))
    swWrite(file_content)
    swFlush()
    swClose()
    }
    HyperLinkText = 預覽 + title;
    HyperLinkNavigateUrl = /news/ + folder + / + htmlfilename   + + ;
    HyperLinkVisible = true;
    txt_timeText = DateTimeNowToString(yyyyMMdd
    txt_titleText = ;
    txt_authorText = ;
    FreeTextBoxText = ;
    }
    catch (Exception e)
    {
    }
    }
    //將正文分成多個頁面
    protected string[] FileSplit(string fileContent)
    {
    int fileIndex = ;
    string[] splitedFile = new string[];
    while (fileContentLength > && fileIndex < )     //每頁至少個字符
    {
    if (fileContentIndexOf(<P> ) < ) break;
    splitedFile[fileIndex] = fileContentSubstring( fileContentIndexOf(<P> ))
    fileContent = fileContentRemove( splitedFile[fileIndex]Length)
    fileIndex++;
    }
    splitedFile[fileIndex] = fileContent;    //超過剩下部分全放第十頁
    return splitedFile;
    }
    }
From:http://tw.wingwit.com/Article/program/net/201311/13952.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.