ASP.NET Tutorial/ASP.net Controls/Literal

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

asp:Literal

   <source lang="csharp">

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Literal ID="Literal1" 
runat="server" Text="Hello, World!"></asp:Literal>
   </form>

</body> </html></source>


Literal Control

   <source lang="csharp">

The Literal control is similar to the Label control. However, unlike the Label control, the Literal control does not render its content inside of a tag. <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       ltlTitle.Text = DateTime.Now.ToString("D");
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head>

   <title><asp:Literal id="ltlTitle" Runat="Server" /></title>

</head> <body>

   <form id="form1" runat="server">

Look in the title bar

   </form>

</body> </html></source>


The Mode property of Literal control

   <source lang="csharp">

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.

<%@ Page Language="C#" %> <!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 id="Head1" runat="server">

   <title>Show Literal Mode</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Literal
       id="ltlFirst"
       Mode="PassThrough"
Text="
"
       Runat="server" />
   

<asp:Literal id="ltlSecond" Mode="Encode"
Text="
"
       Runat="server" />
   

<asp:Literal id="ltlThird" Mode="Transform"
Text="
"
       Runat="server" />
   </form>

</body> </html></source>


Use code behind to set value to asp:Literal (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="LiteralTime" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:literal runat="server" ID="currentTime"></asp:literal>
   </form>

</body> </html>

File: Default.aspx.vb

Partial Class LiteralTime

   Inherits System.Web.UI.Page
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       currentTime.Text = DateTime.Now
   End Sub

End Class</source>