帶運算的驗證碼

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace WebApplication3
  12. {
  13.     /// <summary>
  14.     /// WebForm1 的摘要說明。
  15.     /// </summary>
  16.     public class WebFormYanZhengMa : System.Web.UI.Page
  17.     {
  18.         protected System.Web.UI.WebControls.TextBox TextBox1;
  19.         protected System.Web.UI.WebControls.Label Label1;
  20.         protected System.Web.UI.WebControls.LinkButton LinkButton1;
  21.         protected System.Web.UI.WebControls.Button Button1;
  22.         #region Web 窗體設計器生成的代碼
  23.         override protected void OnInit(EventArgs e)
  24.         {
  25.             //
  26.             // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
  27.             //
  28.             InitializeComponent();
  29.             base.OnInit(e);
  30.         }
  31.         
  32.         /// <summary>
  33.         /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  34.         /// 此方法的內容。
  35.         /// </summary>
  36.         private void InitializeComponent()
  37.         {    
  38.             this.Button1.Click += new System.EventHandler(this.Button1_Click);
  39.             this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
  40.             this.Load += new System.EventHandler(this.Page_Load);
  41.         }
  42.         #endregion
  43.         #region 無運算驗證
  44. //      private void Page_Load(object sender, System.EventArgs e)
  45. //      {
  46. //          if(!IsPostBack)
  47. //          {
  48. //              string ddd = readom(5);
  49. //              Session["readom"]=ddd;
  50. //              this.Label1.Text = ddd;
  51. //          }
  52. //      }
  53. //      private void Button1_Click(object sender, System.EventArgs e)
  54. //      {
  55. //          if(this.TextBox1.Text==Session["readom"].ToString())
  56. //          {
  57. //              Response.Write("<script>alert('Ok')</script>");
  58. //          }
  59. //          else
  60. //          {
  61. //              Response.Write("<script>alert('Error')</script>");
  62. //          }
  63. //      }
  64. //      public string readom(int nums)
  65. //      {
  66. //          string readom="";
  67. //          //string ddd = "a,r,e,t,2,5,7,8,9,k,r,h,y,G,E,Y,T,E,W,d,2,3,5,0,1,6,j,m";
  68. //          string ddd = "1,2,3,4,5,6,7,8,9,0";
  69. //          string [] arr = ddd.Split(',');
  70. //          Random ran = new Random();
  71. //          
  72. //          for(int i = 0 ; i < nums;i++ )
  73. //          {
  74. //              int a = ran.Next(arr.Length);
  75. //              readom += arr[a].ToString();
  76. //          }
  77. //          return readom;
  78. //      }
  79. //      private void LinkButton1_Click(object sender, System.EventArgs e)
  80. //      {
  81. //          string ddd = readom(5);
  82. //          Session["readom"]=ddd;
  83. //          this.Label1.Text = ddd;
  84. //      } 
  85.         #endregion
  86.         //加載
  87.         private void Page_Load(object sender, System.EventArgs e)
  88.         {
  89.             if(!IsPostBack)
  90.             {
  91.                 yunsuan();
  92.             }
  93.         }   
  94.         //驗證
  95.         private void Button1_Click(object sender, System.EventArgs e)
  96.         {
  97.             if(this.TextBox1.Text==Session["readom"].ToString())
  98.             {
  99.                 Response.Write("<script>alert('Ok')</script>");
  100.             }
  101.             else
  102.             {
  103.                 Response.Write("<script>alert('Error')</script>");
  104.             }
  105.         }
  106.         //換圖
  107.         private void LinkButton1_Click(object sender, System.EventArgs e)
  108.         {
  109.             yunsuan();
  110.         } 
  111.         //生成隨機數
  112.         public string readom(int nums)
  113.         {
  114.             string readom="";
  115.             string ddd = "1,2,3,4,5,6,7,8,9";
  116.             string [] arr = ddd.Split(',');
  117.             Random ran = new Random();
  118.             
  119.             for(int i = 0 ; i < nums;i++ )
  120.             {
  121.                 int a = ran.Next(arr.Length);
  122.                 readom +=arr[a].ToString();
  123.             }
  124.             return readom;
  125.         }
  126.         //運算
  127.         public void yunsuan()
  128.         {
  129.             string ddd = readom(3);
  130.             int a = int.Parse(ddd.Substring(0,2).ToString());
  131.             int b = int.Parse(ddd.Substring(2,1).ToString());
  132.             string ys = "+,-,*,/";
  133.             string [] yu = ys.Split(',');
  134.             Random dom = new Random();
  135.             int yy =  dom.Next(yu.Length);
  136.             string yusuan = yu[yy].ToString();
  137.             int c=0;
  138.             switch (yusuan)
  139.             {
  140.                 case "+":
  141.                     c = a + b;
  142.                     Session["readom"]=c;
  143.                     this.Label1.Text = ""+a+"加上"+b+"=?";
  144.                     break;
  145.                 case "-":
  146.                     c = a - b;
  147.                     Session["readom"]=c;
  148.                     this.Label1.Text = ""+a+"減去"+b+"=?";
  149.                     break;
  150.                 case "*":
  151.                     c = a * b;
  152.                     Session["readom"]=c;
  153.                     this.Label1.Text = ""+a+"乘以"+b+"=?";
  154.                     break;
  155.                 case "/":
  156.                     c = a / b;
  157.                     Session["readom"]=c;
  158.                     this.Label1.Text = ""+a+"除以"+b+"=?";
  159.                     break;
  160.             }
  161.         }
  162.     }
  163. }
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章