Creating Captcha HTML Helper

After working with ASP.Net MVC, and my previous posts, in this Post I am trying to implement a Captcha HTML Helper, an HTML Helper is just a method that returns a string. Creating Custom HTML Helpers. It is an extension method, to the existing HTML Helper class.
Here is code.(For more details about the Captcha generation code, check my previous post. : Captcha using ASP.Net and C#);

 public static class CaptchaHelper
    {
        public static string Captcha(this HtmlHelper helper, string text)
        {
            string srcPath = System.Web.VirtualPathUtility.ToAbsolute("~/Handler1.ashx");
            string htmlContent = string.Empty;
            htmlContent += "<script type=\"text/javascript\">function __rc(){document.getElementById(\"" + text +
                           "\").src = \"../Handler1.ashx?query=\" + Math.random();}</script>";
            htmlContent += string.Format("<img id=\"{0}\" src=\"{1}\" alt=\"Captcha Image\"/>", text, srcPath);
            htmlContent += "<a href=\"#\" onclick=\"javascript:__rc();\">Reset</a>";
            return htmlContent;
        }
    }

And in the View you can use like, you may need to import the Namespace of CaptchaHelper class.

 < %= Html.Captcha("Sample") %>

It will render a security image. Happy Programming

This entry was posted in .Net, .Net 3.0 / 3.5, ASP.Net, ASP.Net MVC, Javascript and tagged , , , , , , , . Bookmark the permalink.

3 Responses to Creating Captcha HTML Helper

  1. Pingback: ASP.NET MVC Archived Blog Posts, Page 1

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>