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:

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development: Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code, 4th Edition
ASP.NET Core 3 and Angular 9: Full stack web development with .NET Core 3.1 and Angular 9, 3rd Edition
C# in Depth, 4th Edition

–  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:


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

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

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

<head runat=”server”>

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



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


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