【问题标题】:Display value of Resource without Label or Literal control显示不带标签或文字控件的资源的值
【发布时间】:2011-03-08 07:12:44
【问题描述】:

如何在没有 ASP.NET 控件的情况下显示资源的值,即我想避免这种情况:

<asp:Label text="<%$ Resources: Messages, ThankYouLabel %>" id="label1" runat="server" />

相反,我更愿意在我的 .aspx 页面中这样做:

<%$ Resources: Messages, ThankYouLabel %>

...但我不能,抛出解析器错误:

Literal expressions like '<%$ Resources: Messages, ThankYouLabel %>' are not allowed.
Use <asp:Literal runat="server" Text="<%$ Resources: Messages, ThankYouLabel %>" /> instead.

【问题讨论】:

  • 按照建议使用Literal有什么问题?
  • 它使视图状态膨胀,我想将页面修剪到绝对最小值。我还认为,如果我真的不需要额外的控件时可以跳过它们,它会增加可读性。

标签: c# asp.net resources


【解决方案1】:

改用HttpContext.GetGlobalResourceObject

<asp:Label text='<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>' 
     id="label1" 
     runat="server" />

【讨论】:

    【解决方案2】:

    这是不可能的。你必须至少使用Literal,另一个选择是使用GetGlobalResurceObject,这样你就可以直接在一个页面中使用。

    <%= GetGlobalResourceObject("Messages", "ThankYouLabel")%>
    

    【讨论】:

      【解决方案3】:

      在后面的代码中你可以使用

      `GetLocalResourceObject("YourKeyInLocalResource")` 
      

      还有

      `GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey")` 
      

      然后在您的 Asp.net 标记中使用一个简单的 aspnet 变量,例如

      您可以将资源值分配给您的 Aspnet 变量,例如

      Resourcevalue = GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey").ToString();
      

      【讨论】:

      • 就像基本的 '' 是我真正需要的。谢谢。 :)
      【解决方案4】:

      另一种方法是:-

      <asp:Label text='<%= Resources.Messages.ThankYouLabel %>' 
           id="label1" 
           runat="server" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        • 2017-03-25
        • 2014-08-15
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        相关资源
        最近更新 更多