简体   繁体   中英

How can I use strongly-typed resources in server tags?

I'm trying to change over some ASP.NET code to use the strongly-typed translations we're using in the C# code. Currently the code looks like this:

<asp:HyperLink runat="server" Text='<%$ Resources:Blah1,Blah2 %>' />

I'd like it to look like this:

<asp:HyperLink runat="server" Text='<% Translations.Blah1.Blah2 %>' NavigateUrl="#" />

The reason is that we have a custom provider used in the first case, and it doesn't play nice with aspnet_compiler, and we're trying to eliminate issues caused by aspx/ascx files that don't compile.

However, if I change the code (as shown above), I get this error:

Server tags cannot contain <% ... %> constructs.

Is there any way to achieve this without having to use <%# Databind %> tags and then ensure Databind is called? Using <%= %> doesn't work either (presumably because it writes directly to the response stream, but I need to have it set the property).

EDit : I need to this programatically across thousands of files, so moving it to a code-behind file is not an option :(

Give the hyperlink an ID, and set the text property from the code-behind.

Extra advantage: you split your logic from presentation and filling up their respective values

The only solution I could find is to put the resource between opening/closing tags. This only works with some controls, so others will need to be changed.

<asp:HyperLink runat="server" NavigateUrl="#"><% Translations.Blah1.Blah2 %></asp:HyperLink>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM