How to write encoded text on the web page in ASP.NET

When you want to write encoded text on the web page you should use the Literal control, because it does support a property that is not supported by the Label control: the Mode property. The property enables you to encode HTML content and accept any of the following three values:

–  PassThrough—Displays the contents of the control without encoding.

–  Encode—Displays the contents of the control after HTML encoding the content.

– Transform—Displays the contents of the control after stripping markup that is not supported by the requesting device.

The next .aspx page contains three Literal controls that are set to the three possible values of the Mode property:

TestEncode.aspx

<%@ Page Language=”vb” AutoEventWireup=”false” CodeBehind=”TestEncode.aspx.vb” Inherits=”TextChange.TestEncode” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>Write encoded text on the web page in ASP.NET</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:Literal ID=”Literal1″ runat=”server” Mode=”PassThrough” Text=”<b><hr /></b>”></asp:Literal>

<br />

<asp:Literal ID=”Literal2″ runat=”server” Mode=”Encode” Text=”<b><hr /></b>”></asp:Literal>

<br />

<asp:Literal ID=”Literal3″ runat=”server” Mode=”Transform” Text=”<b><hr /></b>”></asp:Literal>

</div>

</form>

</body>

</html>