ASP .NET 2.0 Captcha Control
(Compatible with Telerik RadAjax and Peter Blum validators)
7 June 2008
I needed a simple ASP .NET Captcha control and this one on
The Code Project appeared to do everything that I wanted. However, it didn't work very well with Telerik's RadAjax or
Peter Blum's Professional Validation And More, so I rearranged a few things. You may find it also behaves better with regular ASP .NET "Atlas"
Ajax. Please let me know if it does.
Download ASP .NET 2.0 source files
Sorry I can't remember the exact details of what I changed, but I've been using it successfully for a while now and haven't experienced any problems.
Let me know if it gives you any trouble or if you can make any improvements.
Installation
Please refer to the original instructions.
Briefly:
- Copy the dll to your bin directory, or add a reference to it in your web project.
- Reference it from your web.config like this:
<system.web>
<pages>
<add tagPrefix="my"> namespace="WebControlCaptcha" assembly="WebControlCaptcha" />
</pages>
-
Add an html handler:
<system.web>
<httpHandlers>
<add verb="GET"> path="CaptchaImage.aspx" type="WebControlCaptcha.CaptchaImageHandler, WebControlCaptcha" />
</httpHandlers>
(Note that the page CaptchaImage.aspx doesn't actually exist!)
-
Now you should be able to add it to your page like this:
<my:CaptchaControl runat="server" />
You should be able to attach a custom validator to it and AJAX post backs will work correctly.
Update: 17 Sep 2008
Achilles has had some difficulty using this control and has had a bit of a cry about
it on his blog :-).
The trouble he was having with my solution occurs when the CaptchaControl itself is part of a RadAjax update, for example if the CaptchaControl
is inside a RadAjaxPanel. Sure enough, when I tried it out, the control didn't work properly.
He has posted some new code, however his solution has the same problem that I was originally trying to get around!
If an ajax postback occurs elsewhere on the page, perhaps in a completely unrelated place, then the CaptchaControl will not
work correctly.
So anyway, you the reader now have three choices to choose from:
- CodingHorror's original code which never
works with RadAjax.
- Achille's code that works if the CaptchaControl is inside a RadAjax update but
doesn't work if an ajax postback occurs elsewhere on the page.
- Mumblepuppy's solution which works if a RadAjax update occurs elsewhere on
the page, but not if the CaptchaControl is part of a RadAjax update itself.
Perhaps someone else wants to have a shot at fixing it?