在线客服
24小时免费咨询电话:18978941786
客服时间:上午9:30~下午6点
NET分页代码,生成静态页面
收藏 分享 发布日期:2012-2-12 16:30:43 编辑:admin 文章来源: 点击率:
using System.Data;
using System.Configuration;
using System.Collections;
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.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WriteFile(" 要好好学啊!! ", "祝你找到一份好工作!!!", "别忘了我", "Jiawenquan.html");
}
public void WriteFile(string strText, string content, string page, string name)
{
string path = HttpContext.Current.Server.MapPath("news/get");
System.Text.Encoding code = System.Text.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();
}
string htmlfilename = name.ToString();
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$title", strText); //模板页中的$title,即标题
str = str.Replace("$content", content);//模板页中的$content,即内容
str = str.Replace("$page", page);//模板页中的$page,即页码连接格式
// 写文件
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();
}
}
} 本文章由南宁网站建设、南宁网站优化、南宁网络公司整理,转载请注明出处:http://www.xqgg.net/
