webfrom-横贯四方-04-静态页的生成

使用范围:经常查看,不需要修改了页面(缓存).

文件上传页面

<%@ Page Language="C#" MasterPageFile="~/Master/MainMaster.Master" AutoEventWireup="true"
    CodeBehind="Add.aspx.cs" Inherits="BookShopManager.Web.Books.Add" Title="增加页" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
    <script src="../js/jquery-1.7.1.js"></script>
    <script src="../SWFUpload/handlers.js"></script>
    <script src="../SWFUpload/swfupload.js"></script>
    <script>
        window.onload = function updatePic() {
            var swfu;
            swfu = new SWFUpload({
                //指定要上传的路径
                upload_url: "/ashx/FileUpload.ashx?action=uploadBookCover",
                post_params: {
                    "ASPSESSID": "<%=Session.SessionID %>"
                },

                // File Upload Settings
                file_size_limit: "2 MB",
                file_types: "*.jpg;*.gif",
                file_types_description: "JPG Images",
                file_upload_limit: 0,    // Zero means unlimited

                // 事件处理了的三个主要方法定义在 Handlers.js
                //  The handlers are not part of SWFUpload but are part of my website and control how
                //  my website reacts to the SWFUpload events.
                swfupload_preload_handler: preLoad,
                swfupload_load_failed_handler: loadFailed,
                file_queue_error_handler: fileQueueError,
                file_dialog_complete_handler: fileDialogComplete,
                upload_progress_handler: uploadProgress,
                upload_error_handler: uploadError,
                upload_success_handler: showMes,
                upload_complete_handler: uploadComplete,

                // 按钮设置
                button_image_url: "/SWFUpload/images/XPButtonNoText_160x22.png",
                button_placeholder_id: "spanButtonPlaceholder",
                button_width: 160,
                button_height: 22,
                button_text: '<span class="button">请上传图书封面图片<span class="buttonSmall">(2 MB Max)</span></span>',
                button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
                button_text_top_padding: 1,
                button_text_left_padding: 5,

                // Flash Settings
                flash_url: "/SWFUpload/swfupload.swf",    // Relative to this file
                flash9_url: "/SWFUpload/swfupload_FP9.swf",    // Relative to this file

                // Debug Settings
                debug: false
            });
        }
       
        //上传成功以后调用该方法
        function showMes(file, serverData) {
            alert("文件上传成功!");
        }

    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table style="width: 100%;" cellpadding="2" cellspacing="1" class="border">
        <tr>
            <td class="tdbg">

                <table cellspacing="0" cellpadding="0" width="100%" border="0">
                    <tr>
                        <td height="25" width="30%" align="right">Title
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtTitle" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">Author
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtAuthor" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">PublisherId
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtPublisherId" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">PublishDate
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtPublishDate" runat="server" Width="70px" onfocus="setday(this)"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">ISBN
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtISBN" ClientIDMode="Static" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">WordsCount
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtWordsCount" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">UnitPrice
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtUnitPrice" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">ContentDescription
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtContentDescription" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">AurhorDescription
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtAurhorDescription" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">EditorComment
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtEditorComment" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">TOC
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtTOC" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">CategoryId
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtCategoryId" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" width="30%" align="right">Clicks
    :</td>
                        <td height="25" width="*" align="left">
                            <asp:TextBox ID="txtClicks" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                </table>
                <script src="/js/calendar1.js" type="text/javascript"></script>

            </td>
        </tr>
        <tr>
            <td class="tdbg" align="center" valign="bottom">
                <span id="spanButtonPlaceholder" onclick="pictureUpload()"></span>
                <asp:Button ID="btnSave" runat="server" Text="保存" ClientIDMode="Static"
                    OnClick="btnSave_Click" class="inputbutton" onmouseover="this.className='inputbutton_hover'"
                    onmouseout="this.className='inputbutton'"></asp:Button>
                <asp:Button ID="btnCancle" runat="server" Text="取消"
                    OnClick="btnCancle_Click" class="inputbutton" onmouseover="this.className='inputbutton_hover'"
                    onmouseout="this.className='inputbutton'"></asp:Button>
            </td>
        </tr>
    </table>
    <br />
