当前位置:首页 > 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) { }
/// <summary>
/// 利用模板生成静态页面 /// </summary>
/// <param name=\标题</param> /// <param name=\作者</param>
/// <param name=\发布时间</param> /// <param name=\内容</param> /// <returns>生成页面名称</returns>
public static string WriteFile(string strTitle, string strAuthor, string strDate, string strContent) {
string path = HttpContext.Current.Server.MapPath(\ Encoding code = Encoding.GetEncoding(\ // 读取模板文件
string temp = HttpContext.Current.Server.MapPath(\ 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(\+ strRd + \
DateTime dtNow = DateTime.Now; // 替换内容
str = str.Replace(\ str = str.Replace(\ str = str.Replace(\ str = str.Replace(\ // 写文件 try {
string pathUrl = path + dtNow.Year + \dtNow.Day;
if (!Directory.Exists(pathUrl)) {
Directory.CreateDirectory(pathUrl); }
sw = new StreamWriter(pathUrl + \ 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() + \ }
protected void Button1_Click(object sender, EventArgs e) {
WriteFile(\测试 <br>\ } }
Template.html <table>
<tr>
<td align=\ </tr> <tr>
<td align=\作者:$author 发布时间:$datetime</td> </tr> <tr>
<td>$content</td> </tr> </table>
思路:首先读取数据库中图片,链接,说明文字等数据,然后将读取到的数据写入首页图片切换效果的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 = \ ImgUp.SaveAs(Server.MapPath(imgpath)); MyOleDb mc = new MyOleDb(); mc.ConnOpen();
OleDbCommand cmd = new OleDbCommand(\into SlideImg(lnk,pic,txt) values ('\+ linkarea.Text.ToString() + \+ imgpath + \+ imgtitle.Text.ToString() + \
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(\文件路径
jstemplete = Server.MapPath(\文件模板路径
deljs(jsfile); //删除JS文件
MyOleDb mc = new MyOleDb(); mc.ConnOpen();
OleDbCommand cmd = new OleDbCommand(\top \+ Img_Num.Text.ToString() + \ OleDbDataReader rdr = null; rdr = cmd.ExecuteReader();
while (rdr.Read()) strlnk += rdr[\\
strpic += rdr[\
strtxt += rdr[\ Encoding code = Encoding.GetEncoding(\ StreamReader sr = null; StreamWriter sw = null; string str = \
try sr = new StreamReader(jstemplete, code);
str = sr.ReadToEnd(); // 读取文件 catch (Exception exp) HttpContext.Current.Response.Write(\type='text/javascript'>alert('读取模板文件错误!')</script>\ HttpContext.Current.Response.End(); sr.Close(); }
// 替换内容 str = str.Replace(\ str = str.Replace(\ str = str.Replace(\
try sw = new StreamWriter(jsfile, false, code); sw.Write(str);
sw.Flush(); catch (Exception ex) HttpContext.Current.Response.Write(\生成JS文件出错!')</script>\
HttpContext.Current.Response.End(); finally sw.Flush();
sw.Close(); } }
共分享92篇相关文档