📄 pollpost.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 : SkinnedWebControl
{
string skinFilename = "Skin-Poll.ascx";
Post post;
PlaceHolder results;
Button voteButton;
PlaceHolder votedFor;
bool enabled = false;
CSContext csContext = CSContext.Current;
DropDownList voteOptions;
PollSummary pollSummary;
int postID;
// *********************************************************************
// PollPost
//
/// <summary>
/// Constructor
/// </summary>
// ***********************************************************************/
public PollPost(ForumPost post) : base()
{
postID = post.PostID;
this.post = post;
pollSummary = Polls.GetPoll(post);
// Assign a default template name
if (SkinFilename == null)
SkinFilename = skinFilename;
}
// *********************************************************************
// Initializeskin
//
/// <summary>
/// Initialize the control template and populate the control with values
/// </summary>
// ***********************************************************************/
override protected void InitializeSkin(Control skin)
{
// Find the results place holder control
//
results = (PlaceHolder) skin.FindControl("Results");
RenderVoteResults();
Label lblTopic = skin.FindControl("Topic") as Label;
if( lblTopic != null ) {
if( pollSummary.Question != String.Empty )
lblTopic.Text = pollSummary.Question;
else
lblTopic.Text = post.Subject;
}
Label lblDescription = skin.FindControl("Description") as Label;
if( lblDescription != null ) {
if( this.pollSummary.Description == String.Empty ) {
lblDescription.Visible = false;
}
else {
lblDescription.Text = pollSummary.Description;
}
}
// Find the vote option control
voteOptions = (DropDownList) skin.FindControl("VoteOptions");
foreach (string key in pollSummary.Answers.Keys) {
voteOptions.Items.Add(new ListItem( ((PollItem) pollSummary.Answers[key]).Answer, key));
}
// Wire up the vote button
//
voteButton = (Button) skin.FindControl("VoteButton");
voteButton.Text = ResourceManager.GetString("PostDisplay_PollPost_buttonText");
voteButton.Click += new EventHandler(VoteButton_Click);
// Can they vote?
if (!enabled) {
Disable();
}
if ((!pollSummary.AllowMultipleVotes) && (pollSummary.HasVoted(csContext.User.UserID))) {
votedFor = (PlaceHolder) skin.FindControl("VotedFor");
votedFor.Controls.Add( new LiteralControl("<p/><b>You voted for: " + pollSummary.GetUserVote( csContext.User.UserID )+"</b>") );
Disable();
}
}
public void Enable() {
voteOptions.Enabled = true;
voteButton.Enabled = true;
}
public void Disable() {
voteOptions.Enabled = false;
voteButton.Enabled = false;
}
protected void RenderVoteResults()
{
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 (string key in pollSummary.Answers.Keys)
sum = sum + ((PollItem) pollSummary.Answers[key]).Total;
// Calculate percentage
//
foreach (string key in pollSummary.Answers.Keys)
{
// 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
//
if (null != pollSummary.Answers[key])
pollValue = ((PollItem) pollSummary.Answers[key]).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( ((PollItem) pollSummary.Answers[key]).Answer) );
progressCell.Controls.Add(new LiteralControl(" "));
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);
}
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 = (PollItem) pollSummary.Answers[voteOptions.SelectedItem.Value];
// Increment our internal poll results
//
poll.Increment();
// Change the display to HasVoted
//
Disable();
RenderVoteResults();
}
// *********************************************************************
// 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;
}
}
public bool Enabled {
get {
return enabled;
}
set {
enabled = 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;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -