惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
小众软件
小众软件
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
Jina AI
Jina AI
博客园 - 【当耐特】
V
Visual Studio Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
量子位
IT之家
IT之家
G
Google Developers Blog
V
V2EX
The GitHub Blog
The GitHub Blog
月光博客
月光博客
GbyAI
GbyAI

博客园 - Jingnan

微信支付开发 c# SDK JSAPI支付开发的流程和微信大坑 T4模板根据数据库表和列的Description生成代码的summary的终极解决方案 微信开放平台 公众号第三方平台开发 教程五 代公众号发起网页授权源码 微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo 微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台 微信开放平台 公众号第三方平台开发 教程二 创建公众号第三方平台 微信开放平台 公众号第三方平台开发 教程一 平台介绍 wcf几种调用方式的性能讨论 WCF同样功能不同写法的性能问题 ASP.NET 4.0 URL路由改进支持 WPF 用webBowser 嵌套 URL地址 显示ReportViewer导出EXCEL的一些问题 简单的封装一个HTML 弹出对话框的空间 原型模式(Prototype) 恶性代码 - Jingnan - 博客园 AdvDataList分页 例码 问一个关于生成静态页面的问题 简单读区XML - Jingnan - 博客园 使用XMLDataSource简单实现多级下拉菜单 更新 增加 用Table形式
ASP.NET实现新闻页面的分页功能[生成静态版]
Jingnan · 2008-03-25 · via 博客园 - Jingnan

//生成静态网页

             
string path = Server.MapPath("~/news/" + folder + "/");

             
string file_template_name = Server.MapPath("~/news/template.htm");    //新闻模版文件
             string file_template_content = "";
             StreamReader sr_reader 
= new StreamReader(file_template_name, Encoding.GetEncoding("gb2312"));
             file_template_content 
= sr_reader.ReadToEnd();
             sr_reader.Close();
             
string[] subContent = FileSplit(content);
             
string file_content = "";
             
int pageNum = 0;
             
while (pageNum < 10 && subContent[pageNum] != "" && subContent[pageNum] != null)
                 pageNum
++;
             
for (int index = 0; index < pageNum; index++)
             
{
                 file_content 
= file_template_content;
                 file_content 
= file_content.Replace("$$category", list_department.SelectedItem.Text);     //新闻类别
                 file_content = file_content.Replace("$$title", title);                //新闻标题
                 file_content = file_content.Replace("<!--来源:$$author-->""来源:" + author);         //作者
                 file_content = file_content.Replace("$$time", time);                      //添加时间
                 file_content = file_content.Replace("$$content", subContent[index]);        //新闻正文
                 string pageLink = "";
                 
int firstPage = 0;
                
//生成页码
                 if (index > 2) firstPage = index - 2;
                 
for (int i1 = firstPage; i1 < index + 3 && i1 < pageNum; i1++)
                 
{
                     
if (i1 == index)
                         pageLink 
= pageLink + "[" + (index + 1+ "]" + "&nbsp; &nbsp; &nbsp; &nbsp;";
                     
else pageLink = pageLink + "<a href=" + htmlfilename + i1 + ".htm>[" + (i1 + 1+ "]</a>&nbsp; &nbsp; &nbsp; &nbsp;";

                 }

                 
if (index < pageNum - 1)
                     pageLink 
= pageLink + "<a href=" + htmlfilename + (index + 1+ ".htm>" + "下一页" + "</a>&nbsp; &nbsp; &nbsp; &nbsp;";
                 
if (index > 0)
                     pageLink 
= "<a href=" + htmlfilename + (index - 1+ ".htm>" + "上一页" + "</a>&nbsp; &nbsp; &nbsp; &nbsp;" + pageLink;
                 file_content 
= file_content.Replace("<!-- $$pageLink -->", pageLink);
                 
if (index == pageNum - 1&&attachment_filename != null && attachment_filename != String.Empty)
                 
{
                     
string[] attachment = attachment_filename.Split(new char[] '|' });
                     
string attachmenthtml = "<a href="attachment/" + attachment[0] + "" >" + attachment[0+ "</a>";
                     
for (int j = 1; j < attachment.Length; j++)
                         attachmenthtml 
= attachmenthtml + "<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="attachment/" + attachment[j] + "" >" + attachment[j] + "</a>";
                     file_content 
= file_content.Replace("$$attachment", attachmenthtml);
                 }

                 StreamWriter sw 
= new StreamWriter(path + htmlfilename + index + ".htm"false, Encoding.GetEncoding("gb2312"));
                 sw.Write(file_content);
                 sw.Flush();
                 sw.Close();
             }

             HyperLink1.Text 
= "预览: " + title;
             HyperLink1.NavigateUrl 
= "../news/" + folder + "/" + htmlfilename   + "0" + ".htm";
             HyperLink1.Visible 
= true;
             txt_time.Text 
= DateTime.Now.ToString("yyyy-MM-dd");
             txt_title.Text 
= "";
             txt_author.Text 
= "";
             FreeTextBox1.Text 
= "";
         }
         
catch (Exception e)
         
{
         }

     }
    
     
//将正文分成多个页面
protected string[] FileSplit(string fileContent)
     
{
         
int fileIndex = 0;
         
string[] splitedFile = new string[10];
         
while (fileContent.Length > 1500 && fileIndex < 9)     //每页至少1500个字符
         {
             
if (fileContent.IndexOf("<P>"1500< 0break;
             splitedFile[fileIndex] 
= fileContent.Substring(0, fileContent.IndexOf("<P>"1500));
             fileContent 
= fileContent.Remove(0, splitedFile[fileIndex].Length);
             fileIndex
++;
         }

         splitedFile[fileIndex] 
= fileContent;    //超过9页,剩下部分全放第十页
         return splitedFile;
     }

}