开发者

Passing parameters to a User Control

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, th开发者_开发知识库e current language is not sent from the page to the usercontrol... any help?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