0%

生成多个新页面例子

第一步:aspx界面搭建

界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%-- 添加数据表单 --%>
<input type="submit" value="生成静态页面" />
</div>
</form>
</body>
</html>

后台

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookShop.Web.AdminManager
{
public partial class CreateStaticPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//生成页面 提交表单 Post
//测试 要为所有的课本生成静态页面
if (IsPostBack)
{
BLL.BookManager bll = new BLL.BookManager();
List<Model.Book> list = bll.GetModelList("");//获取所有的书本
foreach (Model.Book item in list)
{
//为每一本书都调用生成静态页面的方法
bll.CreateStaticHtmlPage(item.Id);
}
}
}
}
}

第二步:按要求生成目录和html

模板页 BookTemplate.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
/*//设置加入购物车超链接的样式*/
.buy_now {
text-decoration: none;
font-weight: 700;
margin-right: 10px;
display: inline-block;
text-align: center;
vertical-align: middle;
background-color: #df3033;
color: #fff;
}
</style>

<link href="/Css/tableStyle.css" rel="stylesheet" />
<!--使用样式的时候,一定要记得修改为绝对路径-->
<script src="/js/jquery-1.7.1.js"></script>
<!--使用样式的时候,一定要记得修改为绝对路径-->
<script src="/ckeditor/ckeditor.js"></script>
<link href="/Css/themes/ui-lightness/jquery-ui-1.8.2.custom.css" rel="stylesheet" />
<script src="/js/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnAdd").click(function () {
AddComment();
});
LoadComment();
//加载一个富文本编辑器
LoadBBCode();
//给加入购物车注册事件
$("#addCart").click(function () {
//跳出一个模态页面 1.提示加入购物车成功 2.请登录(Session)

AddProductCart();
});
});
function AddProductCart() {
//购物车表中需要用户Id和商品ID 用户Id来自Session
$.post("/ashx/ProcessCart.ashx", { "bookId": $bookId }, function (data) {
var serverData = data.split(':');
if (serverData[0] == "ok") {
//登陆成功

$("#msg").text(serverData[1]);
$("#showResult").css("display", "block");
$("#showResult").dialog({
height: 260,
width: 300,
modal: true,
buttons: {
ok: function () {
$(this).dialog("close");

}
}
})

}
else if (serverData[0] == "no") {
//登陆成功,添加失败
}
else {
//如果Login 提示登陆
ShowLoginDialog();
}

});

}
//弹出登陆窗口 窗口用一个Jquery—UI
function ShowLoginDialog() {
//显示登陆窗口 做一个div
$("#loginDiv").css("display", "block");
$("#loginDiv").dialog({
height: 260,
width: 350,
modal: true,
buttons: {
ok: function () {

var txtUserName = $("#txtUserName").val();
var txtPwd = $("#txtPwd").val();
$.post("/ashx/Login.ashx", { "txtUserName": txtUserName, "txtPwd": txtPwd }, function (data) {
if (data == "ok") {

alert("登录失败,请输入正确的用户名和密码!");

}
else {
$(this).dialog("close");
alert(data);
window.location.href = "/shoppingCart/shoppingCart.aspx";
}

});
}
}
})
}
//BBCode
function LoadBBCode() {
//把我们的textarea 替换成了 BBCode编辑器
CKEDITOR.replace('txtContent',
{
extraPlugins: 'bbcode',
removePlugins: 'bidi,button,dialogadvtab,div,filebrowser,flash,format,forms,horizontalrule,iframe,indent,justify,liststyle,pagebreak,showborders,stylescombo,table,tabletools,templates',
toolbar:
[
['Source', '-', 'Save', 'NewPage', '-', 'Undo', 'Redo'],
['Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
['Link', 'Unlink', 'Image'],
'/',
['FontSize', 'Bold', 'Italic', 'Underline'],
['NumberedList', 'BulletedList', '-', 'Blockquote'],
['TextColor', '-', 'Smiley', 'SpecialChar', '-', 'Maximize']
],
smiley_images:
[
'regular_smile.gif', 'sad_smile.gif', 'wink_smile.gif', 'teeth_smile.gif', 'tounge_smile.gif',
'embaressed_smile.gif', 'omg_smile.gif', 'whatchutalkingabout_smile.gif', 'angel_smile.gif', 'shades_smile.gif',
'cry_smile.gif', 'kiss.gif'
],
smiley_descriptions:
[
'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
'indecision', 'angel', 'cool', 'crying', 'kiss'
]
});
}
//加载评论
function LoadComment() {
$.post("/ashx/BookComment.ashx", { "action": "load", "bookId": $bookId }, function (data) {
//获取到data 添加到ul标签
var serverData = $.parseJSON(data);

var serverDataLength = serverData.length;
//alert(serverDataLength);
for (var i = 0; i < serverDataLength; i++) {
//alert(serverData[i].Msg);
$("<li>" + serverData[i].ShowTime + ":" + serverData[i].Msg + "</li>").
appendTo("#commentList");
}

})
}
//添加评论
function AddComment() {
//var msg = $("#txtContent").val();
//获取用户输入的评论
var oEditor = CKEDITOR.instances.txtContent;
//alert( oEditor.getData() );
var msg = oEditor.getData();
//现在获取不到,数据库加载出来的都是空
if (msg != null) {
//$bookId 被替换掉
$.post("/ashx/BookComment.ashx", { "action": "add", "msg": msg, "bookId": $bookId }, function (data) {
var serverData = data.split(':');//no 评论中…………
if (serverData[0] == "ok") {
//情况内容 重新获取光标
//$("#txtContent").val("");
oEditor.setData("");//文本编辑框设置为空
$("#txtContent").focus();
$("#txtMsg").text(serverData[1]);
}
else {
$("#txtMsg").text(serverData[1]);
}
});
}
else {
$("#txtMsg").text("评论不能为空");
$("#txtContent").focus();
}
}
</script>
</head>
<body>
<table>
<tr><td>书名</td><td>$title</td></tr>
<tr><td>作者</td><td>$author</td></tr>
<tr><td>单价</td><td>$unitprice &nbsp;&nbsp;<a href="javascript:void(0)" class="buy_now" id="addCart">加入购物车</a></td></tr>
<tr><td>封面</td><td><img src="/Images/BookCovers/$isbn.jpg" /></td></tr>
<tr><td>简介</td><td>$content</td></tr>
</table>
<hr />
<ul id="commentList" class="comments"></ul>
<textarea id="txtContent" rows="10" cols="100" class="reply_box" placeholder="用什么感谢?留下吧…………"></textarea>

<input type="button" value="发布评论" id="btnAdd" class="reply_btn" /><span id="txtMsg" style="font-size:20px;color:yellow"></span>
<div id="loginDiv" style="display:none;" title="登录" >
<strong style="margin-top:20px;">用户名:</strong><input type="text" id="txtUserName" style="margin-top:20px;"/><br />
<strong style="margin-top:10px;">密 码:</strong><input type="password" id="txtPwd" style="margin-top:10px;"/><br />
</div>
<div id="showResult" style="display:none">
<span id="msg"></span>访问<a style="font-size:20px;color:red" href="/shoppingCart/shoppingCart.aspx">购物车</a>页面,进行支付
</div>
</body>
</html>

页面生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <summary>
/// 将商品信息生成静态页面
/// </summary>
/// <param name="id"></param>
public void CreateStaticHtmlPage(int id)
{
//读取模板页BookTemplate,替换占位符$title
//替换成根据ID从数据库读取的数据
//1.读取商品ID,找到对应的商品信息
Model.Book model = dal.GetModel(id);
//2.读取模板文件 IO
string template = HttpContext.Current.Request.MapPath("/Template/BookTemplate.html");
//3.替换
string fileContent = File.ReadAllText(template);
fileContent = fileContent.Replace("$title", model.Title).Replace("$author", model.Author).
Replace("$unitprice", model.UnitPrice.ToString("0.00")).Replace("$isbn", model.ISBN).
Replace("$content", model.ContentDescription).Replace("$bookId", model.Id.ToString());
//如果是数字需要转换一下 例如"$bookId", model.Id.ToString()

//4.生成静态文件 保存文件 可以根据书本出版信息分类
string dir = "/HtmlPage/" + model.PublishDate.Year
+ "/" + model.PublishDate.Month
+ "/" + model.PublishDate.Day + "/";
//目录结构 /HtmlPage/2015/3/4/mme.html
Directory.CreateDirectory(Path.GetDirectoryName(HttpContext.Current.Request.MapPath(dir)));
//请求路径转换为绝对路径
string fullDir = dir + model.Id + ".html";
File.WriteAllText(HttpContext.Current.Request.MapPath(fullDir),
fileContent, System.Text.Encoding.UTF8);
}

提醒:

Eval返回一个object类型的返回值

获取返回的东西可以使用以下方法

1
2
3
4
5
6
public string GetString(object obj)
{
//Eval() 返回的是Obj
DateTime t = Convert.ToDateTime(obj);
return "/HtmlPage/" + t.Year + "/" + t.Month + "/" + t.Day + "/";
}