views:

31

answers:

3

I'm trying to convert an ASP.NET web application to use resource files. I haven't used resource files before, so I'm just toying around with them and was wondering if this is possible.

I have a message that returns from a search when no results are found, that prompts the user to return to the home page. A lot of these pages have methods to determine what is the proper page to send the user to, so there are many sections with markup similar to this:

Sorry, but we could not find an item matching your search criteria.

Please adjust your search criteria or <a href="<%= SomeMethodToDetermineUri() %>">return to (SomePage)</a>.

So basically, some type of message, followed by a link or a list of links. Getting the message part works fine, it's the server side code to generate links thats the problem.

What is the best way to put that into a resource file? It is able to recognize the html link part just fine, but the server side code gets inserted as plain text.

Is the only way to break it into 2 resources? (Which seems messy)

<%= Resources.Master.NoSearchResultsFound %>
<a href="<%= SomeMethodToDetermineUri() %>"> 
   <%= Resources.Master.NoSearchResultsFoundReturnLinkText %>
</a>.

Or is there a way to get the page to evaluate the server code?

+1  A: 

If you have similar functionality on your site I would suggest using User Controls or Master Pages to handle what you are attempting to do. I believe Resources are more for use with files outside of your project.

RandomBen
The functionality and the messages are not quite the same, that was an example. When I said they are similar, I meant the block of (some text) (some server side code to generate link(s)) (more text) is used throughout the project. It's the server side code part thats a bump in the road to customize the messages to the user. Sorry for the confusion, I've updated the question.
Brandon
I guess I am confused. Why not just use an asp:Literal tag and make a class in App_Code that does all of the work for you?
RandomBen
Well now I'm confused :P How would using a Literal control help me? I'd still be mashing together two resources and a method to generate the url, I'd just be doing it in a different place.
Brandon
I guess without knowing what is in your resources I am not sure what else to do. It seems like this is a case where doing all of your mashing in a single class external to the page would make the most sense but it sounds like you must have a great deal of code in your resources.
RandomBen
+1  A: 

You don't want to put code of any kind into a resource file. Your solution is the correct one, even if you feel it's messy.

The only other thing you could do is to encapsulate this in a user control. The user control would go fetch the resources and therefore hide the messiness from the page.

John Saunders
+1, The usercontrol would work, the problem is that this is a really big web application. Having to make a user control for each section would not be fun. :P
Brandon
@Brandon: pretend you wrote all those controls. Now, what would be the difference between them? Could some of that difference be factored out into properties, for instance? Maybe give the user control a property for the name of the label resource and one for the resource to use within the `<a>` tag. Then you've only got one control.
John Saunders
@John, you are right, refactoring it down would drastically cut down the number of user controls, but what I'm left with would still be a large number. Using String.Format would probably be the easiest thing for me to do in the long run.
Brandon
+1  A: 

Bah, I forgot about my old friend String.Format

<%= String.Format(Resources.Master.NoSearchResultsFound, SomeMethodToDetermineUri() %>

Works perfectly fine.

Then the value in the resource file is just

Sorry, but we could not find an item matching your search criteria.

Please adjust your search criteria or <a href="{0}">return to (SomePage)</a>.

Brandon