Skip to content

Commit d5706cf

Browse files
author
Marin Bratanov
committed
docs(captcha): invalid the first time after ajax or callback
1 parent 46f8f30 commit d5706cf

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: Does not Validate the First Time
3+
page_title: Does not Validate the First Time | RadCaptcha for ASP.NET AJAX Documentation
4+
description: Does not Validate the First Time only the second time after an ajax request or callback.
5+
slug: captcha/troubleshooting/does-not-validate-the-first-time
6+
tags: validate,ajax,callback,first,time,second,time,works
7+
published: True
8+
position: 3
9+
---
10+
11+
# Captcha Does not Validate the First Time
12+
13+
**Problem**: The Captcha is always invalid the first time after an AJAX request. It validates from the second time onward.
14+
15+
**Details**: This is caused by AJAX requests that do not include the captcha, or by callbacks (like load-on-demand requests by a RadComboBox). Such an AJAX request initializes the captcha on the server, as any control is initialized, so the captcha generates a new image. This new image and its code, however, do not travel to the browser because the captcha is not part of the response, so the user does not have a chance of entering a correct code.
16+
17+
**Solution**: Wrap the captcha in an `UpdatePanel` with `UpdateMode="Always"` so it gets refreshed with the other postbacks and shows the correct image to the user. In some cases setting the `CaptchaImage-PersistCodeDuringAjax` property to `true` may help. Callbacks cannot be handled like this, however, because they cannot update controls, they only fetch data. So, having callbacks will always cause the captcha to be invalid one time.
18+
19+
Below you can find an example that showcases the two scenarios that cause the issue and the best available solution.
20+
21+
Scenario 1:
22+
23+
1. expand the combo so its callback is invoked
24+
1. validating the captcha from the button fails because the callback cannot update the captcha in any way
25+
1. validating the captcha from the button the second time works because the button click included the captcha in the response and it now has a correct code shown in the browser
26+
27+
Scenario 2:
28+
29+
1. select a new item from the combo
30+
1. the captcha will be invalid because it is a postback without a correct code entered and this is expected
31+
1. validating the captcha from the button will now work because it was part of the server response (taking out the UpdatePanel around it will cause it to fail, however)
32+
33+
````ASP.NET
34+
<asp:UpdatePanel ID="UpdatepanelCombo" runat="server" UpdateMode="Conditional">
35+
<ContentTemplate>
36+
<telerik:RadComboBox runat="server" ID="RadComboBox1" RenderMode="Lightweight" EnableLoadOnDemand="true" OnItemsRequested="RadComboBox1_ItemsRequested"
37+
AutoPostBack="true" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
38+
</telerik:RadComboBox>
39+
</ContentTemplate>
40+
</asp:UpdatePanel>
41+
<asp:UpdatePanel ID="UpdatepanelCaptcha" runat="server" UpdateMode="Always">
42+
<ContentTemplate>
43+
<telerik:RadCaptcha runat="server" ID="RadCaptcha1" ErrorMessage="The code you entered is not valid." CaptchaImage-TextChars="Numbers"></telerik:RadCaptcha>
44+
</ContentTemplate>
45+
</asp:UpdatePanel>
46+
<asp:UpdatePanel ID="UpdatepanelLabel" runat="server" UpdateMode="Conditional">
47+
<ContentTemplate>
48+
<asp:Label ID="Label1" Text="" runat="server" />
49+
</ContentTemplate>
50+
</asp:UpdatePanel>
51+
<asp:UpdatePanel ID="UpdatepanelButton" runat="server" UpdateMode="Conditional">
52+
<ContentTemplate>
53+
<asp:Button ID="Button1" Text="Submit (validate captcha)" OnClick="Button1_Click" runat="server" />
54+
</ContentTemplate>
55+
</asp:UpdatePanel>
56+
````
57+
58+
````C#
59+
protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
60+
{
61+
for (int i = 0; i < 5; i++)
62+
{
63+
RadComboBox1.Items.Add(new RadComboBoxItem(i.ToString()));
64+
}
65+
e.EndOfItems = true;
66+
}
67+
68+
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
69+
{
70+
Label1.Text = "postback from combo on " + DateTime.Now.ToString() + ", captcha is valid: " + RadCaptcha1.IsValid.ToString();
71+
UpdatepanelLabel.Update();
72+
}
73+
74+
protected void Button1_Click(object sender, EventArgs e)
75+
{
76+
Label1.Text = "postback from the button on " + DateTime.Now.ToString() + ", captcha is valid: " + RadCaptcha1.IsValid.ToString();
77+
UpdatepanelLabel.Update();
78+
}
79+
````
80+
````VB
81+
Protected Sub RadComboBox1_ItemsRequested(sender As Object, e As RadComboBoxItemsRequestedEventArgs)
82+
For i As Integer = 0 To 4
83+
RadComboBox1.Items.Add(New RadComboBoxItem(i.ToString()))
84+
Next
85+
e.EndOfItems = True
86+
End Sub
87+
88+
Protected Sub RadComboBox1_SelectedIndexChanged(sender As Object, e As RadComboBoxSelectedIndexChangedEventArgs)
89+
Label1.Text = "postback from combo on " + DateTime.Now.ToString() + ", captcha is valid: " + RadCaptcha1.IsValid.ToString()
90+
UpdatepanelLabel.Update()
91+
End Sub
92+
93+
Protected Sub Button1_Click(sender As Object, e As EventArgs)
94+
Label1.Text = "postback from the button on " + DateTime.Now.ToString() + ", captcha is valid: " + RadCaptcha1.IsValid.ToString()
95+
UpdatepanelLabel.Update()
96+
End Sub
97+
````
98+
99+
### See Also
100+
101+
* [Getting Started with RadCaptcha]({%slug captcha/getting-started%})
102+

0 commit comments

Comments
 (0)