. NET generates static page and raw page code

Source: Internet
Author: User
Tags flush

int num = 0;





String sql = INSERT INTO news (Title,content,bigclassname,[user],infotime,hits,upfile) VALUES (@title, @content, @ddl, @ Name, @time, @num, @filename) ";


Oledbparameter[] para = new oledbparameter[] {


New OleDbParameter ("@title", Txttitle.text),





New OleDbParameter ("@content", Fckeditor1.value),


New OleDbParameter ("@ddl", Ddllei.selecteditem.value),


New OleDbParameter ("@name", txtName.Text),


New OleDbParameter ("@time", Txttime.text),


New OleDbParameter ("@num", num),


New OleDbParameter ("@filename", filename)


};


Db.executecommand (SQL, para);


String mbpath = Server.MapPath (".. /template/news.html ");


Encoding code = encoding.getencoding ("gb2312");


StreamReader sr = null;


StreamWriter sw = null;


string str = NULL;

Read
Try
{
sr = new StreamReader (Mbpath, code);
str = Sr.readtoend ();

       }
        catch (Exception ex)
        {
            throw ex;
       }
        finally
        {
             Sr.close ();
       }
        String Strmax = "SELECT ID from News ORDER by id DESC";
        int newsid = Convert.ToInt32 (Db.readout2 (Strmax)) +1;
       //automatic renaming based on time, extension can also be modified by itself
         string filename = "News_" + NewSID + ". shtml";

str = str.replace ("#title #", Txttitle.text);
str = str.replace ("#infotime #", Txttime.text);
str = str.replace ("#content #", Fckeditor1.value);
Generating static files
Try
{
SW = New StreamWriter (Server.MapPath ("http://www.cnblogs.com/") + filename, false, code);
Sw.write (str);
Sw.flush ();

Fenye ();

}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Sw.close ();

}
Response.Write ("<script language= page Special effects >alert (' Add success! '); location.href= ' newsmanage.asp tutorial x ';</script> ');

Paging

public void Fenye ()

{

 string str = "SELECT * from news ORDER by id DESC";
        DataTable TB = Db.mydataadapter (str);
        DataRow Dr;


       //Last page full page
        int m = 0;
       //pages
        int pg = 0;
& nbsp;       if (tb.rows.count% = 0)
        {
            pg = TB.ROWS.COUNT/10;
       }
        Else
        {
             pg = Convert.ToInt32 (TB.ROWS.COUNT/10) + 1;
            m = 1;
       }

Paging
String pglist = "";
for (int i = 1; I <= pg; i++)
{
PGList + + "<a href=" "+ i +". html ">" + i + "</a>";
}
PGList = "<div > Total" +pg+ "page &nbsp;<a href=" 1.html "> Home </a>" + pglist + "<a href=" "+ PG +". html ">& amp;nbsp; end </a></div> ";


for (int j = 1; J <= pg; J +)
{
Create a StringBuilder object
StringBuilder sb = new StringBuilder ();

Reading templates
using (StreamReader sr = new StreamReader (Server.MapPath) (". /template/index.html "), System.Text.Encoding.Default, True)
{

Write template to Streambuilder
Sb.append (Sr.readtoend (). ToString ());
}

Delete an existing file
File.delete (".. /....//news/"+ j.tostring () +". html ");

string title = "";


String ublist = "";


String infotime = "";


String id = "";


if (m = = 1 &amp;&amp; j = PG)//Not whole page


{


for (int i = 1 + * (j-1); I &lt;= (tb.rows.count-10 * (pg-1)); i++)


for (int i = 1 + ten * (j-1); I &lt;= (tb.rows.count); i++)


{


Dr = Tb.rows[i-1];


title = dr["title"].tostring ();


id = dr["id"].tostring ();


Infotime = dr["Infotime"].tostring ();


Ublist + + "&lt;table border=" 0 "cellpadding=" 0 "cellspacing=" 0 "style=" width:610px; line-height:10px "&gt;";


Ublist + + &lt;tr &gt;&lt;td align= "left" &gt; "+i+" &lt;a href= "News/news_" + ID + "shtml" &gt;→ "+ title +" &lt;/a&gt; &L T;/TD&GT;&LT;TD align= "Right" &gt; "+ infotime + &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";





}


}


else//is full page


{


for (int i = 1 + * (j-1); I &lt;= J; i++)


{


Dr = Tb.rows[i-1];


id = dr["id"].tostring ();


title = dr["title"].tostring (). Replace ("", "");


Infotime = dr["Infotime"].tostring (). Replace ("", "");


Ublist + + "&lt;table border=" 0 "cellpadding=" 0 "cellspacing=" 0 "style=" width:610px; line-height:25px "&gt;";


Ublist + = "&lt;tr &gt;&lt;td align=" left "&gt;" + i + "&lt;a href=" News/news_ "+ ID +". shtml "&gt;→" + title + "&lt;/a&gt ; &LT;/TD&GT;&LT;TD align= "Right" &gt; "+ infotime + &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";


}


}

Replace the contents of the template ...

Sb.replace ("$title $", ublist);
Replace paging
Sb.replace ("$pglist $", pglist);


using (StreamWriter sw = new StreamWriter (Server.MapPath ("http://www.cnblogs.com/news/" + j.tostring () + ". html"), False , System.Text.Encoding.Default, 100))
{

Write an. html file
Sw.writeline (SB);

Sw.flush ();

Sw.close ();
}

}

}

One-click to generate a static page

protected void Btnhtml_click (object sender, EventArgs e)


{


String sql = "SELECT * from news ORDER by id DESC";


DataTable dt = db.mydataadapter (SQL);


if (Dt.rows.count &gt; 0)


{


for (int i = 0; i &lt; Dt.rows.count; i++)


{


String mbpath = Server.MapPath (".. /template/news.html ");


Encoding code = encoding.getencoding ("gb2312");


StreamReader sr = null;


StreamWriter sw = null;


string str = NULL;

Read
Try
{
sr = new StreamReader (Mbpath, code);
str = Sr.readtoend ();

}


catch (Exception ex)


{


Throw ex;


}


Finally


{


Sr.close ();


}





Automatically renamed according to time, extension can also be modified by itself


string filename = "News_" + dt.rows[i]["id"].tostring () + ". sHTML";





str = str.replace ("#title #", dt.rows[i]["title"].tostring ());


str = str.replace ("#infotime #", dt.rows[i]["Infotime"].tostring ());


str = str.replace ("#content #", dt.rows[i]["Content"].tostring ());


Generating static files


Try


{


SW = New StreamWriter (Server.MapPath ("http://www.cnblogs.com/" + "News" + "/") + filename, false, code);


Sw.write (str);


Sw.flush ();


This.page.clientscript.registerstartups Tutorial Cript (GetType (), "", "&lt;script&gt;alert (' Build success! ') &lt;/script&gt; ");

}


catch (Exception ex)


{


Throw ex;


}


Finally


{


Sw.close ();





}


}

}
Else
{
This.page.clientscript.registerstartupscript (GetType (), "", "<script>alert (' temporary no data ') </script>");
}
}

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.