简体   繁体   English

将参数传递给用户控件

[英]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: 我们这样做的方式是通过页面级别将其发送给master,然后将其发送到控件:

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

Then in the MasterPage.master we do something like this : 然后在MasterPage.master中我们做这样的事情:

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

The masterpage has a public proprety named Lang. 该主页有一个名为Lang的公共财产。

In the control we have set a field that contains the default language and a proprety (Lang) that set the language. 在控件中,我们设置了一个包含默认语言的字段和一个设置语言的proprety(Lang)。 It seems that whatever we do, the 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. 无论如何,为什么不将ui文化设置为asp.net线程(可能来自global.asax),并使用控件中的文件。

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 您可以像这样在MasterPage的代码隐藏中访问它

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. 我应该提到Bind在.Net 2.0及更高版本中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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