This is a migrated thread and some comments may be shown as answers.

RadCaptcha not generating new code after postback

8 Answers 187 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Enigma
Top achievements
Rank 1
Enigma asked on 09 Nov 2010, 03:34 PM
Hi Pero,

I am having a problem with RadCaptcha.  I have it on a Contact page.  When the user fills out the information, the page posts back and emails the information to the admin.  I then hide the div that has the form and display another div to show thank you message.  We are on the same page so no Transfer. If the user clicks on Back button in the browser then he/she can postback again using the same code.  No problem.  I do check for IsValid property before sending out the email and the ISValid always returns true

if (Page.IsValid && RadCaptcha1.IsValid)
            {
                try
                {                                       MailHelper.Instance.SendSystemOwnerEmail("Contact form filled out.", txtName.Text);                 
  
                    this.txtName.Text = string.Empty;
                    // With the code below or without it.  The CAPTCHA image doesnt change and the user can click on Back button and use the same code to postback.
                    RadCaptcha1.CaptchaImage.RenderImage();
  
                    divContact.Visible = false;
                    divThankYou.Visible = true;
                }

8 Answers, 1 is accepted

Sort by
0
Pero
Telerik team
answered on 10 Nov 2010, 05:03 PM
Hi Enigma,

Please, call the RadCaptcha.Validate and Page.Validate methods before checking for the RadCaptcha's IsValid property, to be sure the validation actually has occurred.

Basically your code should look like the following:

RadCaptcha1.Validate();
Page.Validate();
 
if(Page.IsValid && RadCaptcha1.IsValid)
{
    try
    {                                       MailHelper.Instance.SendSystemOwnerEmail("Contact form filled out.", txtName.Text);                 
 
        this.txtName.Text = string.Empty;
        // With the code below or without it.  The CAPTCHA image doesnt change and the user can click on Back button and use the same code to postback.
        RadCaptcha1.CaptchaImage.RenderImage();
 
        divContact.Visible = false;
        divThankYou.Visible = true;
    }



Regards,
Pero
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Enigma
Top achievements
Rank 1
answered on 11 Nov 2010, 06:40 AM
Hi Pero, I tried that but still no go.  I am attaching my sample project.  Fill out the name, click on Submit and then click on Browsers back button and just type in the same code again and click submit and then click on back button again and do the same thing.  The same Captcha work repeatedly.

Thanks for your help.

PS: The attached project is a RAR.  Please just rename the extension from PNG to RAR.Telerik version: 2009.2.826.35
0
Enigma
Top achievements
Rank 1
answered on 11 Nov 2010, 06:44 AM
protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            RadCaptcha1.Validate();
            Page.Validate();
 
            if (Page.IsValid && RadCaptcha1.IsValid)
            {
                // Send out email code.
 
                // clear form and show thank you message
                this.txtname.Text = string.Empty;
                contactformdiv.Visible = false;
                thankyoudiv.Visible = true;
                time.Text = DateTime.Now.ToString();
            }
        }

<form id="form1" runat="server">
  <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
  </telerik:RadScriptManager>
  <div>
      <div id="contactformdiv" runat="server">
 
          Name: <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
              ErrorMessage="Required!" ControlToValidate="txtname" ValidationGroup="ContactGroup"></asp:RequiredFieldValidator>
          <br />
          <telerik:RadCaptcha ID="RadCaptcha1" Runat="server" ForeColor="Red"
              IsValid="False" ValidationGroup="ContactGroup">
          </telerik:RadCaptcha>
          <br />
          <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit"
              ValidationGroup="ContactGroup" />
          <br />
           
      </div>
      <div id="thankyoudiv" runat="server" visible="false">
          Thank you for submitting the form.
          <br />
          <asp:Label ID="time" runat="server"></asp:Label>
      </div>
  </div>
  </form>
0
Pero
Telerik team
answered on 11 Nov 2010, 12:07 PM
Hello Enigma,

The problem seems to be caused by setting Visible=false to the <div/> containing the RadCaptcha control. Apparently setting Visible=false to a control, causes its current state to be not saved in the same way as Visible=true. Here is the modified code:

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <div>
        <div id="contactformdiv" runat="server">
            Name:
            <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required!"
                ControlToValidate="txtname" ValidationGroup="ContactGroup" EnableClientScript="false"></asp:RequiredFieldValidator>
            <br />
            <telerik:RadCaptcha ID="RadCaptcha1" runat="server" ForeColor="Red" ValidationGroup="ContactGroup"
                ErrorMessage="Invalid Code">
            </telerik:RadCaptcha>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" ValidationGroup="ContactGroup" />
            <br />
        </div>
        <div id="thankyoudiv" runat="server" visible="false">
            Thank you for submitting the form.
            <br />
            <asp:Label ID="time" runat="server"></asp:Label>
        </div>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Default_Captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadCaptcha1.Validate();
        Page.Validate();
 
        if (Page.IsValid && RadCaptcha1.IsValid)
        {
            // Send out email code.
 
            // clear form and show thank you message
            this.txtname.Text = string.Empty;
             
            contactformdiv.Style.Add("display", "none");
            //contactformdiv.Visible = false;
             
            thankyoudiv.Visible = true;
            time.Text = DateTime.Now.ToString();
        }
        RadCaptcha1.CaptchaImage.TextChars = Telerik.Web.UI.CaptchaPossibleChars.LettersAndNumbers;
    }
 
}


