Asp. net of C Read the database and generate JS file to make the home page picture switching effect of attached demo source code download

  • 2021-07-21 08:13:46
  • OfStack

This paper describes the method of Asp. net (C #) reading database and generating JS file to make the first page picture switching effect. Share it for your reference, 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>
  ///  Using Templates to Generate Static Pages 
  /// </summary>
  /// <param name="strTitle"> Title </param>
  /// <param name="strText"> Author </param>
  /// <param name="strContent"> Release time </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 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 a 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;
    //  Replace content 
    str = str.Replace("$biaoti", strTitle);
    str = str.Replace("$author", strAuthor);
    str = str.Replace("$datetime", strDate);
    str = str.Replace("$content", strContent);
    //  Write a 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"> By: $author&nbsp;&nbsp; Release time: $datetime</td>
  </tr>
  <tr>
    <td>$content</td>
  </tr>
</table>

Idea: First of all, read the data in the database, such as pictures, links, explanatory text, etc., and then write the read data into the JS file of the first page picture switching effect.

The following code realizes adding pictures, links, explanatory text and other data to the database and generating 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 slides , Writing 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 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 Documents 
    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 a file      catch (Exception exp)       HttpContext.Current.Response.Write("<script type='text/javascript'>alert(' Error reading template file! ')</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(' Generate JS File error! ')</script>" + ex.Message);
      HttpContext.Current.Response.End();     finally       sw.Flush();
      sw.Close();
    }
  }
// The following is a custom deletion of the 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 that can produce home page switching pictures in the system! ')</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$' // What will be replaced (toggle the caption text of the picture) 
pics='$pic$' // What will be replaced (toggle the address of the picture) 
links='$lnk$' // What will 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>');

Approach 3


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

js. aspx outputs js content

Then read the database in this abc. aspx and generate the statement of document. write output news


<%@ Page Language="C#" AutoEventWireup="true" %>
var focus_width="asdasdasdwer";
document.write(focus_width);

Click here to download the complete example code.

Readers who are interested in asp. net can check the topics of this site: "Summary of File Operation Skills of asp", "Summary of Skills of asp. net ajax" and "Summary of Cache Operation Skills of asp. net".

I hope this paper is helpful to everyone's asp.net programming.


Related articles: