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