RSS

Search Engine

Thursday, May 20, 2010

ASP.Net Literal Mode Property

The Mode property of ASP.Net Literal Control enables you to direct the control for customizing the rendering behavior of the specified text that you want to display on the web page. The Mode property accepts the predefined enumeration value that renders the output text according to the specified value. It enables you to display the HTML formatted text as the plain text containing encoded markup tags. You can also display the HTML formatted text as it is. The Literal control supports the dynamic rendering of text value assigned at the server side that may be retrieved from the user input. Using the Mode property you can encode the scripts before rendering them on the web page to prevent the security vulnerabilities.

ASP.Net Literal Mode Property Values

The Mode property supports the predefined set of values stored as enumeration of LiteralMode. Each value controls the Literal output in a different way to render the specified text on the web page. Following are the three types of Literal Modes:

1. Encode: It converts the specified text content to the HTML encoded string. It converts the & to & <> to >.

2. PassThrough: It does not modify the specified content and renders it by applying the HTML formatting or scripts specified in it.

3. Transform: It removes the markup tags of the unsupported markup languages when the Literal control renders on the HTML or XHTML compliance web browsers. If the specified content contains the HTML formatting then it renders the text by applying the HTML formats without modifying the content.

Sample Code for Literal Control with Mode Property

asp:Literal ID="Literal1"
runat="server"
Text="This is a literal Control.
This is a second text line."
asp:Literal


asp:Literal ID="Literal2"
runat="server"
Mode="Encode"
Text="This is a literal Control.This is a second text line."
asp:Literal


asp:Literal ID="Literal3"
runat="server"
Mode="PassThrough"
Text="This is a literal Control.This is a second text line."
asp:Literal

asp:Literal ID="Literal4"
runat="server"
Mode="Transform"
Text="This is a literal Control.
This is a second text line."
asp:Literal


In the above sample code the second Literal control we have specified the Mode as "Encode" that will convert the content to the HTML encoded content. It will not render the specified HTML formatting while rendering the content on the web page.

For the third Literal control we have specified the "PassThrough" Mode that will render the specified content by applying the HTML formats coded in the text.

In the last Literal control we have specified the Mode property value as "Transform". It will remove the unsupported markup tags from the specified text such as and . The transform mode will render the HTML formatting specified in the text without modifying its content on the web page. The "Transform" mode of the Literal control is the default behavior that will render the first Literal control also be removing the unsupported markup tag such as and but it will render the HTML
tag while generating the output.

0 comments:

Post a Comment