Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载)

本文实例讲述了Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果的方法。分享给大家供大家参考,具体如下:

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)
  {
  }
  /// 
  /// 利用模板生成静态页面
  /// 
  /// 标题
  /// 作者
  /// 发布时间
  /// 内容
  /// 生成页面名称
  public static string WriteFile(string strTitle, string strAuthor, string strDate, string strContent)
  {
    string path = HttpContext.Current.Server.MapPath("~/");
    Encoding code = Encoding.GetEncoding("gb2312");
    // 读取模板文件
    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(); // 读取文件
    }
    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;
    // 替换内容
    str = str.Replace("$biaoti", strTitle);
    str = str.Replace("$author", strAuthor);
    str = str.Replace("$datetime", strDate);
    str = str.Replace("$content", strContent);
    // 写文件
    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", "测试 
"); } }

Template.html

$biaoti
作者:$author  发布时间:$datetime
$content

思路:首先读取数据库中图片,链接,说明文字等数据,然后将读取到的数据写入首页图片切换效果的JS文件。

下面代码实现向数据库中增加 图片、链接、说明文字等数据 和 生成JS文件

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) //增加幻灯片,将信息写入数据库     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) //生成JS幻灯文件     string jsfile,jstemplete;
    string strlnk, strpic, strtxt;
    strlnk = null;
    strpic = null;
    strtxt = null;
    jsfile = Server.MapPath("~/Js/") + "SlideImg.js";  //JS文件路径
    jstemplete = Server.MapPath("~/Js/") + "JsTemplete.js";  //JS文件模板路径
    deljs(jsfile); //删除JS文件
    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(); // 读取文件     catch (Exception exp)       HttpContext.Current.Response.Write("" + exp.Message);
      HttpContext.Current.Response.End();
      sr.Close();
    }
    // 替换内容     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();
    }
  }
//以下是自定义删除原有JS文件函数
  protected void deljs(string jsfile)     if (File.Exists(jsfile))       File.Delete(jsfile);     else       Response.Write("");   }
}

JS文件模板 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$' //将被替换的内容(切换图片的说明文字)
pics='$pic$' //将被替换的内容(切换图片的地址)
links='$lnk$' //将被替换的内容(链接地址)
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('');
document.write('');
document.write('');
document.write('');
document.write('');
document.write('');

办法三



js.aspx输出的是js内容就可以了

然后在这个abc.aspx里读取数据库,并生成document.write输出新闻的语句

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

完整实例代码点击此处本站下载。

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

你可能感兴趣的:(Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载))