Improvement to HIP CAPTCHA control for Blog Engines
This is a small improvement to HIP CAPTCHA control (CAPTCHA for blog engines based ASP.NET). (Hey Miguel). It also demonstrates how to use HtmlTextWriter to render HTML properly in ASP.NET. Forget about writing HTML strings. You have to write a bit more code, but you get well-formatted HTML with automatic support for older browsers.
protected override void Render(HtmlTextWriter writer) { StringBuilder sb = new StringBuilder(); sb.Append("?iw="); sb.Append(this._imageWidth); sb.Append("&ih="); sb.Append(this._imageHeight); sb.Append("&tc="); sb.Append(this._textColor.ToArgb()); sb.Append("&tpc="); sb.Append(this._textPatternColor.ToArgb()); sb.Append("&bc="); sb.Append(this._backgroundColor.ToArgb()); sb.Append("&bpc="); sb.Append(this._backgroundPatternColor.ToArgb()); sb.Append("&tp="); sb.Append(this._textPattern.ToString()); sb.Append("&bp="); sb.Append(this._backgroundPattern.ToString()); sb.Append("&fs="); sb.Append(this._fontSize); sb.Append("&hc="); sb.Append(HttpUtility.UrlEncode(this._hipCode)); string strControlMessage = this._controlMessage; if (strControlMessage.Length <= 0) { strControlMessage = "Enter the code you see"; } // // render writer writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0"); writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0"); writer.AddAttribute(HtmlTextWriterAttribute.Border, "0"); writer.RenderBeginTag(HtmlTextWriterTag.Table); writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2"); writer.RenderBeginTag(HtmlTextWriterTag.Td); // image writer.AddAttribute(HtmlTextWriterAttribute.Src, "HIPGenerator.aspx" + sb.ToString()); writer.AddAttribute(HtmlTextWriterAttribute.Width, this._imageWidth.ToString()); writer.AddAttribute(HtmlTextWriterAttribute.Height, this._imageHeight.ToString()); writer.AddStyleAttribute("border", String.Format("{0} solid {1}px", this._imageBorderColor.ToKnownColor(), this._imageBorderSize)); writer.RenderBeginTag(HtmlTextWriterTag.Img); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderBeginTag(HtmlTextWriterTag.Td); // span writer.AddStyleAttribute("font-family", "verdana"); writer.AddStyleAttribute("font-size", "6pt"); writer.RenderBeginTag(HtmlTextWriterTag.Span); writer.Write(strControlMessage + ":"); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Align, "left"); writer.RenderBeginTag(HtmlTextWriterTag.Td); // input text writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID); writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, this.CodeLenght.ToString()); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderEndTag(); if (this._validationMode == PersistMode.Forms) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2"); writer.RenderBeginTag(HtmlTextWriterTag.Td); // input hidden writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden"); writer.AddAttribute(HtmlTextWriterAttribute.Id, "hipCodeEnc"); writer.AddAttribute(HtmlTextWriterAttribute.Name, "hipCodeEnc"); writer.AddAttribute(HtmlTextWriterAttribute.Value, this._hipCode); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderEndTag(); } writer.RenderEndTag(); }
Saturday, November 27, 2004 5:32 AM