- 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 WebApplication3
- {
- /// <summary>
- /// WebForm1 的摘要說明。
- /// </summary>
- public class WebFormYanZhengMa : System.Web.UI.Page
- {
- protected System.Web.UI.WebControls.TextBox TextBox1;
- protected System.Web.UI.WebControls.Label Label1;
- protected System.Web.UI.WebControls.LinkButton LinkButton1;
- protected System.Web.UI.WebControls.Button Button1;
- #region Web 窗體設計器生成的代碼
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
- /// 此方法的內容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Button1.Click += new System.EventHandler(this.Button1_Click);
- this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- #region 無運算驗證
- // private void Page_Load(object sender, System.EventArgs e)
- // {
- // if(!IsPostBack)
- // {
- // string ddd = readom(5);
- // Session["readom"]=ddd;
- // this.Label1.Text = ddd;
- // }
- // }
- // private void Button1_Click(object sender, System.EventArgs e)
- // {
- // if(this.TextBox1.Text==Session["readom"].ToString())
- // {
- // Response.Write("<script>alert('Ok')</script>");
- // }
- // else
- // {
- // Response.Write("<script>alert('Error')</script>");
- // }
- // }
- // public string readom(int nums)
- // {
- // string readom="";
- // //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";
- // string ddd = "1,2,3,4,5,6,7,8,9,0";
- // string [] arr = ddd.Split(',');
- // Random ran = new Random();
- //
- // for(int i = 0 ; i < nums;i++ )
- // {
- // int a = ran.Next(arr.Length);
- // readom += arr[a].ToString();
- // }
- // return readom;
- // }
- // private void LinkButton1_Click(object sender, System.EventArgs e)
- // {
- // string ddd = readom(5);
- // Session["readom"]=ddd;
- // this.Label1.Text = ddd;
- // }
- #endregion
- //加載
- private void Page_Load(object sender, System.EventArgs e)
- {
- if(!IsPostBack)
- {
- yunsuan();
- }
- }
- //驗證
- private void Button1_Click(object sender, System.EventArgs e)
- {
- if(this.TextBox1.Text==Session["readom"].ToString())
- {
- Response.Write("<script>alert('Ok')</script>");
- }
- else
- {
- Response.Write("<script>alert('Error')</script>");
- }
- }
- //換圖
- private void LinkButton1_Click(object sender, System.EventArgs e)
- {
- yunsuan();
- }
- //生成隨機數
- public string readom(int nums)
- {
- string readom="";
- string ddd = "1,2,3,4,5,6,7,8,9";
- string [] arr = ddd.Split(',');
- Random ran = new Random();
- for(int i = 0 ; i < nums;i++ )
- {
- int a = ran.Next(arr.Length);
- readom +=arr[a].ToString();
- }
- return readom;
- }
- //運算
- public void yunsuan()
- {
- string ddd = readom(3);
- int a = int.Parse(ddd.Substring(0,2).ToString());
- int b = int.Parse(ddd.Substring(2,1).ToString());
- string ys = "+,-,*,/";
- string [] yu = ys.Split(',');
- Random dom = new Random();
- int yy = dom.Next(yu.Length);
- string yusuan = yu[yy].ToString();
- int c=0;
- switch (yusuan)
- {
- case "+":
- c = a + b;
- Session["readom"]=c;
- this.Label1.Text = ""+a+"加上"+b+"=?";
- break;
- case "-":
- c = a - b;
- Session["readom"]=c;
- this.Label1.Text = ""+a+"減去"+b+"=?";
- break;
- case "*":
- c = a * b;
- Session["readom"]=c;
- this.Label1.Text = ""+a+"乘以"+b+"=?";
- break;
- case "/":
- c = a / b;
- Session["readom"]=c;
- this.Label1.Text = ""+a+"除以"+b+"=?";
- break;
- }
- }
- }
- }
帶運算的驗證碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.