Now do the program to convert the dynamic page into a static page, today teaches you to implement static page generation method in ASP.
Using System;
Using System.Data;
Using System.Configuration;
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;
Using System.Text;
///
Summary description of WriteFile
///
public class WriteFile
{
Public WriteFile ()
{
}
public static bool Createhtml (string[] strnewshtml,string[] stroldhtml,string strmodefilepath,string strPath)
{
BOOL flag = FALSE;
StreamReader sr = null;
StreamWriter SW = null;
string filepath = HttpContext.Current.Server.MapPath (Strmodefilepath);
Encoding code = encoding.getencoding ("gb2312");
string s = String. Empty;
Try
{
sr = new StreamReader (Filepath,code);
s = Sr. ReadToEnd ();
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}
Try
{
for (int i = 0; i < strnewshtml.length; i++)
{
s = S.replace (Stroldhtml[i], strnewshtml[i]);
}
SW = new StreamWriter (HttpContext.Current.Server.MapPath (strpath), false, code);
Sw. Write (s);
Flag = true;
}
catch (Exception ex)
{
Flag = false;
Throw ex;
}
Finally
{
Sw. Flush ();
Sw. Close ();
}
return flag;
}
public static bool Updatehtmlpage (string[] strnewshtml, string[] strstarthtml, string[] strendhtml, string strhtml)
{
BOOL Flage = false;
StreamReader readerfile = null;
StreamWriter wrirtefile = null;
String FilePath = HttpContext.Current.Server.MapPath (strhtml);
Encoding Code = encoding.getencoding ("gb2312");
String strfile = String. Empty;
Try
{
Readerfile = new StreamReader (FilePath, Code);
strfile = Readerfile.readtoend ();
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Readerfile.close ();
}
Try
{
int intlength = Strnewshtml.length;
for (int i = 0; i < intlength; i++)
{
int intstart = Strfile.indexof (Strstarthtml[i]) + strstarthtml[i]. Length;
int intend = Strfile.indexof (strendhtml[i]);
String stroldhtml = Strfile.substring (Intstart, Intend-intstart);
strfile = Strfile.replace (stroldhtml, strnewshtml[i]);
}
Wrirtefile = new StreamWriter (FilePath, False, Code);
Wrirtefile.write (strfile);
Flage = true;
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Wrirtefile.flush ();
Wrirtefile.close ();
}
return flage;
}
}
Call public class:
----------------------------------------------------------------------------
protected void button2_click (object sender, EventArgs e)
{
String newstitle = this. TextBox1.Text;
String newskindname = this. DropDownList1.SelectedItem.Text;
String newsbody = this. Webeditor1.text;
DateTime pubtime = DateTime.Now;
String UserName = session["UserName"]. ToString ();
Response.Write (Newskindname);
string[] strnewshtml = new string[] {newstitle, newskindname, Newsbody, pubtime.tostring (), UserName};
string[] stroldhtml = new string[] {"@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName"};
String strFileName = DateTime.Now.ToString ("DDHHMMSS") + ". html";
String strFilePath = String. Format ("newshtml/{0}", strFileName);
Try
{
if (writefile.createhtml (strnewshtml, stroldhtml, "mode.htm", strFilePath))
{
This. Label1.Text = "Build succeeded!";
}
Else
{
This. Label1.Text = "Build failed!";
}
}
Catch
{
This. Label1.Text = "Build failed!";
}
}
protected void Button3_Click (object sender, EventArgs e)
{
String[] Strnewshtml=new string[]{"To this trip!"};
String[] strstarthtml=new string[]{""};
String[] strendhtml=new string[]{""};
if (Writefile.updatehtmlpage (strnewshtml, strstarthtml, strendhtml, "newshtml/02011139.html"))
{
This. label1.text= "Generate homepage success!";
}
Else
{
This. Label1.text= "Build home failed!";
}
}
Create new folder newshtml, generate HTML file to put inside
-----------------------------------------------------------
Add a template file
<title> Untitled Page </title>
<body>
<table border= "1" cellpadding= "0" cellspacing= "0" style= "WIDTH:522PX; Height:338px ">
<tr>
<TD align= "center" colspan= "2" >
@Title </td>
</tr>
<tr>
<TD align= "center" colspan= "2" >
Publisher: @UserName Release time: @PubTime News Category: @NewsKInd </td>
</tr>
<tr>
<TD colspan= "2" >
@NewsBody </td></tr><tr>
<TD style= "width:100px" >
</TD><TD style= "width:100px" >
</td></tr></table></body>
Reprinted from: http://www.aspnetjia.com
ASP. NET generates implementation code for static Web pages