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

📄 pollpost.cs

📁 community server 源码
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls.PostDisplay 
{
    /// <summary>
    /// The Vote control is allows for users to vote on topics. The results are stored in 
    /// a SQL Server table and a HTML graph is used to display the selections/percentages.
    /// </summary>
    [
    ToolboxData("<{0}:Vote runat=\"server\" />")
    ]
    public class PollPost : TemplatedWebControl
    {
        #region Child Controls

        //string skinFilename = "Skin-Poll.ascx";
        PlaceHolder results;
        Button voteButton;
        PlaceHolder votedFor;
        DropDownList voteOptions;
        PollSummary pollSummary;
        Label lblTopic;
        Label lblDescription;

        CSContext csContext = CSContext.Current;
        int postID;
        Post post;
        bool enabled = false;

        #endregion

        public PollPost(ForumPost post) : base() 
        {
            this.post = post;
            postID = post.PostID;

            pollSummary = Polls.GetPoll(post);
        }

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Skin-Poll.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            //if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            RenderVoteResults();

            if (lblTopic != null) 
            {
                if (pollSummary.Question != String.Empty)
                    lblTopic.Text = pollSummary.Question;
                else
                    lblTopic.Text = post.Subject;
            }

            if (lblDescription != null) 
            {
                if (this.pollSummary.Description == String.Empty) 
                    lblDescription.Visible = false;
                else 
                    lblDescription.Text = pollSummary.Description;
            }
            
            if (voteOptions != null)
            {
                foreach (PollItem answer in pollSummary.Answers) 
                {
                    voteOptions.Items.Add( new ListItem( answer.Answer, answer.AnswerID ) );
                }
            }
            
            if (voteButton != null)
                voteButton.Text = ResourceManager.GetString("PostDisplay_PollPost_buttonText");

            // Can they vote?
            //
            if (!this.Enabled) 
                Disable();

            if (pollSummary.HasVoted( csContext.User.UserID )) 
            {
                // Display "<p/><b>You voted for: {0}.</b>" 
                //
                votedFor.Controls.Add( new LiteralControl( string.Format( ResourceManager.GetString( "PostDisplay_PollPost_userChoiceText" ), pollSummary.GetUserVote( csContext.User.UserID ) ) ) );
                Disable();
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            results = FindControl("Results") as PlaceHolder;
            lblTopic = FindControl("Topic") as Label;
            lblDescription = FindControl("Description") as Label;
            voteOptions = FindControl("VoteOptions") as DropDownList;
            voteButton = FindControl("VoteButton") as Button;
            votedFor = FindControl("VotedFor") as PlaceHolder;
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            if (voteButton != null)
                voteButton.Click += new EventHandler(VoteButton_Click);		
        }
		        
        #endregion

        #region Properties

        public override bool Enabled 
        {
            get { return enabled; }
            set { enabled = value; }
        }

        // *********************************************************************
        //  ButtonText
        //
        /// <summary>
        /// Used to control the text on the button. Default is 'Vote'
        /// </summary>
        // ***********************************************************************/        
        public virtual String ButtonText 
        {
            get 
            {
                Object state = ViewState["ButtonText"];
                if ( state != null ) 
                {
                    return (String)state;
                }
                return ResourceManager.GetString("PostDisplay_PollPost_buttonText");
            }
            set 
            { 
                ViewState["ButtonText"] = value;
            }
        }

        // *********************************************************************
        //  BarBackColor
        //
        /// <summary>
        /// Controls the back ground color of the rendered bar(s).
        /// </summary>
        // ***********************************************************************/        
        [
        System.ComponentModel.Category( "Style" ),
        ]
        public virtual Color BarBackColor 
        {
            get 
            {
                Object state = ViewState["BarBackColor"];
                if ( state != null ) 
                {
                    return (Color)state;
                }
                return Color.Empty;
            }
            set 
            {
                ViewState["BarBackColor"] = value;
            }
        }

        // *********************************************************************
        //  BarForeColor
        //
        /// <summary>
        /// Controls the foreground color of the rendered bar(s).
        /// </summary>
        // ***********************************************************************/        
        [
        System.ComponentModel.Category( "Style" ),
        ]
        public virtual Color BarForeColor 
        {
            get 
            {
                Object state = ViewState["BarForeColor"];
                if ( state != null ) 
                {
                    return (Color)state;
                }
                return Color.Empty;
            }
            set 
            {
                ViewState["BarForeColor"] = value;
            }
        }

        #endregion

        #region Command Handlers

        void VoteButton_Click(Object sender, EventArgs e) 
        {
            // shouldn't get here
            if (pollSummary.HasVoted( csContext.User.UserID ))
                return;

            Polls.Vote( postID, csContext.User.UserID, voteOptions.SelectedItem.Value );
            PollItem poll = pollSummary.GetPollItemByAnswerID(voteOptions.SelectedItem.Value);

            // Increment our internal poll results
            //
            poll.Increment();

            // Change the display to HasVoted
            //
            Disable();

            RenderVoteResults();
        }


        #endregion

        #region Command Implementations

        protected void RenderVoteResults() 
        {
            if (results == null)
                return;

            results.Controls.Clear();

            // Member Variables
            //
            int sum = 0;

            Table t = new Table();
            t.ID = "ResultsTable";
            t.CellPadding = 3;
            t.CellSpacing = 1;
            TableRow resultsRow = new TableRow();
            TableRow footerRow = new TableRow();
            TableCell resultsCell = new TableCell();
            TableCell footer = new TableCell();

            // Format the table
            //
            t.BorderWidth = 0;

            // Calculate the sum
            //
            foreach (PollItem answer in pollSummary.Answers)
                sum = sum + answer.Total;

            // Calculate percentage
            //
            foreach (PollItem answer in pollSummary.Answers) 
            {
                // Internal variables/instances
                //
                double d = 0;
                int pollValue = 0;
                TableRow row = new TableRow();
                TableCell progressCell = new TableCell();
                TableCell percentageCell = new TableCell();
                ProgressBar bar = new ProgressBar();
				
                // Get the poll value
                //
                pollValue = answer.Total;

                // Percentage for this poll value
                //
                d = ((double)pollValue / (double)sum) * 100;

                // Set properties for each bar
                //
                bar.PercentageOfProgress = (int)d;

                // Special case 0
                //
                Label percentageText = new Label();
                percentageText.CssClass = "normalTextSmallBold";
                if (double.IsNaN(d) || 0 == d)
                    percentageText.Text = "(0%)<br>";
                else
                    percentageText.Text = "(" + d.ToString("##.#") + "%)<br>";

                // Add the bar and set properties of the cell
                //
                progressCell.CssClass = "txt3";
                progressCell.Controls.Add( new LiteralControl( answer.Answer ) );
                progressCell.Controls.Add( new LiteralControl(" &nbsp; ") );
                progressCell.Controls.Add( percentageText );
                progressCell.Controls.Add( bar );
                progressCell.HorizontalAlign = HorizontalAlign.Left;
                progressCell.VerticalAlign = VerticalAlign.Top;
                progressCell.Wrap = false;

                // Add the cells to the row
                //
                row.Cells.Add(progressCell);

                // Add the row to the table
                //
                t.Rows.Add(row);
            }

            // What you voted for
            //
            resultsCell.ColumnSpan = 3;
            resultsCell.CssClass = "txt3";
            resultsCell.HorizontalAlign = HorizontalAlign.Right;

            // Set footer properties
            //
            Label totalVotes = new Label();
            totalVotes.Text = ResourceManager.GetString("PostDisplay_PollPost_totalVotes") + "<b>" + sum + "</b>";
            totalVotes.CssClass = "txt3";
            footer.Controls.Add(totalVotes);
            footer.ColumnSpan = 3;
            footer.HorizontalAlign = HorizontalAlign.Left;

            // Add footer cell and add to table
            //
            footerRow.Cells.Add(footer);
            t.Rows.Add(footerRow);

            results.Controls.Add(t);
        }

        #endregion

        #region Helpers

        public void Enable() 
        {
            voteOptions.Enabled = true;
            voteButton.Enabled = true;
        }

        public void Disable() 
        {
            voteOptions.Enabled = false;
            voteButton.Enabled = false;
        }

        #endregion
    }
}

⌨️ 快捷键说明

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