当前文件路径:BlogEngine/admin/Pages/Add_entry.aspx.cs 
1
Using#region Using
2
3
using System;
4
using System.Web;
5
using System.Text;
6
using System.IO;
7
using System.Web.Security;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.HtmlControls;
10
using System.Threading;
11
using BlogEngine.Core;
12
13
#endregion
14
15
public partial class admin_entry : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
16
...{
17
protected void Page_Load(object sender, EventArgs e)
18
...{
19
this.MaintainScrollPositionOnPostBack = true;
20
if (!Page.IsPostBack && !Page.IsCallback)
21
...{
22
BindCategories();
23
BindUsers();
24
BindDrafts();
25
26
Page.Title = Resources.labels.add_Entry;
27
Page.ClientScript.GetCallbackEventReference(this, "title", "ApplyCallback", "slug");
28
29
if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 36)
30
...{
31
Guid id = new Guid(Request.QueryString["id"]);
32
Page.Title = "Edit post";
33
BindPost(id);
34
}
35
else
36
...{
37
PreSelectAuthor(Page.User.Identity.Name);
38
txtDate.Text = DateTime.Now.AddHours(BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm");
39
cbEnableComments.Checked = BlogSettings.Instance.IsCommentsEnabled;
40
if (Session["content"] != null)
41
...{
42
txtContent.Text = Session["content"].ToString();
43
txtTitle.Text = Session["title"].ToString();
44
txtDescription.Text = Session["description"].ToString();
45
txtSlug.Text = Session["slug"].ToString();
46
txtTags.Text = Session["tags"].ToString();
47
}
48
BindBookmarklet();
49
}
50
51
if (!Page.User.IsInRole("administrators"))
52
ddlAuthor.Enabled = false;
53
54
cbEnableComments.Enabled = BlogSettings.Instance.IsCommentsEnabled;
55
if (!Utils.IsMono) Page.Form.DefaultButton = btnSave.UniqueID;
56
}
57
58
btnSave.Text = Resources.labels.savePost; // mono does not interpret the inline code correctly
59
btnSave.Click += new EventHandler(btnSave_Click);
60
btnCategory.Click += new EventHandler(btnCategory_Click);
61
btnUploadFile.Click += new EventHandler(btnUploadFile_Click);
62
btnUploadImage.Click += new EventHandler(btnUploadImage_Click);
63
valExist.ServerValidate += new ServerValidateEventHandler(valExist_ServerValidate);
64
}
65
66
private void valExist_ServerValidate(object source, ServerValidateEventArgs args)
67
...{
68
args.IsValid = true;
69
70
foreach (Category cat in Category.Categories)
71
...{
72
if (cat.Title.Equals(txtCategory.Text.Trim(), StringComparison.OrdinalIgnoreCase))
73
args.IsValid = false;
74
}
75
}
76
77
private void btnUploadImage_Click(object sender, EventArgs e)
78
...{
79
Upload(BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar, txtUploadImage);
80
string path = Utils.AbsoluteWebRoot.ToString();
81
string img = string.Format("<img src=\"{0}image.axd?picture={1}\" alt=\"\" />", path, Server.UrlEncode(txtUploadImage.FileName));
82
txtContent.Text += string.Format(img, txtUploadImage.FileName);
83
}
84
85
private void btnUploadFile_Click(object sender, EventArgs e)
86
...{
87
Upload(BlogSettings.Instance.StorageLocation + "files" + Path.DirectorySeparatorChar, txtUploadFile);
88
89
string a = "<p><a href=\"{0}file.axd?file={1}\" rel=\"enclosure\">{2}</a></p>";
90
string text = txtUploadFile.FileName + " (" + SizeFormat(txtUploadFile.FileBytes.Length, "N") + ")";
91
txtContent.Text += string.Format(a, Utils.RelativeWebRoot, Server.UrlEncode(txtUploadFile.FileName), text);
92
}
93
94
private void Upload(string virtualFolder, FileUpload control)
95
...{
96
string folder = Server.MapPath(virtualFolder);
97
control.PostedFile.SaveAs(folder + control.FileName);
98
}
99
100
private string SizeFormat(float size, string formatString)
101
...{
102
if (size < 1024)
103
return size.ToString(formatString) + " bytes";
104
105
if (size < Math.Pow(1024, 2))
106
return (size / 1024).ToString(formatString) + " kb";
107
108
if (size < Math.Pow(1024, 3))
109
return (size / Math.Pow(1024, 2)).ToString(formatString) + " mb";
110
111
if (size < Math.Pow(1024, 4))
112
return (size / Math.Pow(1024, 3)).ToString(formatString) + " gb";
113
114
return size.ToString(formatString);
115
}
116
117
Event handlers#region Event handlers
118
119
/**//// <summary>
120
/// Creates and saves a new category
121
/// </summary>
122
private void btnCategory_Click(object sender, EventArgs e)
123
...{
124
if (Page.IsValid)
125
...{
126
Category cat = new Category(txtCategory.Text, string.Empty);
127
cat.Save();
128
ListItem item = new ListItem(Server.HtmlEncode(txtCategory.Text), cat.Id.ToString());
129
item.Selected = true;
130
cblCategories.Items.Add(item);
131
}
132
}
133
134
/**//// <summary>
135
/// Saves the post
136
/// </summary>
137
private void btnSave_Click(object sender, EventArgs e)
138
...{
139
if (!Page.IsValid)
140
throw new InvalidOperationException("One or more validators are invalid.");
141
142
Post post;
143
if (Request.QueryString["id"] != null)
144
post = Post.GetPost(new Guid(Request.QueryString["id"]));
145
else
146
post = new Post();
147
148
if (string.IsNullOrEmpty(txtContent.Text))
149
txtContent.Text = "[No text]";
150
151
post.DateCreated = DateTime.ParseExact(txtDate.Text, "yyyy-MM-dd HH:mm", null).AddHours(-BlogSettings.Instance.Timezone);
152
post.Author = ddlAuthor.SelectedValue;
153
post.Title = txtTitle.Text.Trim();
154
post.Content = txtContent.Text;
155
post.Description = txtDescription.Text.Trim();
156
post.IsPublished = cbPublish.Checked;
157
post.IsCommentsEnabled = cbEnableComments.Checked;
158
159
if (!string.IsNullOrEmpty(txtSlug.Text))
160
post.Slug = Server.UrlDecode(txtSlug.Text.Trim());
161
162
post.Categories.Clear();
163
164
foreach (ListItem item in cblCategories.Items)
165
...{
166
if (item.Selected)
167
post.Categories.Add(Category.GetCategory(new Guid(item.Value)));
168
}
169
170
post.Tags.Clear();
171
if (txtTags.Text.Trim().Length > 0)
172
...{
173
string[] tags = txtTags.Text.Split(',');
174
foreach (string tag in tags)
175
...{
176
post.Tags.Add(tag.Trim().ToLowerInvariant());
177
}
178
}
179
180
post.Save();
181
182
Session.Remove("content");
183
Session.Remove("title");
184
Session.Remove("description");
185
Session.Remove("slug");
186
Session.Remove("tags");
187
Response.Redirect(post.RelativeLink.ToString());
188
}
189
190
#endregion
191
192
Data binding#region Data binding
193
194
private void BindCategories()
195
...{
196
foreach (Category cat in Category.Categories)
197
...{
198
cblCategories.Items.Add(new ListItem(Server.HtmlEncode(cat.Title), cat.Id.ToString()));
199
}
200
}
201
202
private void BindPost(Guid postId)
203
...{
204
Post post = Post.GetPost(postId);
205
txtTitle.Text = post.Title;
206
txtContent.Text = post.Content;
207
txtDescription.Text = post.Description;
208
txtDate.Text = post.DateCreated.ToString("yyyy-MM-dd HH:mm");
209
cbEnableComments.Checked = post.IsCommentsEnabled;
210
cbPublish.Checked = post.IsPublished;
211
txtSlug.Text = Utils.RemoveIllegalCharacters(post.Slug);
212
213
PreSelectAuthor(post.Author);
214
215
foreach (Category cat in post.Categories)
216
...{
217
ListItem item = cblCategories.Items.FindByValue(cat.Id.ToString());
218
if (item != null)
219
item.Selected = true;
220
}
221
222
string[] tags = new string[post.Tags.Count];
223
for (int i = 0; i < post.Tags.Count; i++)
224
...{
225
tags[i] = post.Tags[i];
226
}
227
txtTags.Text = string.Join(",", tags);
228
}
229
230
private void PreSelectAuthor(string author)
231
...{
232
ddlAuthor.ClearSelection();
233
foreach (ListItem item in ddlAuthor.Items)
234
...{
235
if (item.Text.Equals(author, StringComparison.OrdinalIgnoreCase))
236
...{
237
item.Selected = true;
238
break;
239
}
240
}
241
}
242
243
private void BindBookmarklet()
244
...{
245
if (Request.QueryString["title"] != null && Request.QueryString["url"] != null)
246
...{
247
string title = Request.QueryString["title"];
248
string url = Request.QueryString["url"];
249
250
txtTitle.Text = title;
251
txtContent.Text = string.Format("<p><a href=\"{0}\" title=\"{1}\">{1}</a></p>", url, title);
252
}
253
}
254
255
private void BindUsers()
256
...{
257
foreach (MembershipUser user in Membership.GetAllUsers())
258
...{
259
ddlAuthor.Items.Add(user.UserName);
260
}
261
}
262
263
private void BindDrafts()
264
...{
265
Guid id = Guid.Empty;
266
if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 36)
267
...{
268
id = new Guid(Request.QueryString["id"]);
269
}
270
271
int counter = 0;
272
273
foreach (Post post in Post.Posts)
274
...{
275
if (!post.IsPublished && post.Id != id)
276
...{
277
HtmlGenericControl li = new HtmlGenericControl("li");
278
HtmlAnchor a = new HtmlAnchor();
279
a.HRef = "?id=" + post.Id.ToString();
280
a.InnerHtml = post.Title;
281
282
System.Web.UI.LiteralControl text = new System.Web.UI.LiteralControl(" by " + post.Author + " (" + post.DateCreated.ToString("yyyy-dd-MM HH:mm") + ")");
283
284
li.Controls.Add(a);
285
li.Controls.Add(text);
286
ulDrafts.Controls.Add(li);
287
counter++;
288
}
289
}
290
291
if (counter > 0)
292
...{
293
divDrafts.Visible = true;
294
aDrafts.InnerHtml = string.Format(Resources.labels.thereAreXDrafts, counter);
295
}
296
}
297
298
#endregion
299
300
301
ICallbackEventHandler Members#region ICallbackEventHandler Members
302
303