如何动态生成HTML页面

html-css012

如何动态生成HTML页面,第1张

以最简单的新闻网页为例,先是做一个模板页,假定其中3个地方要动态更换的:标题,内容,日期,哪么模板中这3个地方都要用特殊的名称表示。

要生成html页时,先读入html模板页,再查询数据库,把查询到标题,内容,日期的实际值替换模板中的特殊名称,并按规则命名另存这个文件。

以上是动态生成html页的基本原理,实际工作中,可灵活处理,比如一次性生成所有页,或者发布一个生成一个,可以用一个模板,也可以根据类别用3个5个模板。模板可以保存为文件文件,也可保存在数据库中等等。

?

动态生成的HTML标签样式一般可以使用2中方式确定

1、在生成HTML时将样式直接以style=""写在标签上;也就是使用行内样式

例如:

//动态生成html时,给html赋值行内样式

$("body").append("<div style='width:100pxheight:100pxbackground:red'>动态生成的div</div>")

2、预先定义好css样式,动态生成html时,将css类赋值给html

例:

/*定义css样式*/

.content{

    width:100px

    height:100px

    background:red

} //在动态生成html时,将css样式赋值class

$("body").append("<div class='content'>动态生成html赋值class样式</div>")

总结:在动态生成html标签时,直接使用行内样式快捷,但不容易修改;使用预先定义好的css样式赋值html标签的class属性,容易修改;建议使用后者。

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>

/// WriteFile 的摘要说明

/// </summary>

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 = 0i <strnewsHtml.Lengthi++)

{

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 = 0i <intLengThi++)

{

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

}

}

调用公共类:

----------------------------------------------------------------------------

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 = "生成成功!"

}

else

{

this.Label1.Text = "生成失败!"

}

}

catch

{

this.Label1.Text = "生成失败!"

}

}

protected void Button3_Click(object sender, EventArgs e)

{

string[] strNewsHtml=new string[]{"游!"}

string[] strStartHtml=new string[]{"<!-- start -->"}

string[] strEndHtml=new string[]{"<!--end-->"}

if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html"))

{

this.Label1.Text="生成首页成功!"

}

else

{

this.Label1.Text="生成首页失败!"

}

}

新建文件夹NewsHtml,生成html文件放在里面

-----------------------------------------------------------

增加一个模板文件

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

<title>无标题页</title>

</head>

<body>

<table border="1" cellpadding="0" cellspacing="0" style="width: 522pxheight: 338px">

<tr>

<td align="center" colspan="2">

@Title</td>

</tr>

<tr>

<td align="center" colspan="2">

发布人:@UserName     发布时间:@PubTime      新闻类别:@NewsKInd</td>

</tr>

<tr>

<td colspan="2">

@NewsBody</td></tr><tr>

<td style="WIDTH: 100px">

</td><td style="WIDTH: 100px" >

</td></tr></table></body></html>