Поделиться через


How to: Add Literal Web Server Controls to a Web Forms Page

You can add a Literal Web server control to your Web Forms page when you want to set text programmatically without adding extra HTML tags. The Literal control is useful as a way to add text into a page dynamically without adding any elements that are not part of the dynamic text. For example, you can use the Literal control to display HTML that you read from a file or from a stream.

Note

If you want to display static text, you can present it by using HTML — you do not need a Literal control. Use a Literal control only if you need to change the contents in server code dynamically.

To add a Literal Web server control to a Web Forms page

  1. From the Standard tab of the Toolbox, drag a Literal control onto the page.

  2. Optionally, in the Behavior category of the Properties window, set the Mode property to Transform, PassThrough, or Encode. The Mode property specifies how the control handles any markup that is added to it. For details, see Literal Web Server Control Overview.

    The following example shows a simple Web page that displays a headline at run time. The body of the page, including the Literal control, might look like this.

    <body>
      <form runat="server">
        <h1><asp:Literal id="Headline" runat=server 
          mode="PassThrough"/></h1>
      </form>
    </body>
    
  3. Add code to your page to set the control's Text property at run time.

    The following example shows how to programmatically set the text and encoding of the Literal control. The page contains radio buttons that allow the user to choose between encoded and pass-through text.

    Note

    If you are setting the Text property to text that you get from an untrusted source, set the control's Mode property to Encode so that the markup does not result in executable markup.

    <%@ Page Language="VB" %>
    <script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs)
        Literal1.Text = "This <b>text</b> is inserted dynamically."
        If radioEncode.Checked = True Then
            Literal1.Mode = LiteralMode.Encode
        ElseIf radioPassthrough.Checked = True Then
            Literal1.Mode = LiteralMode.PassThrough
        End If
    End Sub
    </script>
    
    <html>
    <head runat="server"></head>
    <body>
    <form id="form1" 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>
    
    <%@ Page Language="C#" %>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Literal1.Text = "This <b>text</b> is inserted dynamically.";
            if (radioEncode.Checked == true)
            {
                Literal1.Mode = LiteralMode.Encode;
            }
            if(radioPassthrough.Checked == true)
            {
                Literal1.Mode = LiteralMode.PassThrough;
            }
        }
    </script>
    </script>
    
    <html>
    <head runat="server"></head>
    <body>
        <form id="form1" 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>&nbsp;</div>
        </form>
    </body>
    </html>
    

See Also

Reference

Literal Web Server Control Overview