.net生成静态页面并生成分页代码
int num = 0;
string sql = "insert into news(title,content,bigclassname,[user],infotime,hits,upfile) values(@title,@content,@ddl,@name,@time,@num,@filename)";
oledbparameter[] para = new oledbparameter[] {
new oledbparameter("@title",txttitle.text),
new oledbparameter("@content",fckeditor1.value),
new oledbparameter("@ddl",ddllei.selecteditem.value),
new oledbparameter("@name",txtname.text),
new oledbparameter("@time",txttime.text),
new oledbparameter("@num",num),
new oledbparameter("@filename",filename)
};
db.executecommand(sql, para);
string mbpath = server.mappath("../template/news.html");
encoding code = encoding.getencoding("gb2312");
streamreader sr = null;
streamwriter sw = null;
string str = null;//读取
try
{
sr = new streamreader(mbpath, code);
str = sr.readtoend();}
catch (exception ex)
{
throw ex;
}
finally
{
sr.close();
}
string strmax = "select id from news order by id desc";
int newsid = convert.toint32(db.readout2(strmax))+1;
//根据时间自动重命名,扩展名也可以自行修改
string filename = "news_" + newsid + ".shtml";str = str.replace("#title#", txttitle.text);
str = str.replace("#infotime#", txttime.text);
str = str.replace("#content#", fckeditor1.value);
//生成静态文件
try
{
sw = new streamwriter(server.mappath("http://www.cnblogs.com/") + filename, false, code);
sw.write(str);
sw.flush();fenye();
}
catch (exception ex)
{
throw ex;
}
finally
{
sw.close();
}
response.write("<script language=网页特效>alert('添加成功!');location.href='newsmanage.asp教程x';</script>");//分页
public void fenye()
{
string str = "select * from news order by id desc";
datatable tb = db.mydataadapter(str);
datarow dr;
//最后一页是否整页
int m = 0;
//页数
int pg = 0;
if (tb.rows.count % 10 == 0)
{
pg = tb.rows.count / 10;
}
else
{
pg = convert.toint32(tb.rows.count / 10) + 1;
m = 1;
}//分页
string pglist = "";
for (int i = 1; i <= pg; i++)
{
pglist += " <a href="" + i + ".html">" + i + "</a>";
}
pglist = "<div >共"+pg+"页 <a href="1.html">首页</a>" + pglist + "<a href="" + pg + ".html"> 尾页</a></div> ";
for (int j = 1; j <= pg; j++)
{
//建立一个stringbuilder对象
stringbuilder sb = new stringbuilder();//读取模板
using (streamreader sr = new streamreader(server.mappath("../template/index.html"), system.text.encoding.default, true))
{//将模板写入streambuilder
sb.append(sr.readtoend().tostring());
}//删除原有文件
// file.delete("../....//news/" + j.tostring() + ".html");string title = "";
string ublist = "";
string infotime = "";
string id = "";
if (m == 1 && j == pg)//不是整页
{
//for (int i = 1 + 10 * (j - 1); i <= (tb.rows.count - 10 * (pg - 1)); i++)
for (int i = 1 + 10 * (j - 1); i <= (tb.rows.count); i++)
{
dr = tb.rows[i - 1];
title = dr["title"].tostring();
id = dr["id"].tostring();
infotime = dr["infotime"].tostring();
ublist += "<table border="0" cellpadding="0" cellspacing="0" style="width:610px; line-height:10px" >";
ublist += " <tr ><td align="left"> "+i+"<a href="news/news_" + id + ".shtml">→" + title + "</a> </td><td align="right" >" + infotime + "</td></tr></table>";
}
}
else//是整页
{
for (int i = 1 + 10 * (j - 1); i <= 10 * j; i++)
{
dr = tb.rows[i - 1];
id = dr["id"].tostring();
title = dr["title"].tostring().replace(" ", "");
infotime = dr["infotime"].tostring().replace(" ", "");
ublist += "<table border="0" cellpadding="0" cellspacing="0" style="width:610px; line-height:25px" >";
ublist += " <tr ><td align="left"> " + i + "<a href="news/news_" + id + ".shtml">→" + title + "</a> </td><td align="right" >" + infotime + "</td></tr></table> ";
}
}//替换模板中的内容...
sb.replace("$title$", ublist);
//替换分页
sb.replace("$pglist$", pglist);
using (streamwriter sw = new streamwriter(server.mappath("http://www.cnblogs.com/news/" + j.tostring() + ".html"), false, system.text.encoding.default, 100))
{//写出.html文件
sw.writeline(sb);sw.flush();
sw.close();
}}
}
一键生成静态页面
protected void btnhtml_click(object sender, eventargs e)
{
string sql = "select * from news order by id desc";
datatable dt = db.mydataadapter(sql);
if (dt.rows.count > 0)
{
for (int i = 0; i < dt.rows.count; i++)
{
string mbpath = server.mappath("../template/news.html");
encoding code = encoding.getencoding("gb2312");
streamreader sr = null;
streamwriter sw = null;
string str = null;//读取
try
{
sr = new streamreader(mbpath, code);
str = sr.readtoend();}
catch (exception ex)
{
throw ex;
}
finally
{
sr.close();
}
//根据时间自动重命名,扩展名也可以自行修改
string filename = "news_" + dt.rows[i]["id"].tostring() + ".shtml";
str = str.replace("#title#", dt.rows[i]["title"].tostring());
str = str.replace("#infotime#", dt.rows[i]["infotime"].tostring());
str = str.replace("#content#", dt.rows[i]["content"].tostring());
//生成静态文件
try
{
sw = new streamwriter(server.mappath("http://www.cnblogs.com/" + "news" + "/") + filename, false, code);
sw.write(str);
sw.flush();
this.page.clientscript.registerstartups教程cript(gettype(), "", "<script>alert('生成成功!')</script>");}
catch (exception ex)
{
throw ex;
}
finally
{
sw.close();
}
}}
else
{
this.page.clientscript.registerstartupscript(gettype(), "", "<script>alert('暂无数据')</script>");
}
}
补充:asp.net教程,.Net开发