📄 adminforumpostlisting.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.Components;
using System.ComponentModel;
namespace AspNetForums.Controls.Admin {
/// <summary>
/// This Web control displays a list of the posts for a particular forum and allows the
/// administrator (hopefully not a regular user) to edit and delete these posts. Note that
/// deleting a post deletes the post and all of its replies.
/// </summary>
/// <remarks>
/// Note that only those posts that fall within the first "page" of posts are displayed for
/// the forum. No means to navigate backward or forward is present. This is a bit restrictive,
/// since it limits the administrator to being able to only edit or delete posts in a forum that
/// fall within the default timespan for the forum.
/// </remarks>
[
ParseChildren(true),
Designer("WebForumsControls.Design", "AdminForumPostListing")
]
public class AdminForumPostListing : WebControl, INamingContainer {
// Create the variables to create the DataList
DataList dlForumView;
DataBoundLiteralControl dataBoundLiteralControl;
int iUserTimezone = Globals.DBTimezone;
DeletePostStyle deletePostStyle;
// the default view of the forum posts
const ViewOptions defaultViewOptions = ViewOptions.Threaded;
// the default paths to the edit and delete images
const String defaultEditPostIconUrl = "edit.gif";
const String defaultDeletePostIconUrl = "delete.gif";
// whether or not to display the "Reasons for Deleting the Post" textbox
const bool defaultShowReasonsTextBox = true;
const String defaultIndentString = " ";
const String defaultThreadSeparator = "<hr noshade size=\"1px\">\n";
// *********************************************************************
//
// AdminForumPostListing Constructor
//
/// <summary>
/// The constructor simply creates an instance of the DataList and sets
/// the ItemTemplate to a new CompiledTemplateBuilder instance.
/// </summary>
//
// ********************************************************************/
public AdminForumPostListing() {
deletePostStyle = new DeletePostStyle();
// create an instance of the datalist and set its properties
dlForumView = new DataList();
dlForumView.ID = "dlForumView";
// set the ItemTemplate property
dlForumView.ItemTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildItemTemplate));
}
// *********************************************************************
//
// HandleImageButtonDataBind Event Handler
//
/// <summary>
/// This event handler fires when the DataList is databound. Here we
/// must set the ImageButton items' CommandArgument property to the
/// current PostID.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//
// ********************************************************************/
private void HandleImageButtonDataBind(Object sender, EventArgs e) {
ImageButton target = (ImageButton) sender;
DataListItem Container = (DataListItem) target.NamingContainer;
// Set the ImageButton's CommandArgument
target.CommandArgument = DataBinder.Eval(Container.DataItem, "PostID").ToString();
}
// *********************************************************************
//
// BeginDataBinding Event Handler
//
/// <summary>
/// This event handler is called when the DataList's DataBound method is
/// called. This event handler needs to add the DataBoundStrings to the
/// DataListItem. Since the ImageButtons break up the DataBinding Literal
/// and DataBound strings, we have two functions: BeginDataBinding and
/// FinishDataBinding. This one, begin, is responsible for the databound
/// content BEFORE the imageButtons.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//
// ********************************************************************/
private void BeginDataBinding(Object sender, EventArgs e) {
DataBoundLiteralControl target = (DataBoundLiteralControl) sender;
DataListItem Container = (DataListItem) target.NamingContainer;
// Add the DataBoundString
target.SetDataBoundString(0, DisplayIndent(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "PostLevel"))));
}
// *********************************************************************
//
// FinishDataBinding Event Handler
//
/// <summary>
/// This event handler is called when the DataList's DataBound method is
/// called. This event handler needs to add the DataBoundStrings to the
/// DataListItem. Since the ImageButtons break up the DataBinding Literal
/// and DataBound strings, we have two functions: BeginDataBinding and
/// FinishDataBinding. This one, finish, is responsible for the databound
/// content AFTER the imageButtons.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//
// ********************************************************************/
private void FinishDataBinding(Object sender, EventArgs e) {
DataBoundLiteralControl target = (DataBoundLiteralControl) sender;
DataListItem Container = (DataListItem) target.NamingContainer;
// Add the DataBoundStrings
target.SetDataBoundString(0, Convert.ToString(DataBinder.Eval(Container.DataItem, "PostID")));
target.SetDataBoundString(1, DataBinder.Eval(Container.DataItem, "Subject").ToString());
// we have to add other strings if we need to show the username / dateposted
if (ShowUsername) {
// we need to show the username
target.SetDataBoundString(2, Context.Server.UrlEncode(DataBinder.Eval(Container.DataItem, "Username").ToString()));
target.SetDataBoundString(3, DataBinder.Eval(Container.DataItem, "Username").ToString());
// we need to show the username AND the DatePosted
if (ShowDatePosted)
target.SetDataBoundString(4, Users.AdjustForTimezone(Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "PostDate", "{0:g}")), iUserTimezone));
// we need to show JUST the DatePosted
} else if (ShowDatePosted) {
target.SetDataBoundString(2, Users.AdjustForTimezone(Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "PostDate", "{0:g}")), iUserTimezone));
}
}
// *********************************************************************
//
// BeginBuildTemplate
//
/// <summary>
/// This function creates a new DataBoundLiteralControl and populates
/// the Static Strings. Additionally, the DataBinding event handler is
/// wired up to a local event handler. Note that there are two BuildItemTemplate
/// functions: BeginBuildItemTemplate and FinishBuildItemTemplate. This
/// one, begin, is responsible for constructing the code BEFORE the ImageButtons.
/// </summary>
/// <returns></returns>
//
// ********************************************************************/
private Control BeginBuildItemTemplate() {
// Create a new DataBoundLiteralControl with 8 literal items and 7 databound items
dataBoundLiteralControl = new DataBoundLiteralControl(1,1);
// we really don't want to add any static strings, but we need to in order to have the
// databound string we wish to emit to appear. Blah.
dataBoundLiteralControl.SetStaticString(0, "\n");
// wire up the handleDataBinding event handler to the DataBinding event
dataBoundLiteralControl.DataBinding += new System.EventHandler(BeginDataBinding);
// return the DataBoundLiteralControl
return dataBoundLiteralControl;
}
// *********************************************************************
//
// FinishBuildItemTemplate
//
/// <summary>
/// This function creates a new DataBoundLiteralControl and populates
/// the Static Strings. Additionally, the DataBinding event handler is
/// wired up to a local event handler. Note that there are two BuildItemTemplate
/// functions: BeginBuildItemTemplate and FinishBuildItemTemplate. This
/// one, finish, is responsible for constructing the code AFTER the ImageButtons.
/// </summary>
/// <returns></returns>
//
// ********************************************************************/
private Control FinishBuildItemTemplate() {
// Create a new DataBoundLiteralControl with literal items and databound items
dataBoundLiteralControl = new DataBoundLiteralControl(6,5);
dataBoundLiteralControl.SetStaticString(0, "<a class=\"postSubject\" href=\"" + Globals.UrlShowPost);
dataBoundLiteralControl.SetStaticString(1, "\" target=\"" + FrameToOpenPosts + "\">");
// show the username, if needed
if (ShowUsername) {
// we are showing the username
dataBoundLiteralControl.SetStaticString(2, "</a>\n<span class=\"postText\">from </span><a class=\"postUsername\" href=\"" + Globals.UrlUserProfile);
dataBoundLiteralControl.SetStaticString(3, "\">");
if (ShowDatePosted) {
// we are showing the username AND the date posted
dataBoundLiteralControl.SetStaticString(4, "</a><span class=\"postText\">, posted </span><span class=\"postDate\">");
dataBoundLiteralControl.SetStaticString(5, "</span>\n");
} else {
// we are showing just the username
dataBoundLiteralControl.SetStaticString(4, "</a>");
}
} else if (ShowDatePosted) {
// we are showing just the date posted
dataBoundLiteralControl.SetStaticString(2, "</a><span class=\"postText\">, posted </span><span class=\"postDate\">");
dataBoundLiteralControl.SetStaticString(3, "</span>\n");
} else if (!ShowUsername && !this.ShowDatePosted) {
// we are showing neither the username nor the date posted
dataBoundLiteralControl.SetStaticString(2, "</a>\n");
}
// wire up the handleDataBinding event handler to the DataBinding event
dataBoundLiteralControl.DataBinding += new System.EventHandler(FinishDataBinding);
// return the DataBoundLiteralControl
return dataBoundLiteralControl;
}
// *********************************************************************
//
// BuildItemTemplate
//
/// <summary>
/// This function is called to create the template for the datalist.
/// It calls BeginBuildItemTemplate, which creates the DataBoundLiteralControl
/// needed. Next, it adds two ImageButtons (the edit/delete images). Finally,
/// it calls FinishBuildItemTemplate, which builds up the static/dynamic content
/// AFTER the imageButtons.
/// </summary>
/// <param name="_ctrl"></param>
//
// ********************************************************************/
private void BuildItemTemplate(Control _ctrl) {
System.Web.UI.IParserAccessor __parser = ((System.Web.UI.IParserAccessor)(_ctrl));
// build up the first part of the DataList
BeginBuildItemTemplate();
// add the DataBoundLiteralControl to the parser
__parser.AddParsedSubObject(this.dataBoundLiteralControl);
// add an edit and delete icon
ImageButton imgbtnTmp = new ImageButton();
imgbtnTmp.ImageUrl = Globals.ImagePath + this.EditPostIconUrl;
imgbtnTmp.CommandName = "edit";
imgbtnTmp.DataBinding += new EventHandler(HandleImageButtonDataBind);
imgbtnTmp.Command += new CommandEventHandler(PostAction_Click);
imgbtnTmp.ToolTip = "Click to edit the contents of this post.";
__parser.AddParsedSubObject(imgbtnTmp);
imgbtnTmp = new ImageButton();
imgbtnTmp.ImageUrl = Globals.ImagePath + this.DeletePostIconUrl;
imgbtnTmp.CommandName = "delete";
imgbtnTmp.DataBinding += new EventHandler(HandleImageButtonDataBind);
imgbtnTmp.Command += new CommandEventHandler(PostAction_Click);
imgbtnTmp.ToolTip = "Click to delete this post.";
__parser.AddParsedSubObject(imgbtnTmp);
__parser.AddParsedSubObject(new LiteralControl(" "));
// complete our building up the DataList
FinishBuildItemTemplate();
// add the DataBoundLiteralControl to the parser
__parser.AddParsedSubObject(this.dataBoundLiteralControl);
}
// *********************************************************************
//
// BindData
//
/// <summary>
/// This function binds the posts to show to the DataList. Note that only
/// those posts that fall within the first "page" of posts are displayed for
/// the forum. No means to navigate backward or forward is present. This is
/// a bit restrictive, since it limits the administrator to being able to only
/// edit or delete posts in a forum that fall within the default timespan for
/// the forum.
/// </summary>
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -