⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createeditpost.cs

📁 微软的.NET论坛的源代码(COOL!!!)
💻 CS
📖 第 1 页 / 共 3 页
字号:
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;
using System.IO;

namespace AspNetForums.Controls {

    [
    ParseChildren(true)	
    ]

    /// <summary>
    /// This Web control allows the user to create a new post or edit an existing post.
    /// The Mode property determines what action is being taken.  A value of NewPost, the
    /// default, constructs Web controls for creating a new post; a value of ReplyToPost
    /// assumes the person is replying to an existing post; a value of EditPost allows the
    /// user to edit an existing post.
    /// </summary>
    /// <remarks>
    /// When adding a new post, the ForumID must be specified, which indicates what forum the
    /// new post belongs to.  When replying to a post, the PostID property must be specified, indicating
    /// the post that is being replied to.  When editing a post, the PostID property must be
    /// specified, indicating the post to edit.  Failure to specify these required properties
    /// will cause an Exception to be thrown.
    /// </remarks>
    public class CreateEditPost : WebControl, INamingContainer {

        ViewOptions postView = ViewOptions.Threaded;
        DropDownList pinnedPost;
        CheckBox allowNoReplies;
        User user;

        /*************** Property and Class contants ****************/
        // the default messages for posting a new message, replying to a message, and editing a message
        const String _defaultPostNewMessageText = "Post a New Message";
        const String _defaultEditMessageText = "Edit an Existing Message";
        const String _defaultReplyToMessageText = "Reply to an Existing Message";
        const String _defaultPreviewMessageText = "Message Preview";
        readonly String _defaultRedirectUrl = Globals.UrlModeration;
		
        // define constants
        const int _subjectMaxLength = 50;
        /************************************************************/


        /********** DECLARE PRIVATE VARIABLES **************/
        // Create the more advanced, custom styles
        PostStyle _post = new PostStyle();
        PreviewPostSyle _previewPostStyle = new PreviewPostSyle();
        ReplyToPostStyle _replyToPostStyle = new ReplyToPostStyle();
        /***************************************************/

        public CreateEditPost() {

            // 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) {

                // Attempt to get the post id
                if (null != Context.Request.QueryString["PostID"]) {
                    this.PostID = Convert.ToInt32(Context.Request.QueryString["PostID"]);
                    this.Mode = CreateEditPostMode.ReplyToPost;
                } else if (null != Context.Request.Form["PostId"]) {
                    this.PostID = Convert.ToInt32(Context.Request.Form["PostID"]);
                    this.Mode = CreateEditPostMode.ReplyToPost;
                }

                // Attempt to get the forum id
                if (null != Context.Request.QueryString["ForumID"]) {
                    this.ForumID = Convert.ToInt32(Context.Request.QueryString["ForumID"]);
                    this.Mode = CreateEditPostMode.NewPost;
                } else if (null != Context.Request.Form["ForumID"]) {
                    this.ForumID = Convert.ToInt32(Context.Request.Form["ForumID"]);
                    this.Mode = CreateEditPostMode.NewPost;
                }

                // If we don't have either a forum id or a post id we have an error
                if ((this.ForumID < 0) && (this.PostID < 0)) {
                    Page.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.PostDoesNotExist));
                    Page.Response.End();
                }

                // Is a mode specified?
                if (null != Context.Request.QueryString["Mode"]) {
                    string mode = Context.Request.QueryString["Mode"];

                    if (mode == "flat")
                        postView = ViewOptions.Flat;
                    else
                        postView = ViewOptions.Threaded;

                } else if (null != Context.Request.Form["Mode"]) {
                    string mode = Context.Request.Form["Mode"];

                    if (mode == "flat")
                        postView = ViewOptions.Flat;
                    else
                        postView = ViewOptions.Threaded;
                }
            }
        }

        /***********************************************************************
        // CreateChildControls
        //
        /// <summary>
        /// Create the child controls of this control
        /// </summary>
        ***********************************************************************/
        protected override void CreateChildControls() {

            // Create an instance of the user control used to format the display
            Control postForm;

            // Get the current user
            user = Users.GetLoggedOnUser();

            // Attempt to load the control. If this fails, we're done
            try {
                postForm = Page.LoadControl(Globals.ApplicationVRoot + "/skins/" + Globals.Skin + "/Skins/Skin-Post.ascx");
            }
            catch (FileNotFoundException e) {
                throw new Exception("The user control skins/Skins/Skin-Post.ascx was not found. Please ensure this file exists in your skins directory");
            }

            // Set the ID
            postForm.ID = "PostForm";

            // Optionally display reply, post, and preview
            if (Mode != CreateEditPostMode.EditPost) {
                DisplayReply(postForm);
                DisplayPost(postForm);
                DisplayPreview(postForm);
            } else {
                DisplayEdit(postForm);
                DisplayPreview(postForm);
            }

            // All done. Add the control to the control collection
            Controls.Add(postForm);
        }


        private PostDetails GetPostForEdit() {
            PostDetails post = null;

            // Read in information about the post we are replying to
            try {
                post = Posts.GetPostDetails(PostID, Users.GetLoggedOnUser().Username);
            } catch (PostNotFoundException postNotFound) {
                HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.PostDoesNotExist));
                HttpContext.Current.Response.End();
            }

            // If the user attempting to edit is the same user that posted and the user is trusted, or the user is a moderator...
            if (((post.Username.ToLower() == Users.GetLoggedOnUser().Username.ToLower()) && (Users.GetLoggedOnUser().IsTrusted)) || (Users.GetLoggedOnUser().IsModerator)) {
                return post;
            }

            throw new AspNetForums.Components.CannotEditPostException("User cannot edit post.");

        }

        /***********************************************************************
        // DisplayEdit
        //
        /// <summary>
        /// When a user replies to a post, the user control that controls the UI
        /// is loaded and passed to this method. Elements of the form are then wired
        /// up to handle events, such as button clicks
        /// </summary>
        /// <param name="control">Usercontrol used to control UI formatting</param>
        ***********************************************************************/
        private void DisplayEdit(Control controlTemplate) {
            PostDetails post = null;
            Label label;
            TextBox textbox;
            Button button;

            if (Mode != CreateEditPostMode.EditPost)
                return;

            // Get the post to edit
            try {
                post = GetPostForEdit();
            } catch (CannotEditPostException cannotEdit) {
                HttpContext.Current.Response.Redirect(Globals.UrlMessage + Convert.ToInt32(Messages.UnableToEditPost));
                HttpContext.Current.Response.End();
            }

            // Set the visibility
            ((Control) controlTemplate.FindControl("Edit")).Visible = true;
            ((Control) controlTemplate.FindControl("EditNotes")).Visible = true;

            // Set the title
            ((Label) controlTemplate.FindControl("PostTitle")).Text = Globals.HtmlDecode(EditMessageText);

            // Set the editor of the post
            ((Label) controlTemplate.FindControl("PostEditor")).Text = user.Username;

            // Set the Username
            label = (Label) controlTemplate.FindControl("PostAuthor");
            label.Text = post.Username;

            // Set the Subject
            textbox = (TextBox) controlTemplate.FindControl("PostSubject");
            textbox.Text = post.Subject;

            // Set the Body
            textbox = (TextBox) controlTemplate.FindControl("PostBody");
            textbox.Text = post.Body;

            // Find the checkbox
            allowNoReplies = (CheckBox) controlTemplate.FindControl("AllowReplies");

            // Wireup the preview button
            button = (Button) controlTemplate.FindControl("PreviewButton");
            button.Click += new System.EventHandler(PreviewButton_Click);

            // Wire up the cancel button
            button = (Button) controlTemplate.FindControl("Cancel");
            button.Click += new System.EventHandler(CancelButton_Click);

            // Wire up the post button
            button = (Button) controlTemplate.FindControl("PostButton");
            button.Click += new System.EventHandler(PostButton_Click);
        }

        /***********************************************************************
        // DisplayPost
        //
        /// <summary>
        /// When a user replies to a post, the user control that controls the UI
        /// is loaded and passed to this method. Elements of the form are then wired
        /// up to handle events, such as button clicks
        /// </summary>
        /// <param name="control">Usercontrol used to control UI formatting</param>
        ***********************************************************************/
        private void DisplayPost(Control controlTemplate) {
            Button button;

            // Set the title message
            if (Mode == CreateEditPostMode.NewPost)
                ((Label) controlTemplate.FindControl("PostTitle")).Text = _defaultPostNewMessageText;
            else 
                ((Label) controlTemplate.FindControl("PostTitle")).Text = _defaultReplyToMessageText;

            // Set the subject if necessary
            if (Mode == CreateEditPostMode.ReplyToPost) {
                HyperLink hyperlink;

                // Get the subject of the message we're replying to
                hyperlink = (HyperLink) controlTemplate.FindControl("ReplySubject");

                // Do we need to prepend 'Re: '?
                if (hyperlink.Text.StartsWith("Re: "))
                    ((TextBox) controlTemplate.FindControl("PostSubject")).Text = Globals.HtmlDecode(hyperlink.Text);
                else
                    ((TextBox) controlTemplate.FindControl("PostSubject")).Text = "Re: " + Globals.HtmlDecode(hyperlink.Text);
            }

            // Set the Username
            ((Label) controlTemplate.FindControl("PostAuthor")).Text = user.Username;

            // Allow pinned posts?
            if (((user.IsAdministrator) || (Moderate.CanModerate(user.Username))) && (Mode != CreateEditPostMode.ReplyToPost)) {
                controlTemplate.FindControl("AllowPinnedPosts").Visible = true;
                
                pinnedPost = (DropDownList) controlTemplate.FindControl("PinnedPost");
                pinnedPost.Items.Add(new ListItem("Do not pin post", "0"));
                pinnedPost.Items.Add(new ListItem("1 Day", "1"));
                pinnedPost.Items.Add(new ListItem("3 Days", "3"));
                pinnedPost.Items.Add(new ListItem("1 Week", "7"));
                pinnedPost.Items.Add(new ListItem("2 Weeks", "14"));
                pinnedPost.Items.Add(new ListItem("1 Month", "30"));
                pinnedPost.Items.Add(new ListItem("3 Months", "90"));
                pinnedPost.Items.Add(new ListItem("6 Months", "180"));
                pinnedPost.Items.Add(new ListItem("1 Year", "360"));
                pinnedPost.Items.Add(new ListItem("Announcement", "999"));

                // Do an autopost back incase we need to flip the allowNoReplies checkbox
                pinnedPost.AutoPostBack = true;
                pinnedPost.SelectedIndexChanged += new System.EventHandler(PinnedPost_Changed);
            }

            // Find the checkbox
            allowNoReplies = (CheckBox) controlTemplate.FindControl("AllowReplies");

            // Wireup the preview button
            button = (Button) controlTemplate.FindControl("PreviewButton");
            button.Click += new System.EventHandler(PreviewButton_Click);

            // Wire up the cancel button
            button = (Button) controlTemplate.FindControl("Cancel");
            button.Click += new System.EventHandler(CancelButton_Click);

            // Wire up the post button
            button = (Button) controlTemplate.FindControl("PostButton");
            button.Click += new System.EventHandler(PostButton_Click);
        }

        /***********************************************************************
        // PinnedPost_Changed
        //
        /// <summary>
        /// Event raised when the pinned post drop down list changes. If the user
        /// selected announcemnt we need to find the allow replies check box, check it,
        /// and then disable it.
        /// </summary>
        /// <param name="control">Usercontrol used to control UI formatting</param>
        ***********************************************************************/
        private void PinnedPost_Changed(Object sender, EventArgs e) {

            // Do we have an announcement?
            if (Convert.ToInt32(pinnedPost.SelectedItem.Value) == 999) {
                allowNoReplies.Checked = true;
                allowNoReplies.Enabled = false;
            } else {
                allowNoReplies.Checked = false;
                allowNoReplies.Enabled = true;
            }

        }

        /***********************************************************************
        // DisplayPreview
        //
        /// <summary>
        /// Displays a preview of a user's post to a message.
        /// </summary>
        /// <param name="control">Usercontrol used to control UI formatting</param>
        ***********************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -