濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > Asp.net(C#)讀取數(shù)據(jù)庫并生成JS文件制作首頁圖片切換效果(附demo源碼下載)

Asp.net(C#)讀取數(shù)據(jù)庫并生成JS文件制作首頁圖片切換效果(附demo源碼下載)

熱門標(biāo)簽:遂寧市地圖標(biāo)注app 地圖標(biāo)注專業(yè)團(tuán)隊(duì) 代理接電話機(jī)器人如何取消 天心智能電銷機(jī)器人 地圖定位圖標(biāo)標(biāo)注 塔城代理外呼系統(tǒng) 400電話辦理哪家性價(jià)比高 地圖標(biāo)注的公司有哪些 濮陽外呼電銷系統(tǒng)怎么樣

本文實(shí)例講述了Asp.net(C#)讀取數(shù)據(jù)庫并生成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>
  /// 利用模板生成靜態(tài)頁面
  /// /summary>
  /// param name="strTitle">標(biāo)題/param>
  /// param name="strText">作者/param>
  /// param name="strContent">發(fā)布時(shí)間/param>
  /// param name="strAuthor">內(nèi)容/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;
    // 替換內(nèi)容
    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", "測(cè)試 br>");
  }
}

Template.html

table>
  tr>
    td align="center">$biaoti/td>
  /tr>
  tr>
    td align="center">作者:$authornbsp;nbsp;發(fā)布時(shí)間:$datetime/td>
  /tr>
  tr>
    td>$content/td>
  /tr>
/table>

思路:首先讀取數(shù)據(jù)庫中圖片,鏈接,說明文字等數(shù)據(jù),然后將讀取到的數(shù)據(jù)寫入首頁圖片切換效果的JS文件。

下面代碼實(shí)現(xiàn)向數(shù)據(jù)庫中增加 圖片、鏈接、說明文字等數(shù)據(jù) 和 生成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) //增加幻燈片,將信息寫入數(shù)據(jù)庫     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("script type='text/javascript'>alert('讀取模板文件錯(cuò)誤!')/script>" + exp.Message);
      HttpContext.Current.Response.End();
      sr.Close();
    }
    // 替換內(nèi)容     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('生成JS文件出錯(cuò)!')/script>" + ex.Message);
      HttpContext.Current.Response.End();     finally       sw.Flush();
      sw.Close();
    }
  }
//以下是自定義刪除原有JS文件函數(shù)
  protected void deljs(string jsfile)     if (File.Exists(jsfile))       File.Delete(jsfile);     else       Response.Write("script type='text/javascript'>alert('系統(tǒng)中不存在能產(chǎn)生首頁切換圖片的文件!')/script>");   }
}

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$' //將被替換的內(nèi)容(切換圖片的說明文字)
pics='$pic$' //將被替換的內(nèi)容(切換圖片的地址)
links='$lnk$' //將被替換的內(nèi)容(鏈接地址)
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>');

辦法三

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

js.aspx輸出的是js內(nèi)容就可以了

然后在這個(gè)abc.aspx里讀取數(shù)據(jù)庫,并生成document.write輸出新聞的語句

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

完整實(shí)例代碼點(diǎn)擊此處本站下載。

更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。

希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • .NET之生成數(shù)據(jù)庫全流程實(shí)現(xiàn)

標(biāo)簽:重慶 本溪 宜春 婁底 河南 汕頭 麗江 吉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Asp.net(C#)讀取數(shù)據(jù)庫并生成JS文件制作首頁圖片切換效果(附demo源碼下載)》,本文關(guān)鍵詞  Asp.net,讀取,數(shù)據(jù)庫,并,生成,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Asp.net(C#)讀取數(shù)據(jù)庫并生成JS文件制作首頁圖片切換效果(附demo源碼下載)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Asp.net(C#)讀取數(shù)據(jù)庫并生成JS文件制作首頁圖片切換效果(附demo源碼下載)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阿拉尔市| 镇原县| 平凉市| 临江市| 饶平县| 岐山县| 阿巴嘎旗| 什邡市| 额济纳旗| 东乌| 蓬溪县| 宜丰县| 吴堡县| 孟津县| 保德县| 博乐市| 洪湖市| 通道| 五莲县| 长丰县| 江川县| 个旧市| 陵川县| 枝江市| 广东省| 清水县| 西昌市| 达州市| 酒泉市| 榆林市| 五寨县| 乌兰察布市| 天门市| 上蔡县| 德令哈市| 新兴县| 宁安市| 江城| 屯留县| 蕲春县| 衡阳市|