ASP.NET生成HTML頁

轉載地址:http://blog.163.com/yong5123@126/blog/static/45516062200921322536881/

ASP.NET生成HTML頁  

asp.net生成html方法一

//生成HTML頁

  public static bool WriteFile(string strText,string strContent,string strAuthor)

  {

   string path = HttpContext.Current.Server.MapPath("/news/");

   Encoding code = Encoding.GetEncoding("gb2312");

   // 讀取模板文件

   string temp = HttpContext.Current.Server.MapPath("/news/text.html");

   StreamReader sr=null;

   StreamWriter sw=null;

   string str=""; 

   try

   {

    sr = new StreamReader(temp, code);

    str = sr.ReadToEnd(); // 讀取文件

   }

   catch(Exception exp)

   {

    HttpContext.Current.Response.Write(exp.Message);

    HttpContext.Current.Response.End();

    sr.Close();

   }

 

  

   string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";

   // 替換內容

   // 這時,模板文件已經讀入到名稱爲str的變量中了

   str =str.Replace("ShowArticle",strText); //模板頁中的ShowArticle

   str = str.Replace("biaoti",strText);

   str = str.Replace("content",strContent);

   str = str.Replace("author",strAuthor);

   // 寫文件

   try

   {

    sw = new StreamWriter(path + htmlfilename , false, code);

    sw.Write(str);

    sw.Flush();

   }

   catch(Exception ex)

   {

    HttpContext.Current.Response.Write(ex.Message);

    HttpContext.Current.Response.End();

   }

   finally

   {

    sw.Close();

   }

   return true;

 

此函數放在Conn.CS基類中了

在添加新聞的代碼中引用 注:工程名爲Hover

 

    if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))

    {

     Response.Write("添加成功");

    }

    else

    {

     Response.Write("生成HTML出錯!");

    }

 

 

 

 


模板頁Text.html代碼

 

 

  CODE:  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

  <title>ShowArticle</title>

   <body>

biaoti

<br>

content<br>

author

</body>

</HTML>

biaoti

<br>

content<br>

author

</body>

</HTML>


asp.net 生成html方法二

導讀:

  思路

  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樣式無法修改,如果網站更換樣式,那麼必須得重新編碼,給後期帶來巨大的工作量。

  因此,這裏採用的是第一種思路。

  示列代碼

  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"";//背景圖片

  format[1]= "#990099";//字體顏色

  format[2]="150px";//字體大小

  format[3]= "<marquee>生成的模板html頁面</marquee>";//文字說明

  //----------替換htm裏的標記爲你想加的內容

  for(int i=0;i<4;i++)

  {

  htmltext.Replace("$htmlformat["+i+"]",format[i]);

  }

  

  //----------生成htm文件------------------――

  

  try

  {

  using(StreamWriter sw=new StreamWriter("存放路徑和頁面名",

  

  false,System.Text.Encoding.GetEncoding("GB2312")))

  {

  sw.WriteLine(htmltext);

  sw.Flush();

  sw.Close();

  }

  

  }

  catch

  {

  Response.Write ("The file could not be wirte:");

  }

  

  小結

  用此方法可以方便的生成html文件。程序使用了是循環替換,因此對需替換大量元素的模板速度非常快。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章