简体   繁体   English

ASP.NET 使用 BasePage 与 MasterPage 进行模板化

[英]ASP.NET Templating with BasePage vs MasterPage

I have to deal with an ASP.NET project in which i have to support localization and translation.我必须处理一个 ASP.NET 项目,我必须在其中支持本地化和翻译。 Change of colture may happen only in InitializeCulture method of Page object and not on MasterPage. colture 的变化可能只发生在 Page object 的 InitializeCulture 方法中,而不发生在 MasterPage 上。

Usually when i have to deal with common elements (header/footer) i put them in MasterPage but doing so prevent me to properly localize those elements.通常当我必须处理公共元素(页眉/页脚)时,我将它们放在 MasterPage 中,但这样做会阻止我正确地本地化这些元素。 So i am intereseted in understand how to template a page using a common base page that will be derived by all other pages (a sort of MasterPage).因此,我很想了解如何使用将由所有其他页面(一种 MasterPage)派生的公共基页来模板化页面。

I have found this article that explain clearly how to handle such situation, but i think is a bit outdated: https://www.codeproject.com/Articles/2915/ASP.NET-Page-Templates-Using-Inheritance我发现这篇文章清楚地解释了如何处理这种情况,但我认为它有点过时了: https://www.codeproject.com/Articles/2915/ASP.NET-Page-Templates-Using-Inheritance

So i would ask if there is a better approach or technology to achieve my goal.所以我会问是否有更好的方法或技术来实现我的目标。

Here a simple demo of how to send a variable to the Master Page.这里有一个简单的演示,说明如何将变量发送到母版页。 Put a public property in the Master named Language .在名为Language的 Master 中放置一个公共属性。

public partial class Site1 : System.Web.UI.MasterPage
{
    public string Language { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = $"My language is {Language}.";
    }
}

Then add a property for the Master in the normal page and cast Page.Master to your Master Page's partial class ( Site1 by default).然后在普通页面中为母版添加一个属性,并将Page.Master为母版页的部分 class(默认为Site1 )。

public partial class Default1 : System.Web.UI.Page
{
    public Site1 master;

    protected void Page_Load(object sender, EventArgs e)
    {
        master = (Site1)Page.Master;
    }

Then you can access Language in the Master from the Page.然后您可以从页面访问母版中的Language

master.Language = "nl-NL";

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

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