ASP.NET-生成静态页面后台怎么调用动的数据
以下是.我用动态生成的静态页面代码.
摸版html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<title>ShowArticle</title>
<body>
ShowArticle
<br>
biaoti<br>
contents
</body>
</HTML>
后台代码
public static bool WriteFile(Compas pc)
{
string path = HttpContext.Current.Server.MapPath("~/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("~/news/text.htm");
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();
}
//用当前时间做为名称如20100324161954.html
string htmlfilename = pc.AddTime.ToString();
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("ShowArticle", pc.Compasstitle); //模板页中的ShowArticle
str = str.Replace("biaoti", pc.Compasstitle);
str = str.Replace("contents", pc.Compasscotent);
str = str.Replace("author", pc.Upuser);
// 写文件
try
{
sw = new StreamWriter(path + 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;
}
Compas pc = new Compas();
pc.Compasstitle = txtName.Text.Trim();
pc.Compasscotent = FCKeditor1.Value.ToString();
pc.UpMove = lbwork.Text.ToString();
pc.UpTime = DateTime.Now.ToString();
pc.AddTime = DateTime.Now.ToString("yyyyMMddHHmmss")+ ".html";
pc.Upuser = lbOperator.Text.ToString();
int planeReslut = CompasManager.Instance.AddCompas(pc);
if (planeReslut > 0)
{
if (WriteFile(pc))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
后台代码
public static bool WriteFile(Compas pc)
{
string path = HttpContext.Current.Server.MapPath("~/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("~/news/text.htm");
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();
}
//用当前时间做为名称如20100324161954.html
string htmlfilename = pc.AddTime.ToString();
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("ShowArticle", pc.Compasstitle); //模板页中的ShowArticle
str = str.Replace("biaoti", pc.Compasstitle);
str = str.Replace("contents", pc.Compasscotent);
str = str.Replace("author", pc.Upuser);
// 写文件
try
{
sw = new StreamWriter(path + 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;
}
Compas pc = new Compas();
pc.Compasstitle = txtName.Text.Trim();
pc.Compasscotent = FCKeditor1.Value.ToString();
pc.UpMove = lbwork.Text.ToString();
pc.UpTime = DateTime.Now.ToString();
pc.AddTime = DateTime.Now.ToString("yyyyMMddHHmmss")+ ".html";
pc.Upuser = lbOperator.Text.ToString();
int planeReslut = CompasManager.Instance.AddCompas(pc);
if (planeReslut > 0)
{
if (WriteFile(pc))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
后台想获取前台静态页的数据,需要在前台页面生成form标签,再用post或者get方法提交数据给后台
请学习ajax相关内容