How to: Validate Text for Display in a Web Browser in Visual Basic

This example demonstrates how to use the HtmlEncode method to ensure that a browser correctly displays text. Characters in a string such as <, >, and & are rendered incorrectly unless they are escaped as &lt;, &gt;, and &amp;.

The following code example encodes a string to be displayed in a browser. It encodes the string named TestString, which contains the text This is a <Test String>, and copies it into the string named EncodedString as This is a &lt;Test String&gt;.

Dim TestString As String = "This is a <Test String>"
Dim EncodedString As String = Server.HtmlEncode(TestString)

This example requires a reference to System.Web.dll.

Community Additions