📄 postview.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetForums;
using AspNetForums.Controls.Moderation;
using AspNetForums.Components;
using System.ComponentModel;
using System.IO;
namespace AspNetForums.Controls {
// *********************************************************************
// PostView
//
/// <summary>
/// This Web controls how posts are displayed.
/// </summary>
/// <remarks>
/// If the user attempts to view a post that has not yet been approved or a post that
/// does not exist (perhaps one that has been deleted), they will be taken to the appropriate
/// error message page. If the user attempts to load the control without specifying a PostID
/// in the PostID property, an Exception is thrown.
/// </remarks>
///
// ********************************************************************/
[
ParseChildren(true)
]
public class PostView : WebControl, INamingContainer {
Paging pager;
PostList postList;
ViewOptions postView = ViewOptions.Flat;
Forum forum;
bool defaultShowSignature = true;
string defaultPostDateTimeFormat = "MMM d, yyyy - h:mm tt";
int threshHold = 3;
Control controlTemplate;
string siteStyle;
User user;
DropDownList displayMode;
DropDownList sortOrder;
string templateName = "Skin-ShowPostList.ascx";
ModerationMenu moderationMenu;
CheckBox emailTracking;
// *********************************************************************
// PostView
//
/// <summary>
/// Class contructor - read in the PostId that was sent via the
/// QueryString or Post body of the request
/// </summary>
///
// ********************************************************************/
public PostView() {
// If we have an instance of context, let's attempt to
// get the ForumID so we can save the user from writing
// the code
if (null != Context) {
try {
if (null != Context.Request.QueryString["PostID"]) {
string postID = Context.Request.QueryString["PostID"];
// Contains a #
if (postID.IndexOf("#") > 0)
postID = postID.Substring(0, postID.IndexOf("#"));
this.PostID = Convert.ToInt32(postID);
} else if (null != Context.Request.Form["PostId"]) {
this.PostID = Convert.ToInt32(Context.Request.Form["PostID"]);
}
} catch (Exception e) {
HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.PostDoesNotExist));
HttpContext.Current.Response.End();
}
// Was a view option passed in via the querystring?
if (null != Context.Request.QueryString["View"])
if (Context.Request.QueryString["View"] == "Threaded")
ViewMode = ViewOptions.Threaded;
// Was a threshhold value passed in
if (null != Context.Request.QueryString["ThreshHold"])
threshHold = Convert.ToInt32(Context.Request.QueryString["ThreshHold"]);
}
// Populate get details about the forum we are in
try {
forum = Forums.GetForumInfoByPostID(PostID);
} catch (Components.ForumNotFoundException fnf) {
HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.UnknownForum));
HttpContext.Current.Response.End();
}
}
// *********************************************************************
// CreateChildControls
//
/// <summary>
/// This event handler adds the children controls. FIrst we apply templates
/// for this control. We then determine how we render the post items, either threaded
/// or flat. We also apply templates defined for each of the post item types.
/// </summary>
///
// ********************************************************************/
protected override void CreateChildControls() {
// Attempt to get the user name
if (Page.Request.IsAuthenticated) {
user = Users.GetLoggedOnUser();
}
// Set the siteStyle for the page
if (user != null)
siteStyle = user.SiteStyle;
else
siteStyle = Globals.SiteStyle;
// Set the view mode
SetViewMode();
// Call CreateChildControls
base.CreateChildControls();
}
// *********************************************************************
// OnPreRender
//
/// <summary>
/// Override OnPreRender and databind
/// </summary>
///
// ********************************************************************/
private void SetViewMode() {
// Render a threaded or flat view
if (ViewMode == ViewOptions.Threaded) {
// Attempt to load the control. If this fails, we're done
try {
controlTemplate = Page.LoadControl(Globals.ApplicationVRoot + "/skins/" + Globals.Skin + "/Skins/Skin-ShowPostThreaded.ascx");
} catch (FileNotFoundException e) {
throw new Exception("The user control skins/Skins/Skin-ShowPostThreaded.ascx was not found. Please ensure this file exists in your skins directory");
}
// Initialize the control template
InitializePostThreadViewControlTemplate();
} else {
try {
controlTemplate = Page.LoadControl(Globals.ApplicationVRoot + "/skins/" + Globals.Skin + "/Skins/" + TemplateName);
} catch (FileNotFoundException e) {
throw new Exception("The user control skins/Skins/" + TemplateName + "was not found. Please ensure this file exists in your skins directory");
}
// Initialize the control template
InitializePostListControlTemplate();
}
// Add the control template to the controls collection
Controls.Add(controlTemplate);
}
// *********************************************************************
// InitializePostThreadViewControlTemplate
//
/// <summary>
/// Render the post threaded view
/// </summary>
///
// ********************************************************************/
private void InitializePostThreadViewControlTemplate() {
PostThreaded postThreaded;
// Initialize common display elements
InitializeCommonTemplateItems();
// Get the threadview control
postThreaded = (PostThreaded) controlTemplate.FindControl("ThreadView");
postThreaded.PostID = PostID;
// Set the threshhold
postThreaded.Threshhold = ThreshHoldForThreadedView;
Controls.Add(controlTemplate);
}
private void InitializeCommonTemplateItems() {
System.Web.UI.WebControls.Image newThreadImage;
HyperLink forumName;
HyperLink newThreadLink;
// Clear the controls collection
Controls.Clear();
// Find the forum name link
forumName = (HyperLink) controlTemplate.FindControl("ForumName");
if (forumName != null) {
forumName.Text = forum.Name;
forumName.NavigateUrl = Globals.UrlShowForum + forum.ForumID;
}
// Find the Display Mode control
displayMode = (DropDownList) controlTemplate.FindControl("DisplayMode");
if (displayMode != null) {
displayMode.AutoPostBack = true;
displayMode.SelectedIndexChanged += new System.EventHandler(DisplayMode_Changed);
}
// Find the email tracking option
emailTracking = (CheckBox) controlTemplate.FindControl("TrackThread");
if ((user != null) && (emailTracking != null)) {
emailTracking.Visible = true;
PostDetails postDetails = Posts.GetPostDetails(PostID, user.Username);
emailTracking.AutoPostBack = true;
emailTracking.CheckedChanged += new System.EventHandler(Toggle_ThreadTracking);
emailTracking.Checked = postDetails.ThreadTracking;
} else {
emailTracking.Visible = false;
}
// Find the new thread link
newThreadLink = (HyperLink) controlTemplate.FindControl("NewThreadLinkTop");
if (newThreadLink != null) {
newThreadLink.NavigateUrl = Globals.UrlAddNewPost + forum.ForumID;
// Find the new thread iamge
newThreadImage = (System.Web.UI.WebControls.Image) controlTemplate.FindControl("NewThreadImageTop");
if (newThreadImage != null) {
newThreadImage.ImageUrl = Globals.ApplicationVRoot + "/skins/" + siteStyle + "/images/newthread.gif";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -