Share via


How to: Add Localize Web Server Controls to ASP.NET Web Pages

You can add a Localize Web server control to your ASP.NET Web page when you want to display localized text in a specific area on the page. The Localize control is identical to the Literal Web server control and similar to the Label Web server control. While the Label control allows you to apply a style to the displayed text, the Localize control does not. You can programmatically control the text that is displayed in the Localize control by setting the Text property, which is inherited from the Literal control. For more information, see Literal Web Server Control Overview.

To add a Localize Web server control to an ASP.NET Web page

  1. Add a<asp:Literal>element to the page. For syntax, see Localize Web Server Control Declarative Syntax.

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

    <body>
      <form runat="server">
        <asp:Localize ID="Localize1" runat="server"></asp:Localize>
      </form>
    </body>
    
  2. Optionally, set the Mode property to Transform, PassThrough, or Encode. The Mode property specifies how the control handles markup that you add to it. For details, see Localize Web Server Control Overview.

  3. Add code to your page to set the control's Text property at run time.

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

    For a code example that uses resource strings, see Localize Web Server Control Declarative Syntax.

    NoteNote

    If you set the Text property to text that you obtained from an untrusted source, set the control's Mode property to Encode so that the markup is not executable.

    <%@ Page Language="VB" %>
    <script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs)
        Localize1.Text = "This <b>text</b> is inserted dynamically."
        If radioEncode.Checked = True Then
            Localize1.Mode = LiteralMode.Encode
        ElseIf radioPassthrough.Checked = True Then
            Localize1.Mode = LiteralMode.PassThrough
        End If
    End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
      <br />
      <asp:RadioButton 
        ID="radioEncode" 
        runat="server"
        GroupName="LocalizeMode" 
        Checked="True" 
        Text="Encode" 
        AutoPostBack="True" />
      <br />
      <asp:RadioButton 
        ID="radioPassthrough" 
        runat="server" 
        GroupName="LocalizeMode" 
        Text="PassThrough" 
        AutoPostBack="True" />
      <br />
      <br />
      <asp:Localize ID="Localize1" runat="server"></asp:Localize>
    </div>
    </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Localize1.Text = "This <b>text</b> is inserted dynamically.";
            if (radioEncode.Checked == true)
            {
                Localize1.Mode = LiteralMode.Encode;
            }
            if(radioPassthrough.Checked == true)
            {
                Localize1.Mode = LiteralMode.PassThrough;
            }
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
      <title>Untitled Page</title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
        <br />
        <asp:RadioButton 
          ID="radioEncode" 
          runat="server"
          GroupName="LocalizeMode" 
          Checked="True" 
          Text="Encode" 
          AutoPostBack="True" />
        <br />
        <asp:RadioButton 
          ID="radioPassthrough" 
          runat="server" 
          GroupName="LocalizeMode" 
          Text="PassThrough" 
          AutoPostBack="True" />
        <br />
        <br />
        <asp:Localize ID="Localize1" runat="server"></asp:Localize>
      </div>
      </form>
    </body>
    </html>
    

See Also

Reference

Localize Web Server Control Declarative Syntax
Localize

Concepts

Localize Web Server Control Overview