Name

HtmlEncode

Synopsis

returnstring = Server.HtmlEncode(s)
Server.HtmlEncode(s, output)

Returns a string in which any HTML tags found are encoded by using the HTML literal equivalents of symbols such as > (&gt;), < (&lt;), and even quotes (&quot;). This allows developers to display HTML and ASP source code on the page, rather than treating it as rendered output or code to execute.

Parameters

returnstring

A String variable to receive the encoded string from the method.

s

A string variable containing the HTML that the method will encode.

output

An instance of any class derived from the TextWriter class, such as a StringWriter class instance (found in the System.IO namespace), used to capture the encoded string.

Example

The code example declares two string variables, sets the value of StrToEncode, assigns the return value of the Server.HtmlEncode call to StrToReturn, and then writes the value to the browser using the Message label control. Note that you have to view the HTML source to see the actual string returned by the method call.

Sub Page_Load(  )
   Dim StrToEncode As String
   Dim StrToReturn As String
   StrToEncode = "<%@ Page Language=""VB"" %>"
   StrToReturn = Server.HtmlEncode(StrToEncode)
   Message.Text = StrToReturn
End Sub

Notes

This method is great for displaying the source of a page for educational purposes. It is also particularly useful for encoding text entered by users that may or may not be displayed or written to the browser. Without this encoding (or some form of filtering or ...

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.