asp.net (C #) read the database and generate the JS file home page picture switching effect (with demo source download) _ Practical Skills

Source: Internet
Author: User
Tags datetime flush httpcontext

This article describes the asp.net (C #) to read the database and generate a JS file to make the first page of the image switching effect method. Share to everyone for your reference, specific as follows:

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>///<para M name= "StrText" > Author </param>///<param name= "strcontent" > Release time </param>///<param name= "Straut Hor "> Content </param>///<returns> generate page name </returns> public static string WriteFile (string strtitle, str
    ing strauthor, string strdate, String strcontent) {String path = HttpContext.Current.Server.MapPath ("~/");
    Encoding code = encoding.getencoding ("gb2312");
    Read template file 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 file} 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; Alternate content str = str.
    Replace ("$biaoti", strtitle); str = str.
    Replace ("$author", strauthor); str = str.
    Replace ("$datetime", strdate); str = str.
    Replace ("$content", strcontent);
      Write file 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 button1_click (object sender, EventArgs e) {WriteFile ("title", "Ttttttt", "2011-09-27", "Test"
  ;br> ");

 }
}

Template.html

<table>
  <tr>
    <td align= "center" > $biaoti </td>
  </tr>
  <tr>
    <td align= "center" > Author: $author release time: $datetime </td>
  </tr>
  <tr>
    <td> $content </td>
  </tr>
</table>

train of Thought : First of all, read the database pictures, links, notes and other data, and then read the data into the first page of the image switch js file.

The following code implementation to the database to add pictures, links, descriptive text and other data 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) {} pro
    tected void Add_btn_click (object sender, EventArgs e)//Add slides, write information to 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 (objectsender, EventArgs e)//Generate JS slide 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"; JS file template path Deljs (jsfile);
    Delete 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 file catch (Exception exp) HttpcontexT.current.response.write (' <script type= ' text/javascript ' >alert (' read template file Error! ') </script> + exp.
      message);
      HttpContext.Current.Response.End (); Sr.
    Close (); }//replace content 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 ("<script type= ' Text/javascript ' >alert" ("Error generating JS file!") ') </script> ' + ex.
      message);     HttpContext.Current.Response.End (); finally SW.
      Flush (); Sw.
    Close ();     The following is the custom delete original JS file 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 produce a first page toggle picture!"   ') </script> ");

 }
}

JS file Template jstemplete.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 (toggle the description text of the picture) pics= ' $pic $ '///the content to be replaced (toggle Picture's address) links= ' $lnk $ '//will be replaced content (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 ' &GT;<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= ' + ' "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 Three

<script language= "javascript" src= "js.aspx?classid=2" > </script>

Js.aspx output is JS content can be

Then read the database in this abc.aspx and generate statements that document.write output news

<%@ Page language= "C #" autoeventwireup= "true"%>
var focus_width= "Asdasdasdwer";
document.write (Focus_width);

Full instance code click here to download the site.

More interested in asp.net related content readers can view the site topics: "asp.net file Operation skills Summary", "ASP.net ajax Skills Summary" and "asp.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

Related Article

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.