Onlineatoz.net | ToolBox Standard Control Literal

ToolBox Standard Control Literal

Literal Web Server Control

in ASP.NET environment,Literal Web server can be served as a container for other content on the page. The Literal control is used most frequently when adding content dynamically to the page.

Literal control's Options

The major purpose ot the Literal control is for for adding content to a page. For static content, you can add markup directly to a page as HTML, without using a container. However, if you want to add content dynamically, you must add the content to a container. Typical containers are the Label control, the Literal control, the Panel control, and the Placeholder control.

The Literal control differs from the Label control in that the Literal control does not add any HTML elements to the text. (The Label control renders a span element.) As a consequence, the Literal control does not support any style attributes, including position attributes. However, the Literal control allows you to specify whether content is encoded. In general, use a Literal control when you want to render text and controls directly into a page without any additional markup.

Encoding Content in the Literal Control

The Literal control contains the Mode property, which specifies how the control handles markup that you add to it. You can set the Mode property to these values:

  • Transform - Any markup you add to the control is transformed to accommodate the protocol of the requesting browser.
  • PassThrough -Any markup you add to the control is rendered as-is to the browser.
  • Encode - Any markup you add to the control is encoded using the HtmlEncode method, which converts HTML encoding into its text representation.
To add a Literal control to a page
  • Type an <asp:Literal> element in the page.
  • Optionally, set the Mode property to Transform, PassThrough, or Encode. The Mode property specifies how the control handles any markup that is added to it.
  • You must set the Id and runat property of the Literal Web Control.

the following code snippets demonstrates a Literal control

<body>
<form id="Form2" runat="server">
<h1><asp:Literal id="Headline" runat=server
mode="PassThrough"/></h1>
</form>
</body>
 
To add Control's Text programmatically

the following code snippets demonstrates this

in .aspx.cs page,

protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = "changed.";
if (radioEncode.Checked == true)
{
Literal1.Mode = LiteralMode.Encode;
}
if(radioPassthrough.Checked == true)
{
Literal1.Mode = LiteralMode.PassThrough;
}
}

in .aspx page,

<html>
<head id="Head1" runat="server"></head>
<body>
<form id="form2" runat="server">
<div>
<br />
<asp:RadioButton
ID="radioEncode"
runat="server"
GroupName="LiteralMode"
Checked="True"
Text="Encode"
AutoPostBack="True" />
<br />
<asp:RadioButton
ID="radioPassthrough"
runat="server"
GroupName="LiteralMode"
Text="PassThrough"
AutoPostBack="True" />
<br />
<br />
<asp:Literal ID="Literal1" runat="server"></asp:Literal> </div>
</form>
</body>
</html>
 
Related Links

Posted by: Admin
Posted on: 9/13/2011 at 1:52 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed