简体   繁体   English

如何在主页面上声明的内容页面的代码后面重新定义变量?

[英]How to redefine a variable on content page's codebehind that was declared on master page?

I am building a web application using VS2010 and ASP.NET 4 我正在使用VS2010和ASP.NET 4构建Web应用程序

I have managed to declare and define a variable in a nested master page and call it within the master page. 我设法在嵌套的母版页中声明和定义一个变量,然后在母版页中调用它。 However, how do I change the value of that variable from a content page's codebehind ? 但是, 如何从内容页面的代码隐藏中更改该变量的值

Here are the variable declarations from DeckNav.master.cs (nested master page codebehind): 这是DeckNav.master.cs的变量声明(位于嵌套主页面代码后面):

public partial class DeckNav : System.Web.UI.MasterPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        this.mfrName = "(BRAND)";
        this.modelName = "(MODEL)";
    }

    public string mfrName { get; set; }
    public string modelName { get; set; }
}

This is how I call the 2 variables (default values) in master page (DeckNav.master): 这就是我在母版页(DeckNav.master)中调用2个变量(默认值)的方式:

<asp:ContentPlaceHolder ID="brandAndModel" runat="server">
<h1 class="page-header"> <%= mfrName %> <%= modelName %>
</h1>
</asp:ContentPlaceHolder>

In the content page inherited from DeckNav.master, I need to know how to change the values of the variables accordingly for each ASPX content page from the codebehind. 在从DeckNav.master继承的内容页面中,我需要知道如何从背后的代码中为每个ASPX内容页面相应地更改变量的值。

Example: 例:

public partial class DeckRepair : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        mfrName = "Brand 10";
        modelName = "Model 12";          
    }

}

The snippet above does not work. 上面的代码段无效。 Visual Studio alerts me that "The name does not exist in the current context". Visual Studio提醒我“该名称在当前上下文中不存在”。


EDIT: 编辑:

I declared the variables as static on the master page's codebehind to: 我在主页的代码背后将变量声明为静态变量:

public partial class LeftNav : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static string camMfr { get; set; }
    public static string camModel { get; set; }
}

... and assigned values on the content page's codebehind: ...,并在内容页面的代码后面分配值:

public partial class BrandModel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LeftNav.camMfr = "BRAND 11";
        LeftNav.camModel = "MODEL 11";
    }
}

I'm still having the same problem. 我仍然有同样的问题。 It successfully assigns the variable the first time to "BRAND 11", but when I load another content page with its own unique values for camMfr and camModel (ie "BRAND 22" , "MODEL 22") it retains the original assigned value ("BRAND 11"). 它首次成功将变量分配给“ BRAND 11”,但是当我加载另一个具有其自己的camMfr和camModel唯一值的内容页面(即“ BRAND 22”,“ MODEL 22”)时,它将保留原始分配的值(“品牌11”)。 Keep in mind I am NOT assigning a value in the master page, only declaring it. 请记住,我没有在母版页中分配值,而只是声明了它。

Try 尝试

DeckNav.mfrName = "Brand 10";
DeckNav.modelName = "Model 12";

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

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