Asp.net static page generation code

Source: Internet
Author: User

Asp tutorial. net static page generation code

Currently, html static web pages are generated using other dynamic technologies, but not static websites. Because of this, generating html static webpages has some benefits.
1. Speed up page opening and browsing. The static page does not need to be connected to the database. The speed of the tutorial is significantly higher than that of the dynamic page;
2. seo tutorials conducive to search engine optimization. Both baidu and google will give priority to static pages, not only quick but also full;
3. Reduce the burden on the server. You do not need to call the system database to browse the webpage;
4. Websites are safer and html pages are not affected by asp vulnerabilities;
The larger websites are basically static pages, which can reduce attacks and prevent SQL injection. When a database error occurs, normal website access is not affected.
Although generating html articles is difficult to operate and complicated in the program, these sacrifices are worth the effort to make it easier for search and make it faster and safer.
// Generate a static page

 

Protected void gridview1_rowdeleting (object sender, gridviewdeleteeventargs e)

    {

String n_id = this. gridview1.datakeys [e. rowindex] ["nid"]. tostring ();

Sqlconnection conn = new sqlconnection ("server =.; uid = sa; pwd =; database = dress ");

Sqlcommand cmd = new sqlcommand ("select nid, n_title, n_content, n_pic, n_date from p_news where nid =" + n_id, conn );

Conn. open ();

Sqldatareader dr = cmd.exe cutereader ();

 

// Obtain the news release time

String date = "";

If (dr. read ())

        {

Date = convert. todatetime (dr ["n_date"]). tostring ("yyyymmddhhmmss _");

// Call the static generation method

Transferstatic (dr ["n_title"]. tostring (), dr ["n_content"]. tostring (), dr ["n_pic"]. tostring (), date, n_id );

Response. write ("<script> alert ('static page generated successfully! '); Location = 'bucket staticpage. aspx' </script> ");

        }

    }

 

// Convert static methods

Public bool transferstatic (string title, string content, string pic, string date, string nid)

    {

// Output path

String outpath = server. mappath ("~ /Newdetails ");

// Simplified Chinese

Encoding = encoding. getencoding ("gb2312 ");

// Read the template file

String htmlmodel = server. mappath ("~ /Htmlmodel/newdetail.html ");

Streamreader sr = null;

Streamwriter sw = null;

String str = ""; // string used to save the content

Try

        {

Sr = new streamreader (htmlmodel, encoding );

Str = sr. readtoend (); // read from the beginning to the end

        }

Catch (exception e)

        {

Response. write (e. message );

Response. end ();

Sr. close ();

        }

 

 

// Delete the specified page

Protected void button2_click (object sender, eventargs e)

    {

String path = server. mappath ("~ /Newdetails ");

File. delete (path + "//" + this. listbox1.selectedvalue );

// Page. clientscript. registerclientscriptblock (this. gettype (), "Information Processing", "<script> alert ('deleted successfully! '); Location = 'bucket staticpage. aspx' </script> ");

Page. clientscript. registerstartups Tutorial example (this. gettype (), "Information Processing", "<script> alert ('deleted successfully! '); Location = 'bucket staticpage. aspx' </script> ");

    }

 

 

 

Generate a static page List

 

 

// Obtain the static page path

Public string htmlpath (datetime date, string nid)

    {

String path = "newdetails/" + date. tostring ("yyyymmddhhmmss _") + nid + ". html ";

Return path;

}

   

 

// Generate a static list

Protected void button#click (object sender, eventargs e)

    {

Sqlconnection conn = new sqlconnection ("server =.; uid = sa; pwd =; database = dress ");

Sqldataadapter da = new sqldataadapter ("select nid, n_title, n_content, n_pic, n_date from p_news", conn );

Dataset ds = new dataset ();

Da. fill (ds );

 

String str = "";

For (int I = 0; I <ds. tables [0]. rows. count; I ++)

        {

          

 

// Obtain the time

String date = convert. todatetime (ds. tables [0]. rows [I] ["n_date"]). tostring ("yyyymmddhhmmss _");

// Obtain the ID

String nid = ds. tables [0]. rows [I] ["nid"]. tostring ();

// Construct the file name

String pagename = date + nid + ". html ";

// Construct a string

Str + = "<tr> <td width = 360px>" + "<a href = 'newdetails/" + pagename + "'>" + ds. tables [0]. rows [I] ["n_title"]. tostring () + "</a>" + "</td> <td width = 200px>" + ds. tables [0]. rows [I] ["n_date"]. tostring () + "</td> </tr> ";

        }

 

// Call the conversion method to generate a static page

Transferstatic (str );

    }

 

// Generate a static page

Public bool transferstatic (string strall)

    {

// Output path

String outpath = server. mappath ("~ /");

Encoding = encoding. getencoding ("gb2312 ");

 

// Read the template file

String htmlmode = server. mappath ("~ /Htmlmodel/newslist.html ");

Streamreader sr = null;

Streamwriter sw = null;

String str = "";

Try

        {

Sr = new streamreader (htmlmode, encoding );

Str = sr. readtoend ();

        }

Catch (exception e)

        {

Response. write (e. message );

Response. end ();

Sr. close ();

        }

// Construct the name of the static page to be generated

String pagename = "newslist.html ";

// Start to replace the content

Str = str. replace ("newslist", strall );

// Write an object

Try

        {

Sw = new streamwriter (outpath + pagename, false, encoding );

Sw. write (str); // write the str string to a file.

Sw. flush ();

        }

Catch (exception e)

        {

Response. write (e. message );

Response. end ();

        }

Finally

        {

Sw. close ();

        }

Return true;

  }

 

Bind the news title and time on the front-end:

<Table>
<Asp: repeater id = "repeater1" runat = "server">
<Itemtemplate>
<Tr>
<Td width = "360px"> <a href = '<% # htmlpath (convert. todatetime (eval ("n_date"), eval ("nid "). tostring () %> '> <% # eval ("n_title") %> </a> </td>
<Td width = "200px"> <% # eval ("n_date") %> </td>
</Tr>
</Itemtemplate>
</Asp: repeater>

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.