Asp.net (C #) reads the JS file generated by the database, creates the homepage, and changes the image switching effect.

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;

Using System. text;
Using System. IO;
Public Partial Class _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{

}

/// <Summary>
/// Use templates to generate static pages
/// </Summary>
/// <Param name = "strtitle"> Title </Param>
/// <Param name = "strtext"> Author </Param>
/// <Param name = "strcontent"> Release Date </Param>
/// <Param name = "strauthor"> Content </Param>
/// <Returns> Generate Page name </Returns>
Public Static String Writefile ( String Strtitle, String Strauthor, String Strdate, String Strcontent)
{
String Path = httpcontext. Current. server. mappath ( " ~ / " );
Encoding code = encoding. getencoding (" Gb2312 " );
// Read template files
String Temp = httpcontext. Current. server. mappath ( " ~ /Template.html " );
Streamreader sr = Null ;
Streamwriter Sw = Null ;
String STR = "" ;
Try
{
Sr = New Streamreader (temp, Code );
STR = Sr. readtoend (); // Read files
}
Catch (Exception exp)
{
Httpcontext. Current. response. Write (exp. Message );
Httpcontext. Current. response. End ();
Sr. Close ();
}

Random RD = New Random ();
String Strrd = RD. Next ( 0 , 9999 ). Tostring ();
String Htmlfilename = datetime. Now. tostring ( " Yyyymmddhhmmss " ) + Strrd + " . Html " ;
Datetime dtnow = datetime. now;
// Replace content
STR = Str. Replace ( " $ Biaoti " , Strtitle );
STR = Str. Replace ( " $ Author " , Strauthor );
STR = Str. Replace ( " $ Datetime " , Strdate );
STR = Str. Replace ( " $ Content " , Strcontent );
// Write files
Try
{
String Pathurl = path + dtnow. Year + " \\ " + Dtnow. Month + " \\ " + Dtnow. Day;
If (! Directory. exists (pathurl ))
{
Directory. createdirectory (pathurl );
}
Sw = New Streamwriter (pathurl + " \\ " + 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 Dtnow. year. tostring () + " / " + Dtnow. Month. tostring () + " / " + Dtnow. Day. tostring () +" / " + Htmlfilename;
}
Protected Void Button#click ( Object Sender, eventargs E)
{
Writefile ( " Title " , " Ttttttt " , " 2011-09- " , " Test <br> " );
}
}

Template.html

 <  Table  > 
< Tr >
< TD Align = "Center" > $ Biaoti </ TD >
</ Tr >
< Tr >
< TD Align = "Center" > Author: $ author & Nbsp; Release date: $ datetime </ TD >
</ Tr >
< Tr >
< TD > $ Content </ TD >
</ Tr >
</ Table >

 

Idea: First read the image, Link, description text and other data in the database, and then write the read data to the JS file for homepage image switching effect.

BelowCodeAdd images, links, instructions, and other data to the database and generate JS files.

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb;
Using system. IO;
Using system. text;

Public partial class admin_slide: system. Web. UI. Page protected void page_load (Object sender, eventargs E)
{

}
Protected void add_btn_click (Object sender, eventargs e) // Add a slide to write information to the database string imgpath;
Imgpath = "../uploadfiles/slideimg/" + imgup. filename;
Imgup. saveas (server. mappath (imgpath ));

Myoledb MC = new myoledb ();
MC. connopen ();
Oledbcommand cmd = new oledbcommand ("insert into slideimg (lnk, PIC, txt) values ('" + linkarea. text. tostring () + "','" + imgpath + "','" + imgtitle. text. tostring () + "');", MC. conn );
Oledbdatareader RDR = NULL;
RDR = cmd. executereader ();
MC. connclose ();
}

Protected void mjs_btn_click (Object sender, eventargs e) // generate the JS magic light file string jsfile, jstemplete;

String strlnk, strpic, strtxt;
Strlnk = NULL;
Strpic = NULL;
Strtxt = NULL;

Jsfile = server. mappath ("~ /JS/") +" slideimg. js "; // JS file path
Jstemplete = server. mappath ("~ /JS/") +" jstemplete. js "; // path of the JS file template

Deljs (jsfile); // delete a JS File

myoledb MC = new myoledb ();
MC. connopen ();
oledbcommand cmd = new oledbcommand ("select top" + img_num.text.tostring () + "* From slideimg order by id desc", MC. conn);
oledbdatareader RDR = NULL;
RDR = cmd. executereader ();
while (RDR. read () strlnk + = RDR ["lnk"]. tostring () + "|";
strpic + = RDR ["pic"]. tostring () + "|";
strtxt + = RDR ["TXT"]. tostring () + "|"; MC. connclose ();

Encoding code = encoding. getencoding ("UTF-8 ");

Streamreader sr = NULL;
Streamwriter Sw = NULL;
String STR = "";

try sr = new streamreader (jstemplete, Code);
STR = sr. readtoend (); // read the file catch (exception exp) httpcontext. current. response. write (" "+ exp. message);
httpcontext. current. response. end ();
Sr. close ();
}

// replace STR = Str. replace ("$ TXT $", strtxt);
STR = Str. replace ("$ PIC $", strpic);
STR = Str. replace ("$ lnk $", strlnk);

try Sw = new streamwriter (jsfile, false, Code);
SW. write (STR);
SW. flush (); catch (exception ex) httpcontext. current. response. write (" "+ ex. message);
httpcontext. current. response. end (); finally SW. flush ();
SW. close ();
}

}

