请教asp.net如何生成html模板静态页面!
我想从数据库里面读取出新闻的有关信息,然后将信息加入到我定义好的模板HTML静态页上,请问我改怎样做?我是一点都不懂的,希望大家能给个完整而简单的例子,高分相送,比较急啊!~ 问题点数:100、回复次数:10Top
1 楼pontus(pontus)回复于 2005-08-31 15:59:34 得分 10
模板上加个标志 如 #Content
读出模板,把#Content 替换成 信息内容就行了Top
2 楼xyunsh(学海无涯,回头是岸)回复于 2005-08-31 16:06:52 得分 10
http://www.pconline.com.cn/pcedu/empolder/net/0412/525216.htmlTop
3 楼starmilan(笨熊肥肥)回复于 2005-08-31 16:07:16 得分 0
请问有简单的例子么?网上有些,都不是完整的,谁能给我个完整点的啊Top
4 楼xyunsh(学海无涯,回头是岸)回复于 2005-08-31 16:07:40 得分 10
google Asp.net动态生成html页面Top
5 楼xyunsh(学海无涯,回头是岸)回复于 2005-08-31 16:11:44 得分 50
public String CreateQuotation()
{
StringBuilder htmlText = new StringBuilder();
try
{
using( StreamReader sr = new StreamReader(
HttpContext.Current.Request.PhysicalApplicationPath + @"\Templates\Quotation.htm",System.Text.Encoding.GetEncoding("GB2312") ) )
{
String line;
while( (line = sr.ReadLine()) != null )
htmlText.Append( line );
sr.Close();
}
}
catch( Exception ex )
{
Error.Log( ex.ToString() );
return String.Empty;
}
QuotationDB qd = new QuotationDB();
qd.Get( this );
ClientDB cd = new ClientDB();
BIMS.Components.BLL.Client.Client client = cd.Get( this.ClientID, this.OperatorID );
BIMS.Components.BLL.Client.LinkmanDB ld = new LinkmanDB();
BIMS.Components.BLL.Client.Linkman clientlinkman = ld.GetByUserID( this.ClientAcceptorID );
StaffDB sd = new StaffDB();
BIMS.Components.BLL.Auth.Staff staff = sd.Get( this.OperatorID );
htmlText.Replace("$ClientName$", client.Name );
htmlText.Replace("$AliasID$", AliasID );
htmlText.Replace("$ClientLinkman$", clientlinkman.Name );
htmlText.Replace("$StaffName$", staff.RealName);
htmlText.Replace("$ClientPhone$", clientlinkman.WorkPhone);
htmlText.Replace("$StaffPhone$", staff.WorkPhone);
htmlText.Replace("$ClientFax$", client.Fax );
htmlText.Replace("$QuoteTime$", this.QuoteTime.ToShortDateString());
htmlText.Replace("$AvailabilityDays$", this.AvailabilityDays.ToString() );
htmlText.Replace("$DeliveryDays$", this.DeliveryDays.ToString());
StringBuilder productlist = new StringBuilder();
productlist.Append("<table cellspacing='0' cellpadding='3' rules='all' bordercolor='Black' border='1' width='100%' class='PrintContent'>");
productlist.AppendFormat( ProductFormat, "产品编号", "产品名称", "单价/元", "数量", "备注" );
using ( SqlDataReader dr = GetItems() )
{
while( dr.Read() )
{
String PID = String.Format("<a href='{1}/Common/products_list2.aspx?PID={0}'><font color='blue'>{0}</font></a>", dr["PID"].ToString(), ConfigurationSettings.AppSettings["WebSite"]);
productlist.AppendFormat( ProductFormat,PID, dr["ProductName"].ToString(),String.Format("{0:c}",dr["UnitCost"]),dr["Quantity"].ToString(),dr["Remarks"].ToString()+" " );
}
dr.Close();
}
productlist.Append("</table>");
htmlText.Replace("$QuotationList$", productlist.ToString() );
QuoteParameterItemDB qpid = new QuoteParameterItemDB();
using( SqlDataReader dr = qpid.GetByQuotation(_ID) )
{
while( dr.Read() )
{
switch( (ClientCategoryBig)(Int32.Parse(dr["BigID"].ToString())) )
{
case ClientCategoryBig.PublishCount:
htmlText.Replace("$Publish$", dr["Descs"].ToString() );
break;
case ClientCategoryBig.TaxTicket:
htmlText.Replace("$TaxTicket$", dr["Descs"].ToString());
break;
case ClientCategoryBig.Consignment:
htmlText.Replace("$Consignment$", dr["Descs"].ToString() );
break;
default:
break;
}
}
}
String quotationFileName = CreateUniqueKey.Create() + ".htm";
try
{
using( StreamWriter sw = new StreamWriter(
HttpContext.Current.Request.PhysicalApplicationPath + @"\Common\Quotation\" + quotationFileName,
false, System.Text.Encoding.GetEncoding("GB2312") ) )
{
sw.WriteLine(htmlText);
sw.Flush();
sw.Close();
}
}
catch( Exception ex )
{
Error.Log( ex.ToString() );
return String.Empty;
}
return quotationFileName;
}
这里$ClientFax$等是模板上的一些标志Top
6 楼jamex(1 + 1 = 爱)回复于 2005-08-31 16:28:51 得分 10
用system.io的相关技术就可以了!
http://www.zahui.com/html/4/7842.htmTop
7 楼jamex(1 + 1 = 爱)回复于 2005-08-31 16:29:14 得分 10
思路
1. 利用如Dw-Mx这样的工具生成html格式的模板,在需要添加格式的地方加入特殊标记(如$htmlformat$),动态生成文件时利用代码读取此模板,然后获得前台输入的内容,添加到此模板的标记位置中,生成新文件名后写入磁盘,写入后再向数据库中写入相关数据。
2. 使用后台代码硬编码Html文件,可以使用HtmlTextWriter类来写html文件。
优点
1. 可以建立非常复杂的页面,利用包含js文件的方法,在js文件内加入document.write()方法可以在所有页面内加入如页面头,广告等内容。
2. 静态html文件利用MS Windows2000的Index Server可以建立全文搜索引擎,利用asp.net可以以DataTable的方式得到搜索结果。而Win2000的Index服务无法查找xml文件的内容。如果包括了数据库搜索与Index索引双重查找,那么此搜索功能将非常强大。
3. 节省服务器的负荷,请求一个静态的html文件比一个aspx文件服务器资源节省许多。
缺点
思路二: 如果用硬编码的方式,工作量非常大,需要非常多的html代码。调试困难。而且使用硬编码生成的html样式无法修改,如果网站更换样式,那么必须得重新编码,给后期带来巨大的工作量。
因此这里采用的是第一种思路
Top
8 楼starmilan(笨熊肥肥)回复于 2005-08-31 16:29:37 得分 0
谢谢各位的热心帮助啊,我先去研究下 xyunsh(#烟雨平生#) 的代码,不过还是希望有个例子,我留我的邮箱下来,有的话请发给我,谢谢了
starmilan@163.com
starmilano@hotmail.comTop
9 楼jamex(1 + 1 = 爱)回复于 2005-08-31 16:29:47 得分 0
示列代码
1.定义(template.htm)html模板页面
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body >
<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
<tr>
<td width="100%" valign="middle" align="left">
<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>
</td>
</tr>
</table>
</body>
</html>
2.asp.net代码:
//---------------------读html模板页面到stringbuilder对象里----
string[] format=new string[4];//定义和htmlyem标记数目一致的数组
StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader("存放模板页面的路径和页面名"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch
{
Response.Write("<Script>alert('读取文件错误')</Script>");
}
//---------------------给标记数组赋值------------
format[0]="background=\"bg.jpg\"";//背景图片
fo
Top
10 楼starmilan(笨熊肥肥)回复于 2005-08-31 16:29:56 得分 0
谢谢各位的热心帮助啊,我先去研究下 xyunsh(#烟雨平生#) 的代码,不过还是希望有个例子,我留我的邮箱下来,有的话请发给我,谢谢了
starmilan@163.com
starmilano@hotmail.comTop




