Επεξεργασία

Κοινή χρήση μέσω


Literal.Text Property

Definition

Gets or sets the caption displayed in the Literal control.

public:
 property System::String ^ Text { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string Text { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.Text : string with get, set
Public Property Text As String

Property Value

The caption displayed in the Literal control.

Implements

Attributes

Examples

The following example demonstrates how to use the Text property to programmatically change the caption displayed in the Literal control.

Note

The following example uses the single-file code model and might not work correctly if copied directly into a code-behind file. This code example must be copied into an empty text file that has an .aspx extension. For more information about the Web Forms code model, see ASP.NET Web Forms Page Code Model.


<%@ Page Language="C#" AutoEventWireup="True" %>

<!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>
    <title>Literal Example</title>
<script runat="server">

      void ButtonClick(Object sender, EventArgs e)
      {
         Literal1.Text="Welcome to ASP.NET!!";
      }

   </script>

</head>
<body>
   <form id="form1" runat="server">
      <h3>Literal Example</h3>

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

      <br /><br />

      <asp:Button id="Button1"
           Text="Change Literal Text"
           OnClick="ButtonClick"
           runat="server"/>

   </form>
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>

<!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>
    <title>Literal Example</title>
<script runat="server">

      Sub ButtonClick(sender As Object, e As EventArgs)
      
         Literal1.Text="Welcome to ASP.NET!!"
      
      End Sub

   </script>

</head>
<body>
   <form id="form1" runat="server">
      <h3>Literal Example</h3>

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

      <br /><br />

      <asp:Button id="Button1"
           Text="Change Literal Text"
           OnClick="ButtonClick"
           runat="server"/>

   </form>
</body>
</html>

Remarks

Use the Text property to specify or determine the caption displayed in the Literal control. This allows you to programmatically change the caption at run time.

The caption is HTML-decoded depending on how you assign a value to this property. If you assign a value through an attribute of the Literal control, the value is HTML decoded before it is displayed. For example, <asp:Literal id="DisplayLiteral" Text="A&nbsp;B"/> is rendered as "A B" on the browser. However, if you set this property programmatically or by placing the text between the opening and closing tags of the control, the caption is not HTML decoded. For example, <asp:Literal id="DisplayLiteral"> A&nbsp;B </asp:Literal> is rendered as "A B".

Caution

This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Validation Server Control Syntax.

The value of this property, when set, can be saved automatically to a resource file by using a designer tool. For more information, see LocalizableAttribute and Globalization and Localization.

Applies to

See also