ASP.NET Tutorial/ASP.net Controls/Literal

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

asp:Literal

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal ID="Literal1" 
                     runat="server" Text="Hello, World!"></asp:Literal></div>
    </form>
</body>
</html>


Literal Control

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 <span> 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">
    <div>
    <h1>Look in the title bar</h1>
    </div>
    </form>
</body>
</html>


The Mode property of Literal control

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">
    <div>
    <asp:Literal
        id="ltlFirst"
        Mode="PassThrough"
        Text="<hr />"
        Runat="server" />
    <br /><br />
    <asp:Literal
        id="ltlSecond"
        Mode="Encode"
        Text="<hr />"
        Runat="server" />
    <br /><br />
    <asp:Literal
        id="ltlThird"
        Mode="Transform"
        Text="<hr />"
        Runat="server" />
    </div>
    </form>
</body>
</html>


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

<%@ 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">
    <div>
        <asp:literal runat="server" ID="currentTime"></asp:literal>
    </div>
    </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