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

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 class Candidate : System.Web.UI.WebControls.WebControl,
  8.       INamingContainer
  9.    {
  10.       public string Name
  11.       {
  12.          get
  13.          {
  14.             object rawName = ViewState["_name"];
  15.             if (rawName != null)
  16.             {
  17.                return (string)rawName;
  18.             }
  19.             else
  20.             {
  21.                ViewState["_name"] = "Candidate";
  22.                return "Candidate";
  23.             }
  24.          }
  25.          set
  26.          {
  27.             ViewState["_name"] = value;
  28.          }
  29.       }
  30.       public long Votes
  31.       {
  32.          get
  33.          {
  34.             object rawVotes = ViewState["_votes"];
  35.             if (rawVotes != null)
  36.             {
  37.                return (long)rawVotes;
  38.             }
  39.             else
  40.             {
  41.                ViewState["_votes"] = (long)0;
  42.                return 0;
  43.             }   
  44.          }
  45.          set
  46.          {
  47.             ViewState["_votes"] = value;
  48.          }
  49.       }
  50.       public void Increment()
  51.       {
  52.          Votes += 1;
  53.       }
  54.       public void Reset()
  55.       {
  56.          Votes = 0;
  57.       }
  58.       protected override void CreateChildControls()
  59.       {
  60.          Button btnVote = new Button();
  61.          btnVote.Text = "Vote";
  62.          btnVote.Click += new System.EventHandler(btnVote_Click);
  63.          Controls.Add(btnVote);
  64.          base.CreateChildControls();
  65.       }
  66.       protected void btnVote_Click(object sender, System.EventArgs e)
  67.       {
  68.          Increment();
  69.          StrawPoll parent = (StrawPoll)Parent;
  70.          if (parent.PollStyle == pollStyle.voteonly)
  71.          {
  72.             parent.PollStyle = pollStyle.valuesonly;
  73.          }
  74.          CandidateEventArgs eCandidate = new CandidateEventArgs(this);
  75.          parent.ChildVote(eCandidate);
  76.       }
  77.    }
  78. }