博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.Net生成静态页面带分页

Posted on 2011-04-30 23:47  itcfj  阅读(304)  评论(1编辑  收藏  举报

模板页

<!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>
<title></title>
</head>
<body>
<div style="width: 417px; height: 54px" align="center">
<br />
Title
</div>
<div style="width: 417px; height: 8px">
浏览
<font color="red">
<script src="http://localhost/.Net/NewsFiles/ClickCount.aspx?NewsId=NewsId"> </script>
</font>次 Time
</div>
<div style="width: 417px; height: 100px">
Content
</div>
<div style="width: 416px; height: 9px">
Pager
</div>
<div style="width: 416px; height: 8px">
<form id="form1" action="../AddComment.aspx" style="margin: 0px">
<input id="Text1" type="text" />
<img id="Image1" src="http://www.dwww.cn/UserInfo/CheckCode.aspx" />
<br />
<textarea id="CommentContent" cols="20" rows="2"> </textarea>
<br />
<input id="NewsId" type="hidden" value="NewsId" />
<input id="Button1" type="submit" value="button" />
<a href="http://www.dwww.cn/News/Display.aspx?NewsId=NewsId">查看更多评论 </a>
</form>
</div>
</body>
</html>

生成代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string content = "发的发生的方法的是否 的|方式大范德萨费点事的的";
        string htmlStr = string.Empty;
        string[] contentStr = content.Split('|');
        string strTable = " <table> <tr> <td>upUrl </td> <td>Number </td> <td>downUrl </td> </tr> </table>";//上下页表格,注意此处的upUrl(上一页),Number(页码分页),downUrl(下一页) 
        int PageNumber = contentStr.Length;

        for (int m = 0; m <PageNumber; m++)
        {
            string numberStr = string.Empty;
            string filePath=string.Empty;
            for (int i = 1; i <= PageNumber; i++)
            {
                if (i == 1)
                {
                    numberStr += "[<a href='" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html" + "'>" + i + "</a>]";

                }
                else
                {
                    int p = i-1;
                    numberStr += "[<a href='" + DateTime.Now.ToString("yyyymmddhhmmss") + "_"+p+".html" + "'>" + i + "</a>]";
                   
                }
            }
            if (m==0)
            {
               filePath = Server.MapPath(".") + "//" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";

            }
            else
            {
                filePath = Server.MapPath(".") + "//" + DateTime.Now.ToString("yyyymmddhhmmss") + "_" + m + ".html";
            }

            using (StreamReader sr = new StreamReader(Server.MapPath("NewsFiles/template.html"),System.Text.Encoding.GetEncoding("gb2312"))) 
            {
                 htmlStr= sr.ReadToEnd();
            }
            htmlStr = htmlStr.Replace("Title", "生成静态页面分页");
            htmlStr = htmlStr.Replace("NewsId", "11");
            htmlStr = htmlStr.Replace("Time", DateTime.Now.ToString("yyyy/MM/dd"));
            htmlStr = htmlStr.Replace("Content", contentStr[m]);
            htmlStr=htmlStr.Replace("Pager", strTable.Replace("Number", numberStr));
            FileInfo file=new FileInfo (filePath);
            Stream fs=file.OpenWrite();
            using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312")))
            {
                sw.Write(htmlStr);

            }
            
       }
    }
}