网页静态化有很多含义,在Web开发中,静态网页一般被理解成网站中大部分超链接所引用的
页面或者是单独的HTML静态页面.使用网页静态化技术可以减少对服务器的负担,增快访问
速度.
下面对网页静态化进行详细讲解:
网页静态化实例:(简单留言系统,在aspx页面添加留言后,添加到后台数据库,并且在HTML页,原理就是通过在aspx后台代码中用语句来替换html页中的字段,达到在html中显示的效果
面中显示出来.
1.新建两个页面 AddData.aspx(添加留言页面),HtmlTemp.htm(静态模板页面),并且在网站根目录下新建一个文件夹Html,用于存放静态网页
HtmlTemp.htm页面实现
<head>
<title>{Title}</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<body style="text-align:center;">
<table cellpadding="5" cellspacing="0" width="750px" border="2"> <!--此页面
用于网页静态化的模板,显示添加的信息-->
<tr>
<td colspan="2" style="font-size: 20pt; background-color: #ff3366">网站留
言</td>
</tr>
<tr>
<td>留言人:</td>
<td>{AddUserName}</td>
</tr>
<tr>
<td>留言信息:</td>
<td>{AddMsg}</td>
</tr>
<tr>
<td align="right" colspan="2">
留言添加时间:{AddTime}
</td>
</tr>
</table>
</body>
要注意的是统一的编码格式UTF-8
2.AddData.aspx页面编写
前台:
<body style="width:500px; text-align:center;">
<form id="form1" runat="server">
<div>
<table width="750px">
<tr>
<td colspan="4">用户留言</td>
</tr>
<tr>
<td style="width:100px;">留言人</td>
<td style="text-align: left"><asp:TextBox ID="name"
runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style=" width:100px; height: 191px;">留言信息</td>
<td style="height: 191px"><asp:TextBox ID="Msg" TextMode="multiline"
Columns="50" runat="server" Rows="10" Height="182px"
Width="631px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="4"><asp:Button ID="btn" runat="server" Text="添加"
OnClick="btn_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码(重要):
要在头部添加引用:
using System.Data.SqlClient;
using System.Text;
using System.IO;
static string CnStr = Connection.connection();
SqlConnection con = new SqlConnection(CnStr);
SqlCommand cmd;
SqlParameter pm;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
string UName = this.name.Text;
string Msg = this.Msg.Text;
Msg = Msg.Replace("\n", "<br />");
string Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string FileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")
+".htm";
OutputHtml(UName, Msg, Time, FileName);
}
private void OutputHtml(string t1, string t2, string t3, string FName)
{
string HtmlPath = String.Format(@"\Html\{0}", FName);
string HtmlTemp = File.ReadAllText(Server.MapPath(".") +
@"\HtmlTemp.htm", Encoding.UTF8);
StringBuilder SBuilder = new StringBuilder(HtmlTemp);
SBuilder.Replace("{Title}", t1 + "的留言");
SBuilder.Replace("{AddUserName}", t1);
SBuilder.Replace("{AddMsg}", t2);
SBuilder.Replace("{AddTime}", t3);
StreamWriter SWriter = File.CreateText(Server.MapPath(".") +
@"\Html\" + FName);
SWriter.WriteLine(SBuilder.ToString());
SWriter.Flush();
SWriter.Close();
AddRow(t1, t2, t3, HtmlPath);
}
private void AddRow(string t1, string t2, string t3, string t4)
{
string Sqlstr = "insert into [tb_Message] ([name],[message],
[time],[htmlpath]) values(@UName,@Msg,@Time,@FPath)";
cmd = new SqlCommand(Sqlstr, con);
pm = new SqlParameter("@UName", SqlDbType.VarChar, 20);
pm.Value = t1;
cmd.Parameters.Add(pm);
pm = new SqlParameter("@Msg", SqlDbType.NText);
pm.Value = t2;
cmd.Parameters.Add(pm);
pm = new SqlParameter("@Time", SqlDbType.DateTime);
pm.Value = t3;
cmd.Parameters.Add(pm);
pm = new SqlParameter("@FPath", SqlDbType.VarChar, 20);
pm.Value = t4;
cmd.Parameters.Add(pm);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.name.Text = string.Empty;
this.Msg.Text = string.Empty;
Response.Redirect(t4);
}
这样当我们添加数据后就会转到相应的html页面显示我们添加的数据,实现了网页静态化.