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

Search Engine Submission

 

They Said It Best

There are two major products that came out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence.
Jeremy S. Anderson

 

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 />

<p>&nbsp;</p>

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

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

<p>&nbsp;</p>

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


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)
    {
    
        EncodedHTML.Text=Server.HtmlEncode(HTMLToEncode.Text);

    }
}

Test Drive

You can try out the HTML Encoder now.

 

Privacy Policy Copyright © 2010 dotnetdesignguide.com  Webmaster  Sponsored by www.portable-computer-keyboard.com