Look at a lot of pages of the news is paging, I want to implement such a function to see their implementation of the method, basically a news into a number of HTML files saved, and then connected with the page number. (Do not know my understanding is correct, if not correct, please give me the advice twos)
1, from the database to obtain news content. Including news headlines, content and so on.
2, set the length of the string displayed on the page, get the character length of the news content. Do the initial setup
3, according to the length of the page to intercept the character of the news content.
4, get the template page. Replaces a string with a caption, content, and so on in the relevant substitution character.
5, save the new page to the development directory below
Here is the code and resource Manager content,
Solid.aspx Content
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using Syste
m.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 Solid:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {W Ritefile ("Olympic theme", "Olympic Games is awesome!")
"," 2 "," 2.html "); public void WriteFile (string strText, string content, String page, string name) {String path = Httpcont Ext.
Current.Server.MapPath ("News/get");
System.Text.Encoding code = System.Text.Encoding.GetEncoding ("gb2312");
Read template file 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 ();
Reading files The catch (Exception exp) {HttpContext.Current.Response.Write (exp.
message);
HttpContext.Current.Response.End (); Sr.
Close (); String htmlfilename = name.
ToString (); Replace///At this time, the template file has been read into the variable named str = str. Replace ("$title", StrText); $title in the template page, that is, title str = str. Replace ("$content", content);//$content in the template page, that is, contents str = str. Replace ($page, page),//$page in the template page, that is, page number connection format/write file try {sw = new StreamWriter (path +
"/" + Htmlfilename, False, code); Sw.
Write (str); Sw.
Flush (); The catch (Exception ex) {HttpContext.Current.Response.Write (ex).
message);
HttpContext.Current.Response.End (); finally {SW.
Close (); }
}
}
Text.htm Content
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<title>无标题页</title>
<body>
<div style="font-size: 20px; text-align: center;">文章分页测试</div>
<div style="font-size: 15px; text-align: center; color: #009966;">$title</div>
<div style="font-size: 12px; text-align: center; color: darkgray;">$page</div>
<div style="font-size: 12px;">$content</div>
</body>