如题如果不是批量生成,也可以是自动生成的(即自动转换成静态的)急,问题一解决就结贴散分
解决方案 »
- 怎么在Ajax中验证一个<input type="text" name="市场比例">是从0到1的数
- SQL 服务器上未安装 ASP.NET 2.0 版会话状态,因此无法使用 SQL Server。请安装 ASP.NET 会话状态 SQL Server 2.0
- 简单的问题,解决立即结贴。
- 郁闷的问题,请教大家
- 菜鸟提问:怎么在母窗体中调用用户控件中的方法?
- 正则表达式匹配img标签的正则,并获得src属性
- C# DataGrid怎么用代码绑定字段
- 代碼的困惑!有一大堆asp的oa 工作流模板是xml,用他做什麽呢?
- 谁有visual Studio.net的注册码?
- web.config的问题
- 用户上传一个文件,有可能是PDF有可能是DOC文件,也可能是图片文件,该如何生成缩略图?
- 浏览器中实现C/S样式是怎么实现的和下拉框中如何实现树形菜单,高手请进,最后50分献上
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<body background="$htmlformat[0]" >
<table height="19%" border="0" width="85%"
cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px
solid #000000">
<tr>
<td width="71%" align="left" >
<span style="color: $htmlformat[1];font-size:
$htmlformat[2]">$htmlformat[3]</span>
</td>
</tr>
</table>
$htmlformat[4]
<br>
<p>$htmlformat[5]</p>
$htmlformat[6]
$htmlformat[7]
<br>
$htmlformat[8]
<br>
$htmlformat[9]</body>
</html>
.cs页面string[] format=new string[10];
//----------替换htm里的标记为你想加的内容
format[0]="logo.gif";//背景图片
format[1]= "#990099";//字体颜色
format[2]="50px";//字体大小
format[3]= "生成的模板html页面,现在是第 "+k.ToString()+" 页";
format[4]= str0;
format[5]= str1;
format[6]= "<a href=00"+ (k-1).ToString() +"tmp.html>上一页</a>";
if (k==0)
format[6]= "本页是第一页";
format[7]= "<a href=00"+ (k+1).ToString() +"tmp.html>下一页</a>";
if (k==count-1)
format[7]= "已经是最后一页了";
format[8]= str2;
format[9]= "<img border='0' src='../" + str3 +"'>";
//循环替换
for(int i=0;i<10;i++)
{
strhtml.Replace("$htmlformat["+i+"]",format[i]);
} string fname = Server.MapPath(".")+"\\createhtml1\\"+"00"+ k +"tmp.html";
using(StreamWriter sw =new StreamWriter(fname,false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
using System;
//记得添加以下三引用
using System.Text;
using System.Web;
using System.IO;
namespace TesConvert
{
/// <summary>
/// MyConvert 的摘要说明。
/// </summary>
public class MyConvert
{
public MyConvert()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/TesConvert/news/");//定义html文件存放路径
Encoding code = Encoding.GetEncoding("gb2312");//定义文字编码
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/TesConvert/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=path + DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("title",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
try
{
sw = new StreamWriter(htmlfilename,false,code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
}
}
2、TestNews.aspx文件:
添加三和TextBox分别为:tbx_Title、tbx_Content、tbx_Author和一个Button:btn_AddNews。
TestNews.aspx.cs文件
private void btn_AddNews_Click(object sender, System.EventArgs e)
{
MyConvert Hover = new MyConvert();
if(Hover.WriteFile(this.txb_Title.Text.ToString(),Server.HtmlDecode(this.txb_Content.Value),this.txb_Author.Text.ToString()))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
}
3、添加模板text.html文件
<head>ShowArticle</head>
<body>
title<br/>
content<br/>
author
</body>
说明:一.news文件夹必须赋予asp.net用户写入的权限。这是一个简单的实现例子,实际项目必须先将数据保存到数据库下面,在datagird中调用数据库下面html文件的URL地址。二.默认情况下,我们是不能向TextBox、TextArea中添加html语法的,必须修改config文件,在<system.web>下面添加<pages validateRequest="false" />,但是这样做的话,整个项目中都允许键入html标签了,暂时还不知道其他的方。
缺点:这种方法是在ASP.net在页面所有内容生成后、输出内容前对页面内容进行操作以前曾说过用HttpModule来在Response前更改,不够灵活,每行修改response,比较费力。