ASP.NET生成静态页
核心技术:
HTMLPage文件夹,ModelHTML.htm文件中的ArticleTitle,ArticleContent都是要替换的区域
WriteFile(dr["ArticleTitle"].ToString(), dr["ArticleContent"].ToString(), dr["ID"].ToString());
1.前台
<head runat="server">
<title>ASP.NET生成静态网页</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<div style="text-align: center">
<table style=" 601px" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" style="height: 85px; text-align: center;"><strong>ASP.NET生成静态网页</strong></td>
</tr>
<tr>
<td colspan="3" rowspan="2" style="vertical-align: top; height: 67px; text-align: center">
<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" OnItemCommand="DataList1_ItemCommand">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<ItemTemplate>
<table cellpadding="0" cellspacing="0" style="font-size: 9pt; 563px">
<tr>
<td style=" 30px; height: 17px">ID:</td>
<td style=" 60px; height: 17px"><%#DataBinder.Eval(Container.DataItem,"ID") %></td>
<td style=" 50px; height: 17px">主题:</td>
<td colspan="3" style=" 354px; height: 17px; text-align: left;">
<%# DataBinder.Eval(Container.DataItem,"ArticleTitle") %>
</td>
<td colspan="1" style=" 70px; height: 17px">
<asp:LinkButton ID="lnkbtnCreateHTML" runat="server" Font-Size="9pt" ForeColor="Red">生成静态页</asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemStyle BackColor="#F7F7F7" />
<ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
</asp:DataList></td>
</tr>
<tr>
<td colspan="3" rowspan="1"><strong>查看生成页</strong></td>
</tr>
<tr>
<td colspan="3" rowspan="1"><asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Width="230px"></asp:ListBox></td>
</tr>
<tr>
<td colspan="3" rowspan="1" style="vertical-align: top; height: 24px; text-align: center">
<asp:Button ID="btnView" runat="server" Font-Size="9pt" OnClick="btnView_Click" Text="查看页面" />
<asp:Button ID="btnDelete" runat="server" Font-Size="9pt" OnClick="btnDelete_Click"
Text="删除页面" /></td>
</tr>
</table>
</div>
</form>
</body>
2.后台
using System.Data.SqlClient; //添加引用
using System.Text;
using System.IO;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ConStr = "Server=(local);DataBase=db_17;Uid=sa;Pwd=";
string cmdtxt = "SELECT * FROM tb_13";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmdtxt, Con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DataList1.DataSource = ds;
this.DataList1.DataKeyField = "ID";
this.DataList1.DataBind();
//将文件绑定到 ListBox中
ArrayList arylst = new ArrayList();
string filepath = Server.MapPath("HTMLPage");
DirectoryInfo info = new DirectoryInfo(filepath);
FileInfo[] fileinfo = info.GetFiles();
foreach (FileInfo allfile in fileinfo)
{
arylst.Add(allfile);
}
this.ListBox1.DataSource = arylst;
this.ListBox1.DataBind();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
string ConStr = "Server=(local);DataBase=db_17;Uid=sa;Pwd=";
string cmdtxt = "SELECT * FROM tb_13 WHERE ID="+this.DataList1.DataKeys[e.Item.ItemIndex].ToString()+"";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand Com = new SqlCommand(cmdtxt,Con);
SqlDataReader dr = Com.ExecuteReader();
dr.Read();
if(dr.HasRows)
{
WriteFile(dr["ArticleTitle"].ToString(), dr["ArticleContent"].ToString(), dr["ID"].ToString());
Response.Write("<script>alert('静态页生成成功!');location='Default.aspx'</script>");
}
dr.Close();
}
public bool WriteFile(string ArticleTitle, string ArticleContent, string ArticleID)
{
string OutPutPath = HttpContext.Current.Server.MapPath("HTMLPage/");
Encoding encoding = Encoding.GetEncoding("gb2312");
// 读取模板文件
string ModelTemp = HttpContext.Current.Server.MapPath("ModelHTML.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(ModelTemp, encoding);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string HtmlFilename = DateTime.Now.ToString("yyyyMMddHHmmss_") +ArticleID+ ".html";
// 替换内容
str = str.Replace("PageTitle", ArticleTitle); //模板页中的PageArticle
str = str.Replace("ArticleTitle", ArticleTitle);
str = str.Replace("ArticleContent", ArticleContent);
// 写文件
try
{
sw = new StreamWriter(OutPutPath+HtmlFilename, false, encoding);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
protected void btnView_Click(object sender, EventArgs e)
{
Response.Redirect("HTMLPage/" + this.ListBox1.SelectedValue);
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string filepath = Server.MapPath("HTMLPage/");
File.Delete(filepath + this.ListBox1.SelectedValue);
Response.Write("<script>alert('页面删除成功!');location='Default.aspx'</script>");
}
}
3.ModelHTML.htm(静态页面模版)
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<title>PageTitle</title>
</head>
<body style="text-align: center">
<table style=" 700px; height: 601px">
<tr>
<td colspan="3" style="height: 106px" bgcolor="#00cc33">
<span style="font-size: 16pt; color: #3333ff"><strong>广告区</strong></span></td>
</tr>
<tr>
<td bgcolor="#00cc33" style=" 177px; height: 146px">
</td>
<td colspan="2" rowspan="3" style="margin-top: 0px; vertical-align: top; padding-top: 0px">
<table style=" 502px">
<tr>
<td colspan="3" style="height: 55px">
</td>
</tr>
<tr>
<td colspan="3" rowspan="2">
<strong>ArticleTitle </strong>
</td>
</tr>
<tr>
</tr>
<tr>
<td colspan="3" rowspan="1" style="text-align: left; font-size: 9pt;">ArticleContent</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>