// The following is a custom JS file deletion function.

Protected void deljs (string jsfile) if (file. exists (jsfile) file. delete (jsfile); else response. write ("<SCRIPT type = 'text/JavaScript '> alert (' There is no file in the system that can generate a homepage switching image! ') </SCRIPT> ");}
}

 

JS file templateJstemplete. js

VaR focus_width = 300;
VaR focus_height = 225;
VaR text_height = 18;
VaR swf_height = focus_height + text_height;
VaR pics, links, texts;

Texts = '$ TXT $' // the content to be replaced (switch the description text of the image)
Pics = '$ PIC $' // content to be replaced (change the image address)
Links = '$ lnk $' // content to be replaced (link address)

Pics = pics. substr (0, pics. Length-1 );
Links = links. substr (0, links. Length-1 );
Texts = texts. substr (0, texts. Length-1 );

VaR FV = "pics =" + pics + "& links =" + Links + "& texts =" + texts + "& borderwidth =" + focus_width + "& borderheight =" + focus_height + "& textheight =" + text_height;

Document. write ('<object classid = "CLSID: d27cdb6e-ae6d-11cf-96b8-444553540000" codebase = "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width = "' + focus_width + '" Height = "' + swf_height + '"> ');
Document. write ('<Param name = "allowScriptAccess" value = "samedomain"> <Param name = "movie" value = ".. /plugin/slide.swf "> <Param name =" quality "value =" high "> <Param name =" bgcolor "value =" # e5ecf4 "> ');
Document. Write ('<Param name = "menu" value = "false"> <Param name = wmode value = "Opaque"> ');
Document. write ('<Param name = "flashvars" value = "pics =' + pics + '& links =' + Links + '& texts =' + texts + '& borderwidth =' + focus_width + '& borderheight =' + focus_height + '& textheight =' + text_height + '"> ');
Document. write ('<embed src = "pixviewer.swf" wmode = "Opaque" flashvars = "pics =' + pics + '& links =' + Links + '& texts =' + texts + '& borderwidth =' + focus_width + '& borderheight =' + focus_height + '& textheight =' + text_height + '"menu =" false "bgcolor =" #009900 "Quality =" high "width =" '+ focus_width +' "Height =" "'+ focus_height +'" allowScriptAccess = "samedomain" type = "application/X-Shockwave-flash" pluginspage = "http://www.macromedia.com/go/getflashplayer "/> ');
Document. Write ('</Object> ');

 

Method 3 = ======================================

<Script language = "JavaScript" src = "Js. aspx? Classid = 2 "> </SCRIPT>
JS. aspx outputs JS content.
Then read the database in ABC. aspx and generate the statement of document. Write to output news.

 
<% @ Page Language ="C #"Autoeventwireup ="True"%>

VaR focus_width ="Asdasdasdwer";


Document. Write (focus_width );

Http://files.cnblogs.com/Fooo/Asp.net%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D% AE %E5%BA%93%E7%94%9F%E6%88%90JS lightning

 

 

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.