views:

342

answers:

2

We are trying here to localize our user control basically we want to be able to do something like this :

<in:Banner runat="server" ID="banners" Lang="fr" />

The way we do this is by page level and send it to master that then send it to the control:

protected void Page_Load(object sender, EventArgs e)
{
    Master.Lang = "FR";
}

Then in the MasterPage.master we do something like this :

<in:Banner runat="server" ID="banners" Lang="<%= Lang %>" />

The masterpage has a public proprety named Lang.

In the control we have set a field that contains the default language and a proprety (Lang) that set the language. It seems that whatever we do, the current language is not sent from the page to the usercontrol... any help?

+1  A: 

Not exactly like that, but you can consider the content page like a control in the master page, so its likely that the page load of the page is executing before the page load of that user control.

Regardless of the above, why not set the ui culture to the asp.net thread (probably from the global.asax), and using that from the control.

Another alternative is to have a separate class where you hold the current language, and expose a change event ... that way you can make sure to use the right language even if the load is done differently --- or there is a change language event later.

eglasius
Not so sure about a page being a control in a master page. It is more like an inheritance, that's why you specify the master type. I agree with the ui culture suggestion though.
Mark Dickinson
Sounds like an idea, I will have to look on that one tho passing the Lang from the page sounds easier.
Erick
Can you specify UICulture for a single control? It would be interesting to find out if it can be applied at various levels other than at the application level.
NickGPS
@Mark u are right, that's why I said not exactly like that - I have found weird behaviors in the master pages and the simplest way I have found to think about it is that. Specifically the controls in the master page are merged into the content page controls hierarchy, so the content in the master page ends up working as a container for the one in the content page. I though that maybe it was the load of the content page vs. the one in the master page (for some reason I was under that idea), but the information here says otherwise: http://msdn.microsoft.com/en-us/library/dct97kc3.aspx ...
eglasius
@Erick you might want to validate my claim b4 using my above analysis - i.e. check which load is called first. Still the above ideas make sense to be used, but you want to make sure you go after the right behaviors. How are you using the language in your custom Banner?
eglasius
@NickGPS n, that's for the full request + it needs to be set v. early in the request lifecycle. If you have parts of your application that you don't want localized, you can just not add text for those to the specific languages resource files.
eglasius
+1  A: 

You can access it in code-behind of the MasterPage like this

public void SetLanguage(string language)
{
    banners.Lang = language; //banners is an ID reference to your user control.
}

Or in your markup I think you can do it like this

<in:Banner runat="server" ID="banners" Lang='<%# Bind("Lang") %>' />

I should mention that Bind works in .Net 2.0 and up.

NickGPS