首页 > 编程 > .NET > 正文

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

2024-07-10 12:47:18
字体:
来源:转载
供稿:网友

本文实例讲述了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="strTitle">标题</param>  /// <param name="strText">作者</param>  /// <param name="strContent">发布时间</param>  /// <param name="strAuthor">内容</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("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", "测试 <br>");  }}

Template.html

<table>  <tr>    <td align="center">$biaoti</td>  </tr>  <tr>    <td align="center">作者:$author  发布时间:$datetime</td>  </tr>  <tr>    <td>$content</td>  </tr></table>

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表