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>
        &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Button" />
        <br />
        <br />
        <asp:Label ID="HowIsIt" runat="server" Text="Label"></asp:Label>
    &nbsp;&nbsp;&nbsp;
        <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?

Recommended Answers

All 8 Replies

Hi,

Since you are creating the random number generation in the page_load event, it will be fired while pressing button_click also.

1) page_load event will generate random number
2) you are giving value and press button to see the result.
so again page_load (since server control) will fire and the random number is generated again which will not be equal to your input value.

So move the random number generation code from page_load event.

Thank you

Thank you MeSamPath for your answer.

Well, I've removed from Page_Load the randon number generating methods.

protected void Page_Load(object sender, EventArgs e)
    {
        //ReturnFirstCaptchaNum();
        //ReturnSecondCaptchaNum();
        ShowNums();
    }

this way. Now, as you see, I have only one which printing the numbers, but I still can't to get this program work correctly.

On button click method only compares the nums

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;
        }
    }

Hardly he can generate some new values. Where the problem still can be and how to get rid of it?

Looks like the problem is that on every click page reloads and generates new values... perhaps this.

How to deny page reload?

Hi,

You may give a try with using "IsPostBack" option.

for example,

if (!isPostBack)
{
ShowNums();
}

Please let us know if this helps.

Thank you.

Hi,

You may give a try with using "IsPostBack" option.

for example,

if (!isPostBack)
{
ShowNums();
}

Please let us know if this helps.

Thank you.

Alas, it doesn't. And also after using isPostBack the new numbers are not generating.

Hi,

Try to use "Page_Init" event. it will be fired once. Javascript is another option.

If this also not working,
Could you let us know little bit more information on your requirement.
So that we can fix it.

As far as my understanding is,
1) You are generating random numbers
2) In a button click you are verifying the generated random number is matched or not.

But,

1) Page load event will be fired everytime. So random number are generated every page load.
2) IsPostBack will be used when a function/code block will not be fired in every page load.

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>
        &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Button" />
        <br />
        <br />
        <asp:Label ID="HowIsIt" runat="server" Text="Label"></asp:Label>
    &nbsp;&nbsp;&nbsp;
        <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?

Hi,

The methods ReturnFirstCaptchaNum() and ReturnSecondCaptchaNum() keep generating fresh numbers again and again. please do change the following methods as mentioned below to solve your issue

protected void Page_Load(object sender, EventArgs e)
        {
// Every time when the page submits the page load event is called. This again generates new number. !IsPostBack is not fired when the button click event is fired
            if (!IsPostBack)
            {
                ReturnFirstCaptchaNum();
                ReturnSecondCaptchaNum();
                ShowNums();
            }
        }

       protected void Button1_Click(object sender, EventArgs e)
        {
// the methods are replaced with the actual values of the labels
            int result = Convert.ToInt16(lblNum01.Text) + Convert.ToInt16(lblNum02.Text);
            if (tbAnswer.Text == result.ToString())
            {
                HowIsIt.Text = "Right";
                tbAnswer.Enabled = false;
            }
            else
            {
                HowIsIt.Text = "Wrong";
                tbAnswer.Text = "";
                tbAnswer.Enabled = true;
            }
        }

This should solve your problem

commented: exellent +1

Thank you. now it works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.