Hi, I am trying to make some "a la captcha" it must be simple, but not for me. my code is:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReturnFirstCaptchaNum();
ReturnSecondCaptchaNum();
ShowNums();
}
public int ReturnFirstCaptchaNum()
{
int firstnum;
Random r = new Random();
firstnum = r.Next(4);
return firstnum;
}
public int ReturnSecondCaptchaNum()
{
int secondnum;
Random r = new Random();
secondnum = r.Next(5);
return secondnum;
}
public void ShowNums()
{
lblNum01.Text = ReturnFirstCaptchaNum().ToString();
lblNum02.Text = ReturnSecondCaptchaNum().ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
int result = ReturnFirstCaptchaNum() + ReturnSecondCaptchaNum();
if (tbAnswer.Text == result.ToString())
{
HowIsIt.Text = "Right";
tbAnswer.Text = "";
tbAnswer.Enabled = false;
}
else
{
HowIsIt.Text = "Wrong";
tbAnswer.Enabled = true;
}
}
}
and page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblNum01" runat="server" Text="Label"></asp:Label> +
<asp:Label ID="lblNum02" runat="server" Text="Label"></asp:Label> =
<asp:TextBox ID="tbAnswer" runat="server" Width="35px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" />
<br />
<br />
<asp:Label ID="HowIsIt" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblResulted" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
when I input the RIGHT answer-number. It always says me Iam wrong. Looks like when I press button the numbers changing and answer in textbox becomes to be wrong. How to avoid it?