Best wishes,
Pero
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Enigma
Top achievements
Rank 1
answered on 11 Nov 2010, 10:13 PM
Thanks Pero.  That is basically what we ended up doing anyways on our end.  We don't like the solution because we are using CAPTCHA on a signup page and the whole signup form is rendered back to the browser and we are forced to use CSS to hide it.

I thought RadCaptcha was using Cache and Session on server and once the code was used it was taken out of server cache so it could not be used again.  Doesn't seem like that is a true statement.  RadCaptcha is depending on the page to store its info because if there are problems rendering the control on the page such as in this instance (div hiding where the captcha lies) the captcha code can be used repeatedly.

I did try another third party control (formshield) and it does behave correctly even with div hiding.  Since my company has telerik license they do not want us introducing other third party controls.

Cheers,
Mans.
0
Pero
Telerik team
answered on 12 Nov 2010, 03:53 PM
Hi Mans,

The code is removed, but I suppose setting Visible=false to RadCaptcha causes the control to not reset the code correctly. I will log this issue for further research.

If you don't want to reload the entire signup form, you could divide the form into two parts, one part containing the RadCaptcha and the button, and the other the rest of the form. The captcha can be hidden using CSS, and the rest with Visible=false.

<div>
    <div id="contactformdiv" runat="server">
        Name:
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required!"
            ControlToValidate="txtname" ValidationGroup="ContactGroup" EnableClientScript="false"></asp:RequiredFieldValidator>
        <br />
    </div>
    <div id="divCaptcha" runat="server">
        <telerik:RadCaptcha ID="RadCaptcha1" runat="server" ForeColor="Red" ValidationGroup="ContactGroup"
            ErrorMessage="Invalid Code">
        </telerik:RadCaptcha>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" ValidationGroup="ContactGroup" />
        <br />
    </div>
</div>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Default_Captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadCaptcha1.Validate();
        Page.Validate();
 
        if (Page.IsValid && RadCaptcha1.IsValid)
        {
            // Send out email code.
 
            // clear form and show thank you message
            this.txtname.Text = string.Empty;
 
            divCaptcha.Style.Add("display", "none");
            contactformdiv.Visible = false;
             
            thankyoudiv.Visible = true;
            time.Text = DateTime.Now.ToString();
        }
        RadCaptcha1.CaptchaImage.TextChars = Telerik.Web.UI.CaptchaPossibleChars.LettersAndNumbers;
    }
 
}


Regards,
Pero
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Erik
Top achievements
Rank 2
answered on 03 Mar 2014, 07:05 PM
This still seems to be the issue:
http://www.telerik.com/forums/radcaptcha-validation-fails-on-second-postback#WVD1nUYBK0iulsvA9nyssA
0
Slav
Telerik team
answered on 06 Mar 2014, 05:43 PM
Hello Erik,

I have responded to your post in the forum thread you linked. Please check it and if possible send the requested information so that I can help you.

I would suggest continuing our discussion in the other thread, so that all of the information is in one place and easy to track.

Regards,
Slav
Telerik

DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!

Tags
Captcha
Asked by
Enigma
Top achievements
Rank 1
Answers by
Pero
Telerik team
Enigma
Top achievements
Rank 1
Erik
Top achievements
Rank 2
Slav
Telerik team
Share this question
or