tags:

views:

48

answers:

4

I'm a bit new to .Net development, been working in Java for some time now. I have an aspx page and we need to externalize some strings to make it more flexible.

If I have a table somewhere and there is just a string sitting outside an asp tag, I can replace it so that

<th> Specific Foo String </th>

becomes

<th> <%= Strings.foo %> </th>

and everything is fine, the problem I'm running into is how do you do this kind of interpolation on an asp tag property

I tried changing

<asp:Label runat="server" ID="lblFoo" Text="Specific Foo String Entry" />

to

<asp:Label runat="server" ID="lblFoo" Text='<%= Strings.foo %> Entry' />

and

<asp:Label runat="server" ID="lblFoo" Text='<%#Eval("Strings.foo") %> Entry' />

but neither worked. Is what I'm doing not possible in the aspx file, I know that I can simulate this by rewriting their properties in the code behind, but that's a level of overhead I'd rather not deal with.

Thanks

A: 

In the codebehind of the page you would do this:

lblFoo.Text = Strings.foo + " Entry";

A good place to put this code would be in the overriden OnLoad method but that is simply a suggestion as I am unfamiliar with your application and the life cycle needs of your page.

If you want to do all this in the aspx page then simply do this:

<span><%= Strings.foo %> Entry</span>

as a Label renders as a span anyhow.

Andrew Hare
Thanks for your reply, although this would work in a simple case, I have a large application with some more complex cases (repeater headers and whatnot). I was hoping there would be a way to do this without having to figure out the lifecycle needs of every page and find the best implementation strategy. More academically than practically, can one perform property interpolation at the aspx level, or is this solely reserved for the code behind?But again, thanks for the answer.
ihumanable
+1  A: 

I think you are looking to do this:

<asp:Label runat="server" id="label1" Text='<%# Strings.Foo + " Entry"%>' />

Then in your code behind (most likely in your OnPageLoad) you need to call

if(!Page.IsPostBack) Page.DataBind();

You need to be cautious however as calling DataBind on controls like textboxes or any labels that may have changed due to logic in the code behind will have their values overwritten with the bound values. Checking that you are not on a post back can help with this, but there are still gotchas.

Also note that I had to move the " Entry" text into the binding statement. If it is placed outside the last '%>' then the binding does not work and it will spit out:

<%# Strings.foo %> Entry

NerdFury
A: 

If your objective is an HTML table of strings, then you can create either a ListView or a GridView and DataBind to that. It would save you the trouble of writing out all of your properties and will also produce the correct table tags for the data.

Without knowing more about your data, I cannot provide a detailed code snippet.

Rob Allen
A: 

You're talking about resources. Read Basic Instincts Resources and Localization in ASP.NET 2.0 which shows you the built in resource editor, and how to use the "<%$ ... %>"-binding, or using meta:resourceKey attribute.

Simon Svensson