Click to learn how to rotate images dynamically with the ASP.NET Image Control
Step by Step ASP.NET Design Solutions
The Ultimate Website Design Guide Using ASP.NET and Adobe Photoshop

Search Engine Submission


They Said It Best

The Internet is a great way to get on the net.
Senator Bob Dole


HTML Encoder

Have you ever tried to embed some scripting examples into a web page? It doesn't come out looking pretty. This example will show you how to create your own HTML Encoder.

Step 1. ASP.NET TextBox

Using Visual Studio create a new webform page called html-encoder.aspx.

  1. Create an ASP.NET TexBox control called HTMLToEncode.
  2. Create a 2nd ASP.NET TextBox control called EncodedHTML.
  3. Create an ASP.NET Button with a OnClick subroutine called EncodeHTML.

Be sure to include validateRequest=false in your @Page directive or else you'll get warning about dangerous content when you submit the form. After you are finished your form should look like this:

<form  runat=server>
<h3>HTML Encode Input Text</h3>

<p>Type or paste in the text you want to HTML encode, then press the <b>Encode</b> button<br />

<asp:TextBox textmode=multiline rows=15 columns=40 ID=HTMLToEncode runat=server />


<h3>HTML Encoded Output</h3><br />

<asp:TextBox textmode=multiline rows=15 columns=40 ID=EncodedHTML runat=server />


<asp:Button id=btnEncode OnClick=EncodeHTML runat=server Text="Encode HTML" />

Step 2. Code-behind

Edit the code-behind file html-coder.aspx.cs. Using Server.HTMLEncode() you only need one line to assign ecoded value the 2nd ASP.NET TextBox.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class html_encoder : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void EncodeHTML(object sender, EventArgs e)


Test Drive

You can try out the HTML Encoder now.


Privacy Policy Copyright © 2010  Webmaster  Sponsored by free spam filter