如何:向 ASP.NET 网页添加 Localize Web Web 服务器控件 (Visual Studio)

更新:2007 年 11 月

当您希望在页上的特定区域中显示本地化的文本时,可以向 ASP.NET 网页添加 Localize Web 服务器控件。Localize 控件与 Literal Web 服务器控件完全相同,并与 Label Web 服务器控件相似。Label 控件允许您向显示的文本应用样式,而 Localize 控件则不允许这样做。通过设置从 Literal 控件继承的 Text 属性,您可以以编程方式控制在 Localize 控件中显示的文本。有关更多信息,请参见 Literal Web 服务器控件概述

向 ASP.NET 网页添加 Localize Web 服务器控件

  1. 从工具箱的**“标准”**选项卡中,将 Localize 控件拖动到页面上。

  2. 或者,将 Mode 属性设置为 TransformPassThroughEncodeMode 属性指定控件如何处理您添加到该控件中的标记。有关详细信息,请参见本地化 Web 服务器控件概述

  3. 将代码添加到页面上以在运行时设置控件的 Text 属性。

    下面的代码示例演示如何以编程方式设置 Localize 控件的文本和编码。该页包含一组单选按钮,允许用户在编码文本和按原样传递的文本之间选择。

    有关使用资源字符串的代码示例,请参见 Localize Web 服务器控件声明性语法

    ms247231.alert_note(zh-cn,VS.90).gif说明:

    如果将 Text 属性设置为从不受信任的源获取的文本,请将控件的 Mode 属性设置为 Encode,以使标记不可执行。

    <%@ 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>
    

请参见

概念

本地化 Web 服务器控件概述

参考

Localize Web 服务器控件声明性语法

Localize