項目首頁 HTML頁 需要全屏輪播展示,要求通過管理平台能夠選擇展示的圖片。開始時使用 Ajax 獲取數據庫表中圖片地址展示,結果只能輪播一次無法循環輪播,最后通過一鍵靜態化實現此功能
一,案例展示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 5.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>模板頁</title> <script src="../Scripts/jquery-1.9.1.min.js"></script> <script src="../Scripts/fullplay.js"></script> <script type="text/javascript"> var widthint = 1920; var heightint = 980; $(document).ready(function (e) { $("#bodyPage").fullImages({ ImgWidth: widthint, ImgHeight: heightint, autoplay: 3500, //自動播放 fadeTime: 1500 //褪色 時間 }); }); </script> <style type="text/css"> html { margin: 0; padding: 0; } body { margin: 0; padding: 0; } #bodyPage { width: 100%; min-height: 100%; height: auto !important; height: 100%; position: fixed; top: 0; left: 0; } img.bg { position: absolute; top: 0px; left: 0px; z-index: 1; display: none; height: 100%; width: 100%; } footer { position: fixed; bottom: 0px; height: 10%; width: 100%; z-index: 9999; } footer marquee { font: 2.5vw "華文行楷"; color: #f00; } </style> </head> <body> <section id="bodyBox"> <div id="bodyPage"> {$imgList$} </div> </section> <footer> <article> <marquee scrollamount="10"> XXXXXXX 系統 </marquee> </article> </footer> </body> </html> <script type="text/javascript"> $("body").click(function () { window.location.href = "NavigationInfo.html"; }); </script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 5.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>模板頁</title> <script src="../Scripts/jquery-1.9.1.min.js"></script> <script src="../Scripts/fullplay.js"></script> <script type="text/javascript"> var widthint = 1920; var heightint = 980; $(document).ready(function (e) { $("#bodyPage").fullImages({ ImgWidth: widthint, ImgHeight: heightint, autoplay: 3500, //自動播放 fadeTime: 1500 //褪色 時間 }); }); </script> <style type="text/css"> html { margin: 0; padding: 0; } body { margin: 0; padding: 0; } #bodyPage { width: 100%; min-height: 100%; height: auto !important; height: 100%; position: fixed; top: 0; left: 0; } img.bg { position: absolute; top: 0px; left: 0px; z-index: 1; display: none; height: 100%; width: 100%; } footer { position: fixed; bottom: 0px; height: 10%; width: 100%; z-index: 9999; } footer marquee { font: 2.5vw "華文行楷"; color: #f00; } </style> </head> <body> <section id="bodyBox"> <div id="bodyPage"> <img class="bg" src="/UpLoadFiles/20180307/2018030716220622.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716223467.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716224437.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716225115.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716225737.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716230370.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716230949.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716221741.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716222305.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716222856.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716211971.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716234059.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716235859.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716240816.jpg" /><img class="bg" src="/UpLoadFiles/20180307/2018030716250642.png" /><img class="bg" src="/UpLoadFiles/20180307/2018030716341533.png" /> </div> </section> <footer> <article> <marquee scrollamount="10"> XXXXXXX 系統</marquee> </article> </footer> </body> </html> <script type="text/javascript"> $("body").click(function () { window.location.href = "NavigationInfo.html"; }); </script>
使用 .aspx 作為網站的后台管理
<asp:Button ID="show" runat="server" Text="生成展示頁" />

//一鍵靜態化 void show_Click(object sender, EventArgs e) { //替換掉模板中的特征字符 \..\showcasing\Webpages\ScreenInfo_Temp.html string mbPath = Server.MapPath("../showcasing/Webpages/ScreenInfo_Temp.html"); Encoding code = Encoding.GetEncoding("UTF-8"); StreamReader sr = null; StreamWriter sw = null; string str = null; //字符串為空 //讀取 try { sr = new StreamReader(mbPath, code); str = sr.ReadToEnd(); //注意:這里是‘tempstr’而不是‘str’,不要混了 } catch (Exception ex) { throw ex; } finally { sr.Close(); } string fileName = "../showcasing/Webpages/ScreenInfo.html"; string imglistStr = GetListStr(); str = str.Replace("{$imgList$}", imglistStr); //生成靜態文件 try { sw = new StreamWriter(Server.MapPath("~/Webpages/") + fileName, false, code); sw.Write(str); sw.Flush(); } catch (Exception ex) { throw ex; } finally { sw.Close(); JscriptPrint("生產成功啦!", "ShowimagesList.aspx?" + CombUrlTxt(this.channelId, this.keywords) + "page=0", "Success"); } }

private String GetListStr() { string listStr = string.Empty; ShowImagesBLL ShowImagesBLL = new ShowImagesBLL(); ShowImagesEntity ShowImagesEntity = new ShowImagesEntity(); DataTable dt = ShowImagesBLL.GetList("Islook>0 order by Islook asc"); foreach (DataRow dr in dt.Rows) { ShowImagesEntity = new ShowImagesEntity(dr); listStr += "<img class=\"bg\" src=\"" + ShowImagesEntity.ImageURL + "\" />"; } return listStr; }
二、實例說明 《轉》
一,先建立一個模版頁
為了適應實際應用功能,一個網站往往需要最終用戶自定義靜態化的模版,本文對這方面不作過多描述,主要只是增刪改查功能,此章僅作拋磚引玉用!!!
前端界面如下
aspx.cs頁面代碼也只簡單實現對模版的保存
- /// <summary>
- /// Saves the data.
- /// </summary>
- /// <param name="id">The id.</param>
- private void SaveData(int id)
- {
- Model.C_Template model = GetData(id);
- if (model != null)
- {
- long x = 0;
- if (id <= 0)
- {
- x = model.id = _BLL.Add(model);
- }
- else
- {
- x = _BLL.Update(model) ? 1 : 0;
- }
- //other code
- //show message
- if (x > 0)
- {
- MessageBox.Show(string.Format(_jsonResult, "true", "保存成功", _returnUrl, id.ToString()), MessageEnum.Error);
- }
- else
- {
- MessageBox.Show(string.Format(_jsonResult, "false", "保存失敗,請稍候再試...", "", id.ToString()), MessageEnum.Error);
- }
- }
- else
- {
- //有錯誤
- MessageBox.Show(string.Format(_jsonResult, "false", _pageMsg, "", id.ToString()), MessageEnum.Error);
- }
- }
二,根據對應模版,靜態出一個include頁面
ok,好吧,其實我這里取巧了,先跳過,后面總結里再詳說。為了使本例簡單,現在先假設模版頁已經生成了html格式。代碼如下
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>{$title} - {$classcName} - <!--#include file="/include/common/siteTitle.html" --></title>
- <meta name="description" content="{$seoDescription},<!--#include file="/include/common/siteDescription.html" -->" />
- <meta name="keywords" content="{$seoKeyWords},<!--#include file="/include/common/siteKeyWords.html" -->" />
- <link href="/css/layout.css" rel="stylesheet" type="text/css" />
- <link href="/css/layout_sub.css" rel="stylesheet" type="text/css" />
- <script src="/js/jquery/jquery.js" type="text/javascript"></script>
- <script src="/js/jquery/myJS.js" type="text/javascript"></script>
- <!--[if lte IE 6]>
- <script type="text/javascript" src="/js/ie6png.js"></script>
- <![endif]-->
- <script type="text/javascript">
- $(function () {
- $(".hotSchool .tabs").tabs(".hotSchool div.contentC", { event: 'click', effect: 'fade' });
- });
- </script>
- </head>
- <body>
- <div id="bgImgWrap">
- <img src="/images/back1.jpg" alt="" />
- </div>
- <!--#include file="/include/common/picStory.html" -->
- <div class="bigWrap">
- <div class="sideWrapBg png"></div>
- <div class="sideWrap png">
- <div class="logo noTxt clearfix"></div>
- <!--#include file="/include/common/sideNav.html" -->
- </div>
- <div class="mainWrap png">
- <!-- top search /-->
- <!--#include file="/include/common/head.html" -->
- <!-- 頻道頁頂部導航 /-->
- <div class="cWrap subTop clearfix">
- <div class="left subTitle">{$channelname}</div>
- <div class="left subNav">
- <!--{$subNav}-->
- </div>
- </div>
- <!-- sub wrap -->
- <div class="cWrap subWrap pie png clearfix">
- <!-- 文章詳細 /-->
- <div class="detailWrap newsListWrap newsDetailWrap clearfix">
- <div class="subNavWrap clearfix">
- <div class="subNav left">{$locationget}</div>
- </div>
- <h1>{$title}</h1>
- <div class="infoWrap clearfix">
- <div class="shareWrap">
- <div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare">
- <span class="left">分享本頁:</span>
- <a class="bds_qzone"></a>
- <a class="bds_tsina"></a>
- <a class="bds_tqq"></a>
- <a class="bds_renren"></a>
- <a class="bds_douban"></a>
- <span class="bds_more"></span>
- </div>
- </div>
- <div class="dateWrap">發表:{$addDate} </div>
- <div class="readWrap">閱讀:<span id="viewNums">...</span></div>
- </div>
- <div class="clearfix"></div>
- <div class="subHR clearfix png"></div>
- <!-- Intro /-->
- <div class="introWrap clearfix">
- {$intro}
- </div>
- <!-- 正文 /-->
- <div class="txtWrap clearfix minArticle">
- {$content}
- </div>
- <!-- 相關文章 /-->
- <div>
- </div>
- </div>
- </div>
- <!--/ sub wrap -->
- <!-- footer /-->
- <!--#include file="/include/common/foot.html" -->
- </div>
- </div>
- </body>
- </html>
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="include.aspx.cs" validateRequest="false" Inherits="Admin_CMS_include" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>文章輸入頁面</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 文章標題:<asp:TextBox ID="txtTitle" runat="server" Height="26px" Width="350px"></asp:TextBox><br />
- 文章摘要:<asp:TextBox ID="txtIntro" runat="server" Height="26px" Width="350px"></asp:TextBox><br />
- 文章內容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine" Width="350px"></asp:TextBox><br />
- <br />
- <asp:Button ID="btnInclude" runat="server" OnClick="btnInclude_Click" Text="生成靜態頁" />
- </div>
- </form>
- </body>
- </html>
- using System.IO;
- using System.Text;
- protected void btnInclude_Click(object sender, EventArgs e)
- {
- //other code;
- //Get date into dataSource;
- string mbPath = Server.MapPath("~/Web/Template/CS1.html");
- Encoding code = Encoding.GetEncoding("UTF-8");
- StreamReader sr = null;
- StreamWriter sw = null;
- string str = null;
- try
- {
- sr = new StreamReader(mbPath, code);
- str = sr.ReadToEnd();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sr.Close();
- }
- string fileName = DateTime.Now.ToString("MMddHHmm") + ".html";//保存靜態頁面的擴展名
- //整章的關鍵點,替換掉相應字符串
- str = str.Replace("{$Title}", txtTitle.Text);
- str = str.Replace("{$intro}", txtIntro.Text);
- str = str.Replace("{$content}", txtContent.Text);
- //生成靜態文件
- try
- {
- sw = new StreamWriter(Server.MapPath("~/html/") + fileName, false, code);
- sw.Write(str);
- sw.Flush();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- sw.Close();
- Response.Write("<a href=/Web/html/" + fileName + " mce_href=" + fileName + " target=_blank>" + fileName + "</a>已經生成!");
- }
- }
三,總結
ok,原理已經明白了。那么,從效益出發,實際應用中應該是怎樣的呢?一:每先建一個靜態頁面模版,就相應生成一個靜態模版頁;
二:或者直接從數據庫中讀取模版的數據流,然后相應替換;
三:確定保存文章的時候,立刻生成一個相應的靜態文章頁;
四:在網站后台管理中,嵌入“一鍵生成靜態頁面”中,重新檢索生成漏掉或者有更改的靜態頁面。
那么,其它比如是否細分類別來實現對應的靜態化功能,就以自己實際項目來考慮了最后,附最后的效果圖。。。。
