ProgressBar类
POLL RESULT CLASS
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace ASPNET.StarterKit.Communities {
/// <summary>
/// Progress bar control is used for multiple purposes to
/// display the progress or status of a particular action
/// for example, it can be used as bars in the voting control
/// </summary>
public class ProgressBar : WebControl, INamingContainer {
// Member variables
//
int progress = 0; // Percentage out of 100
int percentOfProgress = 0; // Percentage in 20ths
// Constructor
//
public ProgressBar() {
// Set up some defaults
//
this.BackColor = System.Drawing.Color.LightGray;
this.ForeColor = System.Drawing.Color.Blue;
}
public override System.Drawing.Color BackColor {
get {
return base.BackColor;
}
set {
base.BackColor = value;
}
}
public override System.Drawing.Color ForeColor {
get {
return base.ForeColor;
}
set {
base.ForeColor = value;
}
}
// Set the percentage of progress
//
public int PercentageOfProgress {
get {
return progress;
}
set {
// Ensure it falls in the correct bounds
//
if (value > 100) // Greater than 100 is still 100
progress = 100;
else if (value < 0) // Less than 0 is stil 0
progress = 0;
progress = value;
if (0 != progress)
percentOfProgress = progress / 5;
}
}
// Render the progress bar
//
protected override void Render(HtmlTextWriter output) {
// We render a table
//
Table _table = new Table();
TableRow row = new TableRow();
_table.CellPadding = 0;
_table.CellSpacing = 0;
_table.BorderWidth = 0;
_table.Width = 80;
// Add a row
//
_table.Rows.Add(row);
// Create 20 cells
for (int i = 0; i < 20; i++ ) {
TableCell td = new TableCell();
td.Width = 5;
td.Height = 16;
// How should we fill this table?
//
if (i < percentOfProgress)
td.BackColor = this.ForeColor;
else
td.BackColor = this.BackColor;
// Add cell to row
//
row.Cells.Add(td);
}
this.Controls.Add(_table);
_table.RenderControl(output);
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace ASPNET.StarterKit.Communities {
/// <summary>
/// Progress bar control is used for multiple purposes to
/// display the progress or status of a particular action
/// for example, it can be used as bars in the voting control
/// </summary>
public class ProgressBar : WebControl, INamingContainer {
// Member variables
//
int progress = 0; // Percentage out of 100
int percentOfProgress = 0; // Percentage in 20ths
// Constructor
//
public ProgressBar() {
// Set up some defaults
//
this.BackColor = System.Drawing.Color.LightGray;
this.ForeColor = System.Drawing.Color.Blue;
}
public override System.Drawing.Color BackColor {
get {
return base.BackColor;
}
set {
base.BackColor = value;
}
}
public override System.Drawing.Color ForeColor {
get {
return base.ForeColor;
}
set {
base.ForeColor = value;
}
}
// Set the percentage of progress
//
public int PercentageOfProgress {
get {
return progress;
}
set {
// Ensure it falls in the correct bounds
//
if (value > 100) // Greater than 100 is still 100
progress = 100;
else if (value < 0) // Less than 0 is stil 0
progress = 0;
progress = value;
if (0 != progress)
percentOfProgress = progress / 5;
}
}
// Render the progress bar
//
protected override void Render(HtmlTextWriter output) {
// We render a table
//
Table _table = new Table();
TableRow row = new TableRow();
_table.CellPadding = 0;
_table.CellSpacing = 0;
_table.BorderWidth = 0;
_table.Width = 80;
// Add a row
//
_table.Rows.Add(row);
// Create 20 cells
for (int i = 0; i < 20; i++ ) {
TableCell td = new TableCell();
td.Width = 5;
td.Height = 16;
// How should we fill this table?
//
if (i < percentOfProgress)
td.BackColor = this.ForeColor;
else
td.BackColor = this.BackColor;
// Add cell to row
//
row.Cells.Add(td);
}
this.Controls.Add(_table);
_table.RenderControl(output);
}
}
}
POLL RESULT CLASS
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ASPNET.StarterKit.Communities.Admin.EditVoting
{
/// <summary>
/// Summary description for PollResults.
/// </summary>
public class PollResults : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel ResultsPanel;
Web Form Designer generated code
System.Drawing.Color barBackColor = System.Drawing.Color.LightGray;
System.Drawing.Color barForeColor = System.Drawing.Color.Blue;
int totalVotes=0;
//*******************************************************
//
// When page loads display the current results
// for a voting poll.
//
//*******************************************************
void Page_Load(Object s, EventArgs e) {
if (Request["Poll_ID"]==null) {
Response.Redirect("Default.aspx");
}
int Poll_ID = Int32.Parse(Request["Poll_ID"]);
PollDetails polldetails = VotingUtility.GetPollForWebBox(Poll_ID);
Table t = new Table();
TableRow questionRow = new TableRow();
TableRow resultsRow = new TableRow();
TableRow footerRow = new TableRow();
TableCell questionCell = new TableCell();
TableCell resultsCell = new TableCell();
TableCell footer = new TableCell();
// Format the table
//
t.BorderWidth = 0;
// Question
//
questionCell.Text = polldetails.Question;
questionCell.Attributes.Add("style","font-weight:bold;");
questionCell.ColumnSpan = 3;
// Add the question cell/row to the table
//
questionRow.Cells.Add(questionCell);
t.Rows.Add(questionRow);
// if (polldetails.DisplayResultsToPublic) {
Hashtable pollresults = VotingUtility.GetPollResults(polldetails.Poll_ID);
// Calculate the sum
//
int sum=0;
foreach (int key in pollresults.Keys)
sum = sum + (int)pollresults[key];
// Calculate percentage
//
foreach (int key in pollresults.Keys) {
// Internal variables/instances
//
double d = 0;
TableRow row = new TableRow();
TableCell progressCell = new TableCell();
TableCell percentageCell = new TableCell();
TableCell choiceCell = new TableCell();
ProgressBar bar = new ProgressBar();
// Percentage for this poll value
//
d = ((double)((int)pollresults[key]) / (double)sum) * 100;
// Display the choice in cell and format
//
choiceCell.Text = ((ChoiceInfo)polldetails.Choices[key]).Choice + ": ";
choiceCell.VerticalAlign = VerticalAlign.Top;
choiceCell.Wrap = false;
// Set properties for each bar
//
bar.PercentageOfProgress = (int)d;
bar.BackColor = barBackColor;
bar.ForeColor = barForeColor;
// Add the bar and set properties of the cell
//
progressCell.Controls.Add(bar);
progressCell.HorizontalAlign = HorizontalAlign.Right;
progressCell.VerticalAlign = VerticalAlign.Top;
progressCell.Width = 100;
// Special case 0
//
if ((double.IsNaN(d)) || (0 == d))
percentageCell.Text = "(0%)";
else
percentageCell.Text = "(" + d.ToString("##.#") + "%) (" + (int)pollresults[key] + " votes)";
// Format percentage cell
//
percentageCell.HorizontalAlign = HorizontalAlign.Left;
percentageCell.VerticalAlign = VerticalAlign.Top;
// Add the cells to the row
//
row.Cells.Add(choiceCell);
row.Cells.Add(progressCell);
row.Cells.Add(percentageCell);
// Add the row to the table
//
t.Rows.Add(row);
// Update total votes
totalVotes+=(int)pollresults[key];
// }
}
// Add results cell/row to table
//
resultsRow.Cells.Add(resultsCell);
t.Rows.Add(resultsRow);
// Display Total Votes
TableRow totalRow = new TableRow();
TableCell totalCell = new TableCell();
totalCell.Text="Total Votes: " + totalVotes;
totalRow.Cells.Add(totalCell);
t.Rows.Add(totalRow);
ResultsPanel.Controls.Add(t);
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ASPNET.StarterKit.Communities.Admin.EditVoting
{
/// <summary>
/// Summary description for PollResults.
/// </summary>
public class PollResults : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel ResultsPanel;
Web Form Designer generated code
System.Drawing.Color barBackColor = System.Drawing.Color.LightGray;
System.Drawing.Color barForeColor = System.Drawing.Color.Blue;
int totalVotes=0;
//*******************************************************
//
// When page loads display the current results
// for a voting poll.
//
//*******************************************************
void Page_Load(Object s, EventArgs e) {
if (Request["Poll_ID"]==null) {
Response.Redirect("Default.aspx");
}
int Poll_ID = Int32.Parse(Request["Poll_ID"]);
PollDetails polldetails = VotingUtility.GetPollForWebBox(Poll_ID);
Table t = new Table();
TableRow questionRow = new TableRow();
TableRow resultsRow = new TableRow();
TableRow footerRow = new TableRow();
TableCell questionCell = new TableCell();
TableCell resultsCell = new TableCell();
TableCell footer = new TableCell();
// Format the table
//
t.BorderWidth = 0;
// Question
//
questionCell.Text = polldetails.Question;
questionCell.Attributes.Add("style","font-weight:bold;");
questionCell.ColumnSpan = 3;
// Add the question cell/row to the table
//
questionRow.Cells.Add(questionCell);
t.Rows.Add(questionRow);
// if (polldetails.DisplayResultsToPublic) {
Hashtable pollresults = VotingUtility.GetPollResults(polldetails.Poll_ID);
// Calculate the sum
//
int sum=0;
foreach (int key in pollresults.Keys)
sum = sum + (int)pollresults[key];
// Calculate percentage
//
foreach (int key in pollresults.Keys) {
// Internal variables/instances
//
double d = 0;
TableRow row = new TableRow();
TableCell progressCell = new TableCell();
TableCell percentageCell = new TableCell();
TableCell choiceCell = new TableCell();
ProgressBar bar = new ProgressBar();
// Percentage for this poll value
//
d = ((double)((int)pollresults[key]) / (double)sum) * 100;
// Display the choice in cell and format
//
choiceCell.Text = ((ChoiceInfo)polldetails.Choices[key]).Choice + ": ";
choiceCell.VerticalAlign = VerticalAlign.Top;
choiceCell.Wrap = false;
// Set properties for each bar
//
bar.PercentageOfProgress = (int)d;
bar.BackColor = barBackColor;
bar.ForeColor = barForeColor;
// Add the bar and set properties of the cell
//
progressCell.Controls.Add(bar);
progressCell.HorizontalAlign = HorizontalAlign.Right;
progressCell.VerticalAlign = VerticalAlign.Top;
progressCell.Width = 100;
// Special case 0
//
if ((double.IsNaN(d)) || (0 == d))
percentageCell.Text = "(0%)";
else
percentageCell.Text = "(" + d.ToString("##.#") + "%) (" + (int)pollresults[key] + " votes)";
// Format percentage cell
//
percentageCell.HorizontalAlign = HorizontalAlign.Left;
percentageCell.VerticalAlign = VerticalAlign.Top;
// Add the cells to the row
//
row.Cells.Add(choiceCell);
row.Cells.Add(progressCell);
row.Cells.Add(percentageCell);
// Add the row to the table
//
t.Rows.Add(row);
// Update total votes
totalVotes+=(int)pollresults[key];
// }
}
// Add results cell/row to table
//
resultsRow.Cells.Add(resultsCell);
t.Rows.Add(resultsRow);
// Display Total Votes
TableRow totalRow = new TableRow();
TableCell totalCell = new TableCell();
totalCell.Text="Total Votes: " + totalVotes;
totalRow.Cells.Add(totalCell);
t.Rows.Add(totalRow);
ResultsPanel.Controls.Add(t);
}
}
}