C#.net中轉換成html頁

來源:互聯網
上載者:User

Asp.net產生靜態html檔案
內容 一 表結構:

CREATE TABLE [dbo].[HtmlNews] (
 [Id] [int] IDENTITY (1, 1) NOT NULL ,
 [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [filename] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [Addtime] [datetime] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[HtmlNews] ADD
 CONSTRAINT [DF_HtmlNews_Addtime] DEFAULT (getdate()) FOR [Addtime]
GO

二 WebForm1.aspx <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="htmlNews.WebForm1" %>

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="htmlNews.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat=server">
   <FONT face="宋體">
    <asp:TextBox id="subject" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 112px" runat=server"></asp:TextBox>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 176px" runat=server"
     Text="Button"></asp:Button>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 103; LEFT: 384px; POSITION: absolute; TOP: 120px"
     runat=server" ErrorMessage="*" ControlToValidate="subject"></asp:RequiredFieldValidator></FONT>
  </form>
 </body>
</HTML>

三 WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace htmlNews
{
 /// <summary>
 /// WebForm1 的摘要說明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox subject;
  protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置使用者代碼以初始化頁面
  }

  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
 
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);  }
  #endregion

  public void Button1_Click(object sender, System.EventArgs e)
  {
   string s=subject.Text.Trim().ToString();
   cars cf=new cars();
   string newfilename=cf.new_file()+".html";

   string sql="insert into htmlnews(subject,filename) values('"+s+"','"+newfilename+"')";  
  
   SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
   conn.Open(); 
  
   SqlCommand cmd=new SqlCommand(sql,conn);
   cmd.ExecuteNonQuery();
   conn.Close();

    if(cf.WriteFile(s,newfilename))
    {
     Response.Write("添加成功");
    }
    else
    {
     Response.Write("產生HTML出錯!");
    }

 }
 }
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="htmlNews.WebForm1" %>

 

cars.cs

using System;
using System.IO;
using System.Web;
using System.Text;

namespace htmlNews
{
 /// <summary>
 /// cars 的摘要說明。
 /// </summary>
 public class cars
 {
  public cars()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }

  public bool WriteFile(string strText,string fn)
  {
   string path = HttpContext.Current.Server.MapPath("news/");
   Encoding code = Encoding.GetEncoding("gb2312");
   // 讀模數板檔案
   string temp = HttpContext.Current.Server.MapPath("type.htm");
   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=fn;
   // 替換內容
   // 這時,模板檔案已經讀入到名稱為str的變數中了
   str =str.Replace("$subject$",strText); //模板頁中的ShowArticle
  
   // 寫檔案
   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;
  }

  //檔案名稱名稱,年月日時分秒,隨機數
  public string new_file()
  {
   string newfile="";
   Random a=new Random();
   newfile=DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString()+"_"+a.Next(1,100000).ToString();
   return newfile;
  } }
}
 

 

模板檔案忘了貼出來

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title></title>
  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  <meta name="ProgId" content="VisualStudio.HTML">
  <meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
 </head>
 <body>
  <FONT face="宋體">
   <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="500" border="1">
    <TR>
     <TD width="293">$subject$</TD>    
    </TR>
   </TABLE>
  </FONT>
 </body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.