请教asp.net如何生成html模板静态页面! 我想从数据库里面读取出新闻的有关信息,然后将信息加入到我定义好的模板HTML静态页上,请问我改怎样做?我是一点都不懂的,希望大家能给个完整而简单的例子,高分相送,比较急啊!~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 模板上加个标志 如 #Content读出模板,把#Content 替换成 信息内容就行了 http://www.pconline.com.cn/pcedu/empolder/net/0412/525216.html google Asp.net动态生成html页面 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["Res"].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$等是模板上的一些标志 用system.io的相关技术就可以了!http://www.zahui.com/html/4/7842.htm 思路 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样式无法修改,如果网站更换样式,那么必须得重新编码,给后期带来巨大的工作量。 因此这里采用的是第一种思路 谢谢各位的热心帮助啊,我先去研究下 xyunsh(#烟雨平生#) 的代码,不过还是希望有个例子,我留[email protected] 示列代码 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 谢谢各位的热心帮助啊,我先去研究下 xyunsh(#烟雨平生#) 的代码,不过还是希望有个例子,我留[email protected] 怎样存储dataset,用户关闭网页后又能及时消失不占资源。 Timer的问题 javacript:如何实现文本框跟随鼠标移动 一个页面两个datalist无法进行分页 网站日 IP 300 高分求助一个神秘问题。。。痛苦中 翻了好多文档源代码外加这里的FAQ也没学会,只好来求教了。 datagrid中怎样将列显示为固定长度,如果取出数据大于列的长度则显示‘。。。。‘ 一个程序员的彷徨,求助 关于验证的问题 请问怎样用 datagrid实现以下的特殊表格? 如何使用C#页面模板
{
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["Res"].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$等是模板上的一些标志
http://www.zahui.com/html/4/7842.htm
[email protected]
[email protected]