C #. net switch to html page

Source: Internet
Author: User

Asp.net generates static html files
Content 1 table structure:

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

2. 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: Export PX" 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>

3. 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>
/// Summary of 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)
{
// Place user code here to initialize the page
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Load + = new System. EventHandler (this. Page_Load );}
# Endregion

Public void button#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. etettings ["ConnectionString"]);
Conn. Open ();

SqlCommand cmd = new SqlCommand (SQL, conn );
Cmd. ExecuteNonQuery ();
Conn. Close ();

If (cf. WriteFile (s, newfilename ))
{
Response. Write ("added successfully ");
}
Else
{
Response. Write ("An error occurred while generating 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>
/// Summary of cars.
/// </Summary>
Public class cars
{
Public cars ()
{
//
// TODO: add the constructor logic here
//
}

Public bool WriteFile (string strText, string fn)
{
String path = HttpContext. Current. Server. MapPath ("news /");
Encoding code = Encoding. GetEncoding ("gb2312 ");
// Read the Template File
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 (); // read the file
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
Sr. Close ();
}


String htmlfilename = fn;
// Replace content
// At this time, the template file has been read into the variable named str
Str = str. Replace ("$ subject $", strText); // ShowArticle on the template page

// Write an object
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;
}

// File name, year, month, day, hour, minute, and second, random number
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;
}}
}
 

 

The template file is missing.

<! 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.