当前文件路径:BlogEngine/admin/Pages/Pages.aspx.cs 
1
Using#region Using
2
3
using System;
4
using System.IO;
5
using System.Web;
6
using System.Text;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.HtmlControls;
9
using BlogEngine.Core;
10
11
#endregion
12
13
public partial class admin_Pages_pages : System.Web.UI.Page ...{
14
protected void Page_Load(object sender, EventArgs e) ...{
15
base.MaintainScrollPositionOnPostBack = true;
16
17
if (!Page.IsPostBack && !Page.IsCallback) ...{
18
if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 36) ...{
19
Guid id = new Guid(Request.QueryString["id"]);
20
BindPage(id);
21
BindParents(id);
22
} else ...{
23
BindParents(Guid.Empty);
24
}
25
26
BindPageList();
27
}
28
29
btnSave.Click += new EventHandler(btnSave_Click);
30
btnSave.Text = Resources.labels.savePage; // mono does not interpret the inline code correctly
31
btnUploadFile.Click += new EventHandler(btnUploadFile_Click);
32
btnUploadImage.Click += new EventHandler(btnUploadImage_Click);
33
Page.Title = Resources.labels.pages;
34
35
if (!Utils.IsMono)
36
Page.Form.DefaultButton = btnSave.UniqueID;
37
}
38
39
private void btnUploadImage_Click(object sender, EventArgs e) ...{
40
Upload(BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar, txtUploadImage);
41
string path = System.Web.VirtualPathUtility.ToAbsolute("~/");
42
string img = string.Format("<img src=\"{0}image.axd?picture={1}\" alt=\"\" />", path, Server.UrlEncode(txtUploadImage.FileName));
43
txtContent.Text += string.Format(img, txtUploadImage.FileName);
44
}
45
46
private void btnUploadFile_Click(object sender, EventArgs e) ...{
47
Upload(BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar, txtUploadFile);
48
49
string a = "<p><a href=\"{0}file.axd?file={1}\">{2}</a></p>";
50
string text = txtUploadFile.FileName + " (" + SizeFormat(txtUploadFile.FileBytes.Length, "N") + ")";
51
txtContent.Text += string.Format(a, Utils.RelativeWebRoot, Server.UrlEncode(txtUploadFile.FileName), text);
52
}
53
54
private void Upload(string virtualFolder, FileUpload control) ...{
55
string folder = Server.MapPath(virtualFolder);
56
control.PostedFile.SaveAs(folder + control.FileName);
57
}
58
59
private string SizeFormat(float size, string formatString) ...{
60
if (size < 1024)
61
return size.ToString(formatString) + " bytes";
62
63
if (size < Math.Pow(1024, 2))
64
return (size / 1024).ToString(formatString) + " kb";
65
66
if (size < Math.Pow(1024, 3))
67
return (size / Math.Pow(1024, 2)).ToString(formatString) + " mb";
68
69
if (size < Math.Pow(1024, 4))
70
return (size / Math.Pow(1024, 3)).ToString(formatString) + " gb";
71
72
return size.ToString(formatString);
73
}
74
75
Event handlers#region Event handlers
76
77
private void btnSave_Click(object sender, EventArgs e) ...{
78
if (!Page.IsValid)
79
throw new InvalidOperationException("One or more validators are invalid.");
80
81
Page page;
82
if (Request.QueryString["id"] != null)
83
page = BlogEngine.Core.Page.GetPage(new Guid(Request.QueryString["id"]));
84
else
85
page = new Page();
86
87
if (string.IsNullOrEmpty(txtContent.Text))
88
txtContent.Text = "[No text]";
89
90
page.Title = txtTitle.Text;
91
page.Content = txtContent.Text;
92
page.Description = txtDescription.Text;
93
page.Keywords = txtKeyword.Text;
94
95
if (cbIsFrontPage.Checked) ...{
96
foreach (Page otherPage in BlogEngine.Core.Page.Pages) ...{
97
if (otherPage.IsFrontPage) ...{
98
otherPage.IsFrontPage = false;
99
otherPage.Save();
100
}
101
}
102
}
103
104
page.IsFrontPage = cbIsFrontPage.Checked;
105
page.ShowInList = cbShowInList.Checked;
106
page.IsPublished = cbIsPublished.Checked;
107
108
if (ddlParent.SelectedIndex != 0)
109
page.Parent = new Guid(ddlParent.SelectedValue);
110
else
111
page.Parent = Guid.Empty;
112
113
page.Save();
114
115
Response.Redirect(page.RelativeLink.ToString());
116
}
117
118
#endregion
119
120
Data binding#region Data binding
121
122
private void BindPage(Guid pageId) ...{
123
Page page = BlogEngine.Core.Page.GetPage(pageId);
124
txtTitle.Text = page.Title;
125
txtContent.Text = page.Content;
126
txtDescription.Text = page.Description;
127
txtKeyword.Text = page.Keywords;
128
cbIsFrontPage.Checked = page.IsFrontPage;
129
cbShowInList.Checked = page.ShowInList;
130
cbIsPublished.Checked = page.IsPublished;
131
}
132
133
private void BindParents(Guid pageId) ...{
134
foreach (Page page in BlogEngine.Core.Page.Pages) ...{
135
if (pageId != page.Id)
136
ddlParent.Items.Add(new ListItem(page.Title, page.Id.ToString()));
137
}
138
139
ddlParent.Items.Insert(0, "-- " + Resources.labels.noParent + " --");
140
if (pageId != Guid.Empty) ...{
141
Page parent = BlogEngine.Core.Page.GetPage(pageId);
142
if (parent != null)
143
ddlParent.SelectedValue = parent.Parent.ToString();
144
}
145
}
146
147
private void BindPageList() ...{
148
foreach (Page page in BlogEngine.Core.Page.Pages) ...{
149
HtmlGenericControl li = new HtmlGenericControl("li");
150
HtmlAnchor a = new HtmlAnchor();
151
a.HRef = "?id=" + page.Id.ToString();
152
a.InnerHtml = page.Title;
153
154
System.Web.UI.LiteralControl text = new System.Web.UI.LiteralControl(" (" + page.DateCreated.ToString("yyyy-dd-MM HH:mm") + ")");
155
156
li.Controls.Add(a);
157
li.Controls.Add(text);
158
ulPages.Controls.Add(li);
159
}
160
161
divPages.Visible = true;
162
aPages.InnerHtml = BlogEngine.Core.Page.Pages.Count + " " + Resources.labels.pages;
163
}
164
165
#endregion
166
}
167