</asp:Content>
<%--<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceCheckright" runat="server">
</asp:Content>--%>
Add.aspx
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.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace BookShopManager.Web.Books
{
    public partial class Add : Page
    {
        public string ISBN { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            ISBN = "111";
            ISBN = txtISBN.Text;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtTitle.Text.Trim().Length == 0)
            {
                strErr += "Title不能为空!\\n";
            }
            if (this.txtAuthor.Text.Trim().Length == 0)
            {
                strErr += "Author不能为空!\\n";
            }
            if (!PageValidate.IsNumber(txtPublisherId.Text))
            {
                strErr += "PublisherId格式错误!\\n";
            }
            if (!PageValidate.IsDateTime(txtPublishDate.Text))
            {
                strErr += "PublishDate格式错误!\\n";
            }
            if (this.txtISBN.Text.Trim().Length == 0)
            {
                strErr += "ISBN不能为空!\\n";
            }
            if (!PageValidate.IsNumber(txtWordsCount.Text))
            {
                strErr += "WordsCount格式错误!\\n";
            }
            if (!PageValidate.IsDecimal(txtUnitPrice.Text))
            {
                strErr += "UnitPrice格式错误!\\n";
            }
            if (this.txtContentDescription.Text.Trim().Length == 0)
            {
                strErr += "ContentDescription不能为空!\\n";
            }
            if (this.txtAurhorDescription.Text.Trim().Length == 0)
            {
                strErr += "AurhorDescription不能为空!\\n";
            }
            if (this.txtEditorComment.Text.Trim().Length == 0)
            {
                strErr += "EditorComment不能为空!\\n";
            }
            if (this.txtTOC.Text.Trim().Length == 0)
            {
                strErr += "TOC不能为空!\\n";
            }
            if (!PageValidate.IsNumber(txtCategoryId.Text))
            {
                strErr += "CategoryId格式错误!\\n";
            }
            if (!PageValidate.IsNumber(txtClicks.Text))
            {
                strErr += "Clicks格式错误!\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string Title = this.txtTitle.Text;
            string Author = this.txtAuthor.Text;
            int PublisherId = int.Parse(this.txtPublisherId.Text);
            DateTime PublishDate = DateTime.Parse(this.txtPublishDate.Text);
            string ISBN = this.txtISBN.Text;
            int WordsCount = int.Parse(this.txtWordsCount.Text);
            decimal UnitPrice = decimal.Parse(this.txtUnitPrice.Text);
            string ContentDescription = this.txtContentDescription.Text;
            string AurhorDescription = this.txtAurhorDescription.Text;
            string EditorComment = this.txtEditorComment.Text;
            string TOC = this.txtTOC.Text;
            int CategoryId = int.Parse(this.txtCategoryId.Text);
            int Clicks = int.Parse(this.txtClicks.Text);

            BookShopManager.Model.Books model = new BookShopManager.Model.Books();
            model.Title = Title;
            model.Author = Author;
            model.PublisherId = PublisherId;
            model.PublishDate = PublishDate;
            model.ISBN = ISBN;
            model.WordsCount = WordsCount;
            model.UnitPrice = UnitPrice;
            model.ContentDescription = ContentDescription;
            model.AurhorDescription = AurhorDescription;
            model.EditorComment = EditorComment;
            model.TOC = TOC;
            model.CategoryId = CategoryId;
            model.Clicks = Clicks;

            BookShopManager.BLL.Books bll = new BookShopManager.BLL.Books();
            bll.Add(model);
            bll.CreateHTMLPage(model.ISBN);
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功!", "add.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
Add.aspx.cs

其中保存之前   bll.CreateHTMLPage(model.ISBN);

  /// <summary>
        /// 商品信息生成静态页面
        /// </summary>
        public void CreateHTMLPage(string ISDN) {
            Model.Books model = dal.GetModelByISBN(ISDN);
            //获取模板文件
            string template = HttpContext.Current.Request.MapPath("/Template/BookTemplate.html");
            string fileContent = File.ReadAllText(template);
            fileContent = fileContent.Replace("$title", model.Title).Replace("$author", model.Author).Replace("$isbn", model.ISBN).Replace("$unitprice", model.UnitPrice.ToString("0.00")).Replace("$Content", model.ContentDescription);
            //Directory.CreateDirectory(Path.GetDirectoryName(HttpContext.Current.Request.MapPath()));
            string fullDir = "/HtmlPage/" + model.ISBN + ".html";
            File.WriteAllText(HttpContext.Current.Request.MapPath(fullDir),fileContent,System.Text.Encoding.UTF8);
        }
商品信息生成静态页面CreateHTMLPage
<!DOCTYPE html>
<!--商品详细页布局-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="/Css/tableStyle.css" rel="stylesheet" />
    <!--在模板文件中引用外部文件时,一定要使用绝对路径-->
</head>
<body>
    <table>
        <tr>
            <td>书名</td><td>$title</td>
        </tr>
        <tr>
            <td>作者</td>
            <td>$author</td>
        </tr>
        <tr>
            <td>单价</td>
            <td>$unitprice</td>
        </tr>
        <tr>
            <td>封面</td>
            <td><img src="/Images/BookCovers/$isbn.jpg" /></td>
        </tr>
        <tr>
            <td>简介</td>
            <td>$Content</td>
        </tr>
        
    </table>
</body>
</html>
商品详细页布局模板

posted @ 2017-09-19 08:26  逍遥小天狼  阅读(81)  评论(0)    收藏  举报