Display value of Resource without Label or Literal

2020-08-09 10:11发布

How do I display the value of a resource without a ASP.NET control, i.e. I want to avoid this:

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

Instead I would prefer to do just this in my .aspx pages:

<%$ Resources: Messages, ThankYouLabel %>

... but I can’t, a parser error is thrown:

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

4条回答
够拽才男人
2楼-- · 2020-08-09 10:26

It's not possible. you have to use atleast Literal, Another option is to use GetGlobalResurceObject, so that you can use directly in a page.

<%= GetGlobalResourceObject("Messages", "ThankYouLabel")%>
查看更多
Summer. ? 凉城
3楼-- · 2020-08-09 10:31

In code behind You can Use

`GetLocalResourceObject("YourKeyInLocalResource")` 

and also

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

and then use a simple aspnet variable in your Asp.net Markup like <%= Resourcevalue %>

The you can assign your resource value to your Aspnet Variable like

Resourcevalue = GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey").ToString();
查看更多
时光不老,我们不散
4楼-- · 2020-08-09 10:33

Use HttpContext.GetGlobalResourceObject instead:

<asp:Label text='<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>' 
     id="label1" 
     runat="server" />
查看更多
倾城 Initia
5楼-- · 2020-08-09 10:38

Another method is :-

<asp:Label text='<%= Resources.Messages.ThankYouLabel %>' 
     id="label1" 
     runat="server" />
查看更多
登录 后发表回答