Asp.net(C#)讀取資料庫產生JS檔案製作首頁圖片轉場效果

來源:互聯網
上載者:User
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&nbsp;&nbsp;發布時間:$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 = "../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('讀模數板檔案錯誤!')</script>" + 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("<script type='text/javascript'>alert('產生JS檔案出錯!')</script>" + 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("<script type='text/javascript'>alert('系統中不存在能產生首頁切換圖片的檔案!')</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$' //將被替換的內容(切換圖片的解說文字)
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('<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內容就可以了
然後在這個abc.aspx裡讀取資料庫,並產生document.write輸出新聞的語句

<%@ Page Language="C#" AutoEventWireup="true" %>

var focus_width="asdasdasdwer";


document.write(focus_width);

 http://files.cnblogs.com/Fooo/Asp.net%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE%E5%BA%93%E7%94%9F%E6%88%90JS(Html)%E5%8F%8AEt%E9%A6%96%E9%A1%B5%E5%9B%BE%E7%89%87%E8%BD%AE%E6%8D%A2.rar

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.