 |
Website Design Guide
|
|
|
|
Search Engine Submission
|
|
|
They Said It Best |
NULLThere is no reason anyone would want a computer in their home. Ken Olson, president, chairman and founder of DEC
|
|
|
|
 |
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.
- Create an ASP.NET TexBox control called HTMLToEncode.
- Create a 2nd ASP.NET TextBox control called EncodedHTML.
- 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> </p>
<h3>HTML Encoded Output</h3><br />
<asp:TextBox textmode=multiline rows=15 columns=40 ID=EncodedHTML runat=server />
<p> </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.
|
|
 |
 |