第一步:新建一个模板aspx页面,最重要的是利用$htmlformat[0]、$htmlformat[1]等标记覆盖到你想替换的任意一处。
第二步:一个按钮的触发事件,事件代码如下:
protected void btnSave_Click(object sender, EventArgs e)
{
string content = "";
if (this.txtEditorContents.Text == "" || this.txtEditorContents.Text == null)
{
return;
}
else
{
content = this.txtEditorContents.Text;//读取富文本编辑器的内容
}
//调用模板页面
string[] format = new string[1];//定义和htmlyem标记数目一致的数组
format[0] = content;
string k = Guid.NewGuid().ToString();//新生成页面的名字
string newye = Creatfw_asp(format, k);
}
private string Creatfw_asp(string[] arry, string k)//arry为存放的信息 k为新生成的验证页面名字
{
string mubanyemian_path = AppDomain.CurrentDomain.BaseDirectory + "GwWebsite\\Home\\ShowAgencysAsp.aspx";//读取模板页面的在系统的存放地址
string[] format = new string[1];//定义和htmlyem标记数目一致的数组
StringBuilder htmltext = new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(mubanyemian_path))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);//读取模板页面的布局信息
}
sr.Close();
}
}
catch
{
Response.Write("<Script>alert('读取文件错误')</Script>");
}
//---------------------给标记数组赋值------------
format[0] = arry[0];
//----------替换htm里的标记为你想加的内容
for (int i = 0; i < 1; i++)
{
htmltext.Replace("$htmlformat[" + i + "]", format[i]);
}
//----------生成htm文件------------------――
try
{
string htmlfilename = k + ".aspx";
string save_path = AppDomain.CurrentDomain.BaseDirectory + "GwWebsite\\Home" + "\\" + htmlfilename;//保存在只放显示中介信息的页面的文件夹里
using (StreamWriter sw = new StreamWriter(save_path, false, System.Text.Encoding.GetEncoding("utf-8")))//"存放路径和页面名"
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
string yz_www = "~/GwWebsite/Home/" + htmlfilename;//这个域名要根据到时候申请的域名相关,先试下相对路径的存储定位
return yz_www;
}
catch
{
return null;
}
}