Core code of a static news

Source: Internet
Author: User

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. oledb;
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. configuration;

Namespace makehtmlfile
{
/// <Summary>
/// Summary of makeallfiles.
/// </Summary>
Public class makeallfiles: system. Web. UI. Page
{
Public String strcon;
Public oledbconnection conn;
Public String strsql;

Private void page_load (Object sender, system. eventargs E)
{
 
Initialpages (); // place user code here to initialize the page
}

Public void initialpages ()
{
Strcon = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ server. mappath (configurationsettings. appsettings ["mdbpath2"]) + ";"; // connection character seek // place user code here to initialize the page
Strsql = "select ID, class1id, class2id from news order by id desc ";
Makeareaforshow ();
Readnewsforwritefileuserdatareader (); // same as datareader to read data,
// Readnewsforwritefileuserdataset (); // directly attach the data to dataset for reading,
}
/// <Summary>
/// Used to generate a circular display page area, and load the ASPX page area of the HTML page
/// </Summary>
Public void makeareaforshow ()
{
Response. Write ("<span id = showimport> </span> ");
Response. Write ("<ie: Download id = 'odownload' style = 'behavior: URL (# default # download) '/> ");
}

/// <Summary>
/// Read data through datareader
/// </Summary>
Public void readnewsforwritefileuserdatareader ()
{
Int num = 0;
String newsid = NULL;
String class1id = NULL;
String class2id = NULL;
Oledbdatareader DR = NULL;
Oledbconnection conn = new oledbconnection (strcon );
Conn. open ();
Oledbcommand mycommand = new oledbcommand (strsql, Conn );
Dr = mycommand. executereader ();
While (dr. Read ())
{
Newsid = Dr ["ID"]. tostring ();
Class1id = Dr ["class1id"]. tostring ();
Class2id = Dr ["class2id"]. tostring ();
Writejscript (newsid, class1id, class2id );
Num ++;
}
Dr. Close ();
Conn. Close ();
Response. Write (Num. tostring ());

}

/// <Summary>
/// Read data through Dataset
/// </Summary>
Public void readnewsforwritefileuserdataset ()
{
Dataset DS = new dataset ();
Int num = 0;
String newsid = NULL;
String class1id = NULL;
String class2id = NULL;

Oledbconnection conn = new oledbconnection (strcon );
Conn. open ();
Oledbdataadapter da = new oledbdataadapter (strsql, Conn );
Da. Fill (DS, "news ");
Conn. Close ();
Num = Ds. Tables ["news"]. Rows. count;
Foreach (datarow DR in DS. Tables ["news"]. Rows)
{
Newsid = Dr ["ID"]. tostring ();
Class1id = Dr ["class1id"]. tostring ();
Class2id = Dr ["class2id"]. tostring ();
Writejscript (newsid, class1id, class2id );
}
DS = NULL;
Response. Write (Num. tostring ());
 
}

Public void writejscript (string newsid, string class1id, string class2id)
{
Response. Write ("<SCRIPT> ");
Response. Write ("function ondownloaddone (downdate )");
Response. Write ("{");
Response. Write ("showimport. innerhtml = downdate ");
Response. Write ("}");
Response. Write ("odownload. startdownload ('makefile2. aspx? Id = ");
Response. Write (newsid );
Response. Write ("& class1id = ");
Response. Write (class1id );
Response. Write ("& class2id = ");
Response. Write (class2id );
Response. Write ("', ondownloaddone )");
Response. Write ("</SCRIPT> ");
}

 

# 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. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
} Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. configuration;
Using system. Data. oledb;
Using system. drawing;
Using system. Web;
Using system. IO;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace makehtmlfile
{
/// <Summary>
/// Summary of makefile2.
/// </Summary>
Public class makefile2: system. Web. UI. Page
{
Public String strcon;
Public oledbconnection conn;
 
Public String class1id;
Public String class2id;
//***********************************
Public String previd;
Public String prevtitle;
Public String nextid;
Public String nexttitle;
//***********************************
Public String newstitle;
Public String newscontent;
Public String newsdate;
Public String newsip;
Public String newsid;
//************************************

Private void page_load (Object sender, system. eventargs E)
{
Strcon = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ server. mappath (configurationsettings. appsettings ["mdbpath2"]) + ";"; // connection character seek // place user code here to initialize the page
If (request. Params ["ID"]! = NULL & request. Params ["class1id"]! = NULL & request. Params ["class2id"]! = NULL)
{
Initialpages ();

} // Place user code here to initialize the page
}

Public void initialpages ()
{
Strcon = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (configurationsettings. receivettings ["mdbpath2"]) + ";";
 
If (request. Params ["ID"]! = NULL)
{
Newsid = request. Params ["ID"]. tostring ();
}
If (request. Params ["class1id"]! = NULL)
{
Class1id = request. Params ["class1id"]. tostring ();
}
If (request. Params ["class2id"]! = NULL)
{
Class2id = request. Params ["class2id"]. tostring ();
}
Readdatabase (newsid, class2id );
Makehtmlfile (newsid, class1id, class2id );
}

/// <Summary>
/// Read and write the same category in the previous and next articles
/// </Summary>
/// <Param name = "inputid"> ID of this article </param>
/// <Param name = "class2id"> category ID of the document </param>
Public void readprevandnext (string inputid, string class2id)
{
Int id = int. parse (inputid );
String strprevsql = "select top 1 ID, newstitle, newsdate from news where class2id = '" + class2id + "' and ID <" + ID + "order by id desc ";
String strnextsql = "select top 1 ID, newstitle, newsdate from news where class2id = '" + class2id + "' and ID>" + ID + "order by id asc ";
Oledbdatareader DATAR = NULL;
Oledbconnection con = new oledbconnection (strcon );
Con. open ();
Oledbcommand newcommand = new oledbcommand (strprevsql, con );
DATAR = newcommand. executereader ();
While (DATAR. Read ())
{
Previd = DATAR ["ID"]. tostring ();
Prevtitle = DATAR ["newstitle"]. tostring ();
}
DATAR. Close ();
Newcommand. commandtext = strnextsql;
DATAR = newcommand. executereader ();
While (DATAR. Read ())
{
Nextid = DATAR ["ID"]. tostring ();
Nexttitle = DATAR ["newstitle"]. tostring ();
}
Con. Close ();

}

/// <Summary>
/// Read the article information from the library and write the HTML file path to the database.
/// </Summary>
/// <Param name = "inputid"> </param>
/// <Param name = "class2id"> </param>
Public void readdatabase (string inputid, string class2id)
{
String filename_w = makefilename (class1id, class2id, newsid) + ". htm ";
 
Readprevandnext (inputid, class2id); // read the information of the next and previous articles.

Oledbconnection mycon = new oledbconnection (strcon); // open the database connection
Mycon. open ();

Int id = int. parse (inputid );
String strsql = "select * from news where id =" + ID;
Oledbdatareader DR = NULL;
Oledbcommand mycommand = new oledbcommand (strsql, mycon );
Dr = mycommand. executereader ();
While (dr. Read ())
{
Newstitle = Dr ["newstitle"]. tostring ();
Newscontent = Dr ["newscontent"]. tostring ();
Newsdate = Dr ["newsdate"]. tostring ();
Newsip = Dr ["newsip"]. tostring ();
}
Dr. Close ();
Mycommand. commandtext = "Update News set url = '" + filename_w + "'where id =" + Int. parse (inputid); // writes the generated file path to the database for easy use on the generated category page.
Mycommand. executenonquery ();
Mycon. Close ();
 
}

/// <Summary>
/// Generate the target directory and file, mainly used to generate directories of different categories
/// </Summary>
/// <Param name = "inputstr"> </param>
/// <Returns> </returns>
Public String makecatalogname (string class1, string class2) // generate the target directory file
{
String namestr = "article ";
String rootstr = server. mappath ("."). tostring ();
String class1str = rootstr + "//" + namestr + "_" + class1 + "//";
String class2str = rootstr + "//" + namestr + "_" + class1 + "//" + namestr + "_" + class2 + "//";
If (! Directory. exists (class1str ))
{
Directory. createdirectory (class1str );
}
If (! Directory. exists (class2str ))
{
Directory. createdirectory (class2str );
}
// Create the target folder
Return class2str;
}

/// <Summary>
/// Generate a file name based on the document category and ID
/// </Summary>
/// <Param name = "class1id"> </param>
/// <Param name = "class2id"> </param>
/// <Param name = "NID"> </param>
/// <Returns> returned file name </returns>
Public String makefilename (string class1, string class2, string ID) // generate the file name to generate the previous
{
String myclass2id = class2;
String myclass1id = class1;
String S = datetime. Now. year. tostring ()
+ Datetime. Now. Month. tostring ()
+ Datetime. Now. Day. tostring ()
+ "_"
+ Myclass1id
+ "_"
+ Myclass2id // parent class ID
+ "_"
+ ID; // news ID
Return S;
}

/// <Summary>
/// Generate an HTML file
/// </Summary>
/// <Param name = "NID"> ID of the document </param>
Public void makehtmlfile (string NID, string cla1id, string cla2id) // makehtmlfile (string NID, string cla1id, string cla2id, string filetemp) is used to differentiate different classes
{
String file_path = server. mappath ("template/news_mb.htm ");
String desfilename = makefilename (cla1id, cla2id, NID) + ". htm ";
String desfile = makecatalogname (cla1id, cla2id) + makefilename (cla1id, cla2id, NID) + ". htm ";
String prevurl = makefilename (cla1id, cla2id, previd) + ". htm"; // generate the file connection name in the previous article based on the category and ID
String nexturl = makefilename (cla1id, cla2id, nextid) + ". htm"; // next
System. Text. Encoding code = system. Text. encoding. getencoding ("gb2312 ");
 
Streamreader SRD = NULL; // read
Streamwriter SWR = NULL; // write
 
String strfile = NULL; // string
 
Try
{
SRD = new streamreader (file_path, Code );
Strfile = SRD. readtoend (); // read the file
}
Catch (exception exp)
{
Httpcontext. Current. response. Write (exp. Message );
Httpcontext. Current. response. End ();
SRD. Close ();
}
 
Strfile = strfile. Replace ("$ title $", newstitle );
Strfile = strfile. Replace ("$ content $", newscontent );
Strfile = strfile. Replace ("$ date $", newsdate );
Strfile = strfile. Replace ("$ IP $", newsip );
Strfile = strfile. Replace ("$ Prev $", prevtitle );
Strfile = strfile. Replace ("$ next $", nexttitle );
Strfile = strfile. Replace ("$ prevurl $", prevurl );
Strfile = strfile. Replace ("$ nexturl $", nexturl );
 
Try
{
SWR = new streamwriter (desfile, false, Code );
SWR. Write (strfile );
SWR. Flush ();
}
Catch (exception ex)
{
Httpcontext. Current. response. Write (ex. Message );
Httpcontext. Current. response. End ();
}
Finally
{
SWR. Close ();
}
If (SRD! = NULL)
{
SRD. Close ();
}
Response. Write (desfilename );

}

Public void open ()
{
If (conn = NULL)
{
Conn = new oledbconnection (strcon );
Conn. open ();
}
}
Public void close ()
{
If (Conn! = NULL)
{
Conn. Close ();
}
}

# 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. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

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.