📄 createeditpost.cs
字号:
private void DisplayPreview(Control control) {
Button button;
// Wire up the back button
button = (Button) control.FindControl("BackButton");
button.Click += new System.EventHandler(BackButton_Click);
// Wire up the post button
button = (Button) control.FindControl("PreviewPostButton");
button.Click += new System.EventHandler(PostButton_Click);
}
/***********************************************************************
// BackButton_Click
//
/// <summary>
/// Event handler for the back button click from Preview mode
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
************************************************************************/
private void BackButton_Click(Object sender, EventArgs e) {
Control form;
// The event was raised by a button in the user control
// the is the UI for the form -- get the Parent, e.g. the User Control
form = ((Control)sender).Parent;
// Find and hide the Preview display
form.FindControl("Preview").Visible = false;
if (Mode == CreateEditPostMode.NewPost)
form.FindControl("ReplyTo").Visible = false;
else if (Mode == CreateEditPostMode.ReplyToPost)
form.FindControl("ReplyTo").Visible = true;
// Find and enable the Post
form.FindControl("Post").Visible = true;
}
/***********************************************************************
// PostButton_Click
//
/// <summary>
/// This event handler fires when the preview button is clicked. It needs
/// to show/hide the appropriate panels.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
************************************************************************/
private void PostButton_Click (Object sender, EventArgs e) {
Control form;
Post postRepliedTo;
// Only proceed if the post is valid
if (!Page.IsValid)
return;
// Get the user control that the click originated from
form = ((Control)sender).Parent;
// When we add a new post, we want to get back the NewPostID, so that we
// can automagically redirect the user to the page showing the post.
// If iNewPostID comes back as 0, though, that means that the post needs
// to be approved first, so the user is taken to a page explaining this.
Post newPost = null;
Post postToAdd = new Post();
postToAdd.Username = Context.User.Identity.Name;
postToAdd.ForumID = postToAdd.ParentID = 0;
postToAdd.Subject = ((TextBox) form.FindControl("PostSubject")).Text;
postToAdd.Body = ((TextBox) form.FindControl("PostBody")).Text;
postToAdd.IsLocked = allowNoReplies.Checked;
// Are we in edit mode?
/*
if (Mode == CreateEditPostMode.EditPost) {
string editNotes = CreateEditNotes(form);
postToAdd.Body = editNotes + postToAdd.Body;
}
*/
// Are we pinning the post?
if ((pinnedPost != null) && (Convert.ToInt32(pinnedPost.SelectedItem.Value) > 0)) {
switch (Convert.ToInt32(pinnedPost.SelectedItem.Value)) {
case 1:
postToAdd.PostDate = DateTime.Now.Date.AddDays(1);
break;
case 3:
postToAdd.PostDate = DateTime.Now.Date.AddDays(3);
break;
case 7:
postToAdd.PostDate = DateTime.Now.Date.AddDays(7);
break;
case 14:
postToAdd.PostDate = DateTime.Now.Date.AddDays(14);
break;
case 30:
postToAdd.PostDate = DateTime.Now.Date.AddMonths(1);
break;
case 90:
postToAdd.PostDate = DateTime.Now.Date.AddMonths(3);
break;
case 180:
postToAdd.PostDate = DateTime.Now.Date.AddMonths(6);
break;
case 360:
postToAdd.PostDate = DateTime.Now.Date.AddYears(1);
break;
case 999:
postToAdd.PostDate = DateTime.Now.Date.AddYears(25);
break;
}
}
// Are we adding a new post, editing an existing one, or replying to an existing one?
switch (Mode) {
case CreateEditPostMode.NewPost: // adding a new post
postToAdd.ForumID = ForumID; // specify the forum ID that the new post belongs
try {
newPost = Posts.AddPost(postToAdd);
}
catch (PostDuplicateException postdup) {
Context.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.DuplicatePost) + "&ForumId=" + ForumID);
Context.Response.End();
}
break;
case CreateEditPostMode.ReplyToPost: // replying to an existing post
try {
// if (postView == ViewOptions.Threaded) {
postToAdd.ParentID = PostID; // specify the post we are replying to
// } else {
// postRepliedTo = Posts.GetPost(PostID, user.Username);
// postToAdd.ParentID = postRepliedTo.ThreadID;
// }
newPost = Posts.AddPost(postToAdd);
}
catch (Components.PostNotFoundException exp) {
// uh-oh, something is off... are we replying to a message that has been deleted?
Context.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.ProblemPosting));
Context.Response.End();
}
catch (PostDuplicateException postdup) {
Context.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.DuplicatePost) + "&PostId=" + PostID);
Context.Response.End();
}
break;
case CreateEditPostMode.EditPost:
postToAdd.PostID = PostID; // specify the ID of the post we are updating
string editedBy = Users.GetLoggedOnUser().Username;
// update the post
Posts.UpdatePost(postToAdd, editedBy);
// send the user back to from where they came
Context.Response.Redirect(RedirectUrl);
Context.Response.End();
// exit from the event handler
return;
}
// now that we've added the post, redirect the user to the post display (if the post
// has been approved)
if (newPost.Approved) {
if (Mode == CreateEditPostMode.NewPost)
Context.Response.Redirect(Globals.UrlShowPost + newPost.ThreadID);
else
Context.Response.Redirect(Globals.UrlShowPost + newPost.ThreadID + "#" + newPost.PostID);
Context.Response.End();
} else {
// if the post HASN'T been approved, send the user to an explanation page, passing along the Post or ForumID
String strRedirect = Globals.UrlMessage + Convert.ToInt32(Messages.PostPendingModeration);
if (ForumID > 0)
Context.Response.Redirect(strRedirect + "&ForumID=" + ForumID.ToString());
else
Context.Response.Redirect(strRedirect + "&PostID=" + PostID.ToString());
}
}
/***********************************************************************
// CancelButton_Click
//
/// <summary>
/// Event raised when the user decides to cancel the post.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
************************************************************************/
private void CancelButton_Click(Object sender, EventArgs e) {
Post post = Posts.GetPost(PostID, null);
Page.Response.Redirect(Globals.UrlShowPost + post.ThreadID + "#" + PostID);
Page.Response.End();
}
/***********************************************************************
// PreviewButton_Click
//
/// <summary>
/// This event handler fires when the preview button is clicked. It needs
/// to show/hide the appropriate panels.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
************************************************************************/
private void PreviewButton_Click(Object sender, EventArgs e) {
Control form;
Label label;
TextBox textbox;
Button button;
// only do this stuff if the page is valid
if (!Page.IsValid)
return;
// The event was raised by a button in the user control
// the is the UI for the form -- get the Parent, e.g. the User Control
form = ((Control)sender).Parent;
// Find and enable the Preview display
form.FindControl("Preview").Visible = true;
// Find and hide the ReplyTo display and Post
form.FindControl("ReplyTo").Visible = false;
form.FindControl("Post").Visible = false;
// Set the title text
((Label) form.FindControl("PostTitle")).Text = "Preview Message";
// Preview the post subject
label = (Label) form.FindControl("PreviewSubject");
textbox = (TextBox) form.FindControl("PostSubject");
label.Text = Globals.HtmlEncode(textbox.Text);
// Preview the post body
label = (Label) form.FindControl("PreviewBody");
textbox = (TextBox) form.FindControl("PostBody");
// Are we in edit mode?
/*
if (Mode == CreateEditPostMode.EditPost) {
string editNotes = CreateEditNotes(form);
label.Text = Globals.FormatPostBody(editNotes + textbox.Text) + Globals.FormatSignature(user.Signature);
} else {
}
*/
label.Text = Globals.FormatPostBody(textbox.Text) + Globals.FormatSignature(user.Signature);
}
/***********************************************************************
// CreateEditNotes
//
/// <summary>
/// When a post is edited we add some notes to the post.
/// </summary>
***********************************************************************/
private string CreateEditNotes(Control form) {
string editNotes;
TextBox textbox;
// Get the edit notes that the editor submitted
textbox = (TextBox) form.FindControl("EditNotesBody");
// Create the edit notes string
editNotes = "[Edit by=\"" + user.Username + "\"]" + textbox.Text + "[/Edit]\n";
return editNotes;
}
/***********************************************************************
// DisplayReply
//
/// <summary>
/// When a user replies to a post, the user control that controls the UI
/// is loaded and passed to this method. Details such as the username, subject,
/// and message are extracted and displayed.
/// </summary>
/// <param name="control">Usercontrol used to control UI formatting</param>
***********************************************************************/
private void DisplayReply(Control control) {
PostDetails post = null;
HyperLink hyperlink;
if (Mode == CreateEditPostMode.NewPost)
return;
// Set the visibility
((Control) control.FindControl("ReplyTo")).Visible = true;
// Read in information about the post we are replying to
try {
post = Posts.GetPostDetails(PostID, Context.User.Identity.Name);
} catch (Components.PostNotFoundException postNotFound) {
HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.PostDoesNotExist));
HttpContext.Current.Response.End();
}
// Set the Username
hyperlink = (HyperLink) control.FindControl("ReplyPostedBy");
hyperlink.Text = post.Username;
hyperlink.NavigateUrl = Globals.UrlUserProfile + post.Username;
// Set the date
((Label) control.FindControl("ReplyPostedByDate")).Text = " on " + post.PostDate.ToString(user.DateFormat + " " + Globals.TimeFormat);
// Set the Subject
hyperlink = (HyperLink) control.FindControl("ReplySubject");
hyperlink.Text = post.Subject;
hyperlink.NavigateUrl = Globals.UrlShowPost + post.PostID;
// Set the Body
((Label) control.FindControl("ReplyBody")).Text = Globals.FormatPostBody(post.Body);
}
/************ PROPERTY SET/GET STATEMENTS **************/
/// <summary>
/// Indicates the style and UI settings for the post. Through this property you can set
/// features like the columns and rows in the post body text box, and other such settings.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -