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

📄 createeditpoll.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Configuration;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;


namespace CommunityServer.Discussions.Controls 
{

    /// <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 CreateEditPoll : TemplatedWebControl
    {

        #region Member Variables
        CSContext csContext   = CSContext.Current;
        string skinFilename	= "View-CreateEditPoll.ascx";
//        Editor postBody = null;
        
        Forum forum;
  //      ForumPost postReplyingTo;
//        DropDownList pinnedPost;
//		ForumRolloverImageButton allowReply;
		
  //      CheckBox isLocked;
        User user;
	//	TextBox forumsPostedTo;
	//	System.Collections.Specialized.StringCollection pollOptions;
		
		ForumRolloverImageButton postButton;
		//Repeater rPollData;
		DataList dlPollData;
		LinkButton btnAdd;
		TextBox txtTopic;
		TextBox txtOptionAdd;
		TextBox txtDescription;
		//CreateEditPostMode PostMode;

		public string Question {
			get {
				string s = ViewState["pollQuestion"] as string;
				if( s == null ) {
					s = "";
				}
				return s;
			}
			set {
				ViewState["pollQuestion"] = value;
			}
		}

		public string Description {
			get{
				string s = ViewState["pollDescription"] as string;
				if( s == null ) {
					s = "";
				}
				return s;
			}
			set{
				ViewState["pollQuestion"] = value;
			}
		}

		public StringCollection Data {
			get{ 
				StringCollection d = ViewState["pollOptions"] as StringCollection;
				if( d == null ) {
					d = new StringCollection();
				}

				return d;
			}
			set{ 
				ViewState["pollOptions"] = value;
			}
		}

        #endregion

        #region CreateEditPost Constructor
        /// <remarks>
        /// Class contructor used to determine the mode of the server control.
        /// </remarks>
        public CreateEditPoll() 
        {
            // Assign a default template name
			this.ExternalSkinFileName = skinFilename;

            // Get the current user
            //
            user = CSContext.Current.User;

            // Is the user anonymous?
            //
            if (!Context.Request.IsAuthenticated)
                user = Users.GetAnonymousUser();

            // Do we have a ForumID?
            //
            if (csContext.ForumID > 0) 
            {
                forum = Forums.GetForum(csContext.ForumID, false, true, CSContext.Current.User.UserID);
            }

            // Security check to see if the forum allows anonymous posts
            //
            if ( (user.IsAnonymous) && (!forum.EnableAnonymousPosting) && (!CSContext.Current.SiteSettings.EnableAnonymousUserPosting) ) 
            {
                if (!csContext.Context.Request.IsAuthenticated) 
                {
                    csContext.Context.Response.Redirect(Globals.GetSiteUrls().Login);
                    csContext.Context.Response.End();
                }
            }

            // If we don't have either a forum id or a post id we have an error
            //
            if (csContext.ForumID < 0)
            {
                throw new CSException(CSExceptionType.PostNotFound);
            }

        }
        #endregion

        #region InitializeSkin and other render fuctions.

		protected override void OnLoad(EventArgs e) {

			if ( !Page.IsPostBack ) {
				this.DataBind();
			}
			base.OnLoad (e);
		}

		protected override void AttachChildControls() {
			txtTopic = FindControl("txtTopic") as TextBox;
			txtDescription = FindControl("txtDescription") as TextBox;
			txtOptionAdd = FindControl("txtOptionAdd") as TextBox;


			postButton = this.FindControl("PostButton") as ForumRolloverImageButton;
			postButton.Click +=new EventHandler(postButton_Click);

			btnAdd = FindControl("btnAdd") as LinkButton;
			btnAdd.Click +=new EventHandler(btnAdd_Click);

			dlPollData = this.FindControl("dlPollData") as DataList;
			dlPollData.EditCommand +=new DataListCommandEventHandler(dlPollData_EditCommand);
			dlPollData.CancelCommand +=new DataListCommandEventHandler(dlPollData_CancelCommand);
			dlPollData.UpdateCommand +=new DataListCommandEventHandler(dlPollData_UpdateCommand);
			dlPollData.DeleteCommand +=new DataListCommandEventHandler(dlPollData_DeleteCommand);
		}
		
		public override void DataBind() {
			base.DataBind();

			BindData();
		}

		private void BindData() {
			dlPollData.DataSource = this.Data;
			dlPollData.DataBind();
		}

		private void postButton_Click(object sender, EventArgs e) {
			Thread thread = new Thread();

			thread.Section = this.forum;
			thread.SectionID = this.forum.SectionID;
			thread.Subject = String.Format( ResourceManager.GetString( "CreateEditPoll_PollFormat" ), txtTopic.Text ); // 
			thread.User = this.user;

			StringCollection collection = this.Data as StringCollection;
			string[] array = new string[ collection.Count ] ;

			collection.CopyTo( array, 0 );

			thread.Body = PollSummary.SavePoll( this.txtTopic.Text, array, txtDescription.Text );
			thread.IsSticky = false;
			thread.PostType = PostType.Poll;

			ForumPost newPost = Posts.AddPost( thread );

			// Redirect the user
			//
			if (newPost.IsApproved) 
			{

				switch (PostMode) 
				{
					case CreateEditPostMode.NewPost:
						Context.Response.Redirect( Globals.GetSiteUrls().PostInPage(newPost.PostID, newPost.PostID), true );
						break;

					case CreateEditPostMode.NewPrivateMessage:
						Context.Response.Redirect( Globals.GetSiteUrls().Post(newPost.PostID), true);
						break;

					default:
						Context.Response.Redirect( Globals.GetSiteUrls().PostInPage(newPost.PostID, newPost.PostID), true);
						break;
				}
			} 
			else 
			{
				//throw new CSException(CSExceptionType.PostPendingModeration);
				Context.Response.Redirect( ForumUrls.Instance().PostPendingModeration(newPost.Forum.SectionID) );
			}  
		}

		#endregion

		private void dlPollData_EditCommand(object source, DataListCommandEventArgs e) {
			dlPollData.EditItemIndex = e.Item.ItemIndex;
			this.BindData();
		}

		private void dlPollData_CancelCommand(object source, DataListCommandEventArgs e) {
			dlPollData.EditItemIndex = -1;
			this.BindData();
		}

		private void dlPollData_UpdateCommand(object source, DataListCommandEventArgs e) {
			TextBox txtOption = e.Item.FindControl("txtOption") as TextBox;

			StringCollection collection = this.Data as StringCollection;
			collection[e.Item.ItemIndex] = txtOption.Text;

			this.Data = collection;

			dlPollData.EditItemIndex = -1;
			this.BindData();
		}

		private void dlPollData_DeleteCommand(object source, DataListCommandEventArgs e) {
			StringCollection collection = this.Data as StringCollection;
			collection.RemoveAt( e.Item.ItemIndex );

			this.Data = collection;

			dlPollData.EditItemIndex = -1;
			this.BindData();
		}

		private void btnAdd_Click(object sender, EventArgs e) {
			StringCollection collection = this.Data as StringCollection;
			collection.Add( this.txtOptionAdd.Text );
			txtOptionAdd.Text = "";

			this.Data = collection;

			dlPollData.SelectedIndex = -1;
			this.BindData();
		}

		#region Properies
		/// <value>
		/// Indicates whether the post being made is a new post, a reply to an existing post, or an
		/// editing of an existing post.  The allowable values are NewPost, ReplyToPost, and EditPost.
		/// </value>
		/// <remarks>When setting the Mode property to NewPost, you must supply a ForumID property.
		/// When setting the Mode to ReplyToPost or EditPost, you must supply a PostID property.</remarks>
		public CreateEditPostMode PostMode 
		{
			get 
			{
				Object state = ViewState[ "mode" ];
				if ( state != null ) 
				{
					return (CreateEditPostMode) state;
				}
				return CreateEditPostMode.NewPost;
			}
			set { ViewState["mode"] = value; }
		}
		#endregion
	}
}

⌨️ 快捷键说明

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