62,072
社区成员
发帖
与我相关
我的任务
分享
//判断是否生成静态页面
public string isHtml(string html, int id)
{
if (string.IsNullOrEmpty(html))
{
return "<a href='JstaticPost.aspx?nid=" + id + "'>生成HTML</a>";
}
else
{
return html;
}
}
<td><%# isHtml(Eval("HtmlAddress").ToString(), Convert.ToInt32(Eval("NewsInfoId")))%></td>
public partial class JstaticPost : System.Web.UI.Page
{
string uid = string.Empty;
string title = string.Empty;
string cont = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["nid"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
if (Request["nid"] != null)
{
uid = Request["nid"].ToString();
}
else
{
Response.Redirect("JUser.aspx");
}
if (!IsPostBack)
{
GetTC();
Pub(title, cont);
}
}
}
//获取公告的新闻和标题
private void GetTC()
{
string getSqls = "select NewsTitle,NewsContent from NewsInfo where NewsInfoId="+uid;
SqlDataReader dr = DbSqlHelper.ExecuteReader(getSqls);
try
{
if (dr.Read())
{
title = dr["NewsTitle"].ToString();
cont = dr["NewsContent"].ToString();
}
}
catch
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('对不起,功能正在维护中........');</script>");
}
finally
{
dr.Close();
}
}
//生成静态页面
private void Pub(string title,string con)
{
FileStream fs = new FileStream(Server.MapPath("HTML/News.htm"),FileMode.Open);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadToEnd();
sr.Close();
//替换静态页面的内容
str = str.Replace("{ping:title}",title);
str = str.Replace("{ping:content}",con);
//1
string filename = DateTime.Now.ToString("yyyyMMddhhmmss")+".htm";
string path = Server.MapPath("/NewsHtml/")+filename;
StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.Default);
sw.Write(str);
sw.Close();
//将静态页面保存在数据库
InDB(filename);
Response.Redirect("Jpost.aspx");
}
//将静态页面生成的名称存入数据库中
private void InDB(string ht)
{
string InDBsql = "update NewsInfo set HtmlAddress='"+ht+"' where NewsInfoId="+uid+"";
DataSet ds = DbSqlHelper.ExecuteDataSet(InDBsql);
}
using System;
using System.Data;
using System.Configuration;
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;
using System.Text;
using System.IO;
/// <summary>
/// 动态生成html页面的类
/// </summary>
public static class DymicHtml
{
public static string GoldCreateHtml(HttpServerUtility server, string title, string addTime, string contents)
{
Random rd = new Random();
string url = DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(1000, 9999);//保存动态生成的页面的名字
string[] newContent = new string[5];//定义和html标记数目一致的数组
StringBuilder strhtml = new StringBuilder();
try
{
//创建StreamReader对象
//解决模板乱码问题
// System.IO.StreamReader m_fs = new System.IO.StreamReader(Hfile_SelectFile.Value, System.Text.Encoding.GetEncoding("gb2312"));
//using (StreamReader sr = new StreamReader(server.MapPath("~/") + "Gold\\MgrSpdb\\common\\GoldNewsListTemplate.html", Encoding.GetEncoding("gb2312")))
using (StreamReader sr = new StreamReader(server.MapPath("~/") + "common\\HotFinacial.html", Encoding.GetEncoding("gb2312")))
{
string oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch (Exception err)
{
//输出异常信息
Console.WriteLine(err.ToString());
}
//为标记数组赋值
newContent[0] = title;//网页标题
newContent[1] = title;//正文标题
newContent[2] = addTime;
newContent[3] = contents;
newContent[4] = url;
//根据上面新的内容生成html文件
try
{
//若目录不存在,则新建目录
string dir = server.MapPath("~/") + "c\\" + DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
//指定要生成的HTML文件
//string fname = server.MapPath("~/") + "Gold\\MgrSpdb\\c\\" + url + ".html";
string fname = dir+"\\"+ url + ".html";
//替换html模版文件里的标记为新的内容
for (int i = 0; i < 5; i++)
{
strhtml.Replace("$htmlkey[" + i + "]", newContent[i]);
}
//创建文件信息对象
FileInfo finfo = new FileInfo(fname);
//以打开或者写入的形式创建文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
//把新的内容写到创建的HTML页面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
//设置超级链接的属性
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
return url;
}
}
//简单写了下!
string opath = "html.htm";
string path = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".html";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(Server.MapPath(opath), Encoding.UTF8))
{
sb.Append(sr.ReadToEnd().Replace("${title}", "csdn标题").Replace("${content}", "csdn内容"));
}
using (StreamWriter sw = new StreamWriter(Server.MapPath(path),true, Encoding.UTF8))
{
sw.Write(sb.ToString());
}
<!--html.htm-->
<!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>
${title}
${content}
</body>
</html>