StrawPoll.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. namespace PCSCustomWebControls
  6. {
  7.    public enum pollStyle
  8.    {
  9.       voteonly,
  10.       valuesonly,
  11.       voteandvalues
  12.    }
  13.    [ControlBuilderAttribute(typeof(StrawPollControlBuilder))]
  14.    [ParseChildren(false)]
  15.    public class StrawPoll : System.Web.UI.WebControls.WebControl,
  16.       INamingContainer
  17.    {
  18.       public event CandidateEventHandler Voted;
  19.       protected void OnVoted(CandidateEventArgs e)
  20.       {
  21.          if (Voted != null)
  22.          {
  23.             Voted(this, e);
  24.          }
  25.       }
  26.       internal void ChildVote(CandidateEventArgs e)
  27.       {
  28.          OnVoted(e);
  29.       }
  30.       public StrawPoll()
  31.       {
  32.          //
  33.          // TODO: Add constructor logic here
  34.          //
  35.       }
  36.       public string Title
  37.       {
  38.          get
  39.          {
  40.             object rawTitle = ViewState["_title"];
  41.             if (rawTitle != null)
  42.             {
  43.                return (string)rawTitle;
  44.             }
  45.             else
  46.             {
  47.                ViewState["_title"] = "Straw Poll";
  48.                return "Straw Poll";
  49.             }
  50.          }
  51.          set
  52.          {
  53.             ViewState["_title"] = value;
  54.          }
  55.       }
  56.       public pollStyle PollStyle
  57.       {
  58.          get
  59.          {
  60.             object rawPollStyle = ViewState["_pollStyle"];
  61.             if (rawPollStyle != null)
  62.             {
  63.                return (pollStyle)rawPollStyle;
  64.             }
  65.             else
  66.             {
  67.                ViewState["_pollStyle"] = pollStyle.voteandvalues;
  68.                return pollStyle.voteandvalues;
  69.             }
  70.          }
  71.          set
  72.          {
  73.             ViewState["_pollStyle"] = value;
  74.          }
  75.       }
  76.       protected override void Render(HtmlTextWriter output)
  77.       {
  78.          Candidate currentCandidate;
  79.          long iTotalVotes = 0;
  80.          long iPercentage = 0;
  81.          int iColumns = 2;
  82.          // Start table, display title
  83.          if (PollStyle == pollStyle.voteandvalues)
  84.          {
  85.             iColumns = 3;
  86.          }
  87.          output.Write("<TABLE border='1' bordercolor='black'" 
  88.             + " bgcolor='#DDDDBB'"
  89.             + " width='90%' cellpadding='1' cellspacing='1'"
  90.             + " align='center'>");
  91.          output.Write("<TR><TD colspan='" + iColumns 
  92.             + "' align='center'"
  93.             + " bgcolor='#FFFFDD'>");
  94.          output.Write("<B>" + Title + "</B></TD></TR>");
  95.          if (Controls.Count == 0)
  96.          {
  97.             // Default text when no options contained
  98.             output.Write("<TR><TD bgcolor='#FFFFDD'>No options to"
  99.                + " display.</TR></TD>");
  100.          }
  101.          else
  102.          {
  103.             // Get total votes
  104.             for (int iLoop = 0; iLoop < Controls.Count; iLoop++)
  105.             {
  106.                // Get option
  107.                currentCandidate = (Candidate)Controls[iLoop];
  108.                // Sum votes cast
  109.                iTotalVotes += currentCandidate.Votes;
  110.             }
  111.             // Render each option
  112.             for (int iLoop = 0; iLoop < Controls.Count; iLoop++)
  113.             {
  114.                // Get option
  115.                currentCandidate = (Candidate)Controls[iLoop];
  116.                // Place option name in first column
  117.                output.Write("<TR><TD bgcolor='#FFFFDD' width='15%'> "
  118.                   + currentCandidate.Name + " </TD>");
  119.                // Add voting option to second column if required
  120.                if (PollStyle != pollStyle.valuesonly)
  121.                {
  122.                   output.Write("<TD width='1%' bgcolor='#FFFFDD'>"
  123.                      + "<FONT color='#FFFFDD'>.</FONT>");
  124.                   currentCandidate.RenderControl(output);
  125.                   output.Write("<FONT color='#FFFFDD'>.</FONT></TD>");
  126.                }
  127.                // Place graph, value, and percentage in third
  128.                // column if required
  129.                if (PollStyle != pollStyle.voteonly)
  130.                {
  131.                   if (iTotalVotes > 0)
  132.                   {
  133.                      iPercentage = (currentCandidate.Votes * 100) /
  134.                         iTotalVotes;
  135.                   }
  136.                   else
  137.                   {
  138.                      iPercentage = 0;
  139.                   }
  140.                   output.Write("<TD bgcolor='#FFFFDD'>"
  141.                      + "<TABLE width='100%'>"
  142.                      + "<TR><TD><TABLE border='1' bordercolor='black'"
  143.                      + " width='100%' cellpadding='0'"
  144.                      + " cellspacing='0'>");
  145.                   output.Write("<TR><TD bgcolor='red' width='"
  146.                      + iPercentage
  147.                      + "%'><FONT color='red'>.</FONT></TD>");
  148.                   output.Write("<TD bgcolor='white' width='"
  149.                      + (100-iPercentage)
  150.                      + "%'><FONT color='white'>."
  151.                      + "</FONT></TD></TR></TABLE></TD>");
  152.                   output.Write("<TD width='75'>"
  153.                      + currentCandidate.Votes + " ("
  154.                      + iPercentage
  155.                      + "%)</TD></TR></TABLE></TD>");
  156.                }
  157.                // End row
  158.                output.Write("</TR>");
  159.             }
  160.             // Show total votes cast if values displayed
  161.             if (PollStyle != pollStyle.voteonly)
  162.             {
  163.                output.Write("<TR><TD bgcolor='#FFFFDD' colspan='"
  164.                   + iColumns
  165.                   + "'>Total votes cast: " + iTotalVotes
  166.                   + "</TD></TR>");
  167.             }
  168.          }
  169.          // Finish table
  170.          output.Write("</TABLE>");
  171.       }
  172.    }
  173. }