【问题标题】:Get Public Content Page Variable into Masterpage将公共内容页面变量获取到 Masterpage
【发布时间】:2015-01-09 16:18:12
【问题描述】:

我的内容页面中有一个公共变量,我需要在我的 MasterPage 中访问它。所以我可以设置一个 Javascript 变量...。

如何从母版页引用公共内容页面变量?

【问题讨论】:

标签: c# master-pages content-pages


【解决方案1】:

我想你想说你想从竞争页面访问 MasterPage 中的变量,如果正确,使用这个例子:

声明您的公共或受保护变量:

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

{
    public string strEmpresa = "NS";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

在内容页面的开头设置以下指令:

<%@ MasterType  virtualPath="~/MasterPage.Master"%>

然后您可以使用 MasterPage 的公共变量,使用 Master.NameVariable。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox1.Text = Master.strEmpresa;
        }
    }

如果你真的想从 MasterPage 访问 ContentPage 中的变量,你可以在 Session 中设置值,然后在 MasterPage 中读取。例如:

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

        if (!IsPostBack)
        {
            if (Session["myVariable"] != null)
            {
                TextBox1.Text = Session["myVariable"].ToString();
            }
        }
    }
}

 public partial class WebFormMP_TestPublicVariable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["myVariable"] = "Test";
        }
    }

}

有很多方法可以实现这一目标。检查互联网;)。

【讨论】:

  • 你也可以使用&lt;%@ MasterType TypeName="(type)" %&gt;
  • 您可能要提到,此方法不会从母版页访问内容页,而是允许内容页访问母版页成员。根据情况,这可能不适用于操作。
  • 是的,你好吧,我猜他想说这个。因为其他情况看起来很容易回答。我将编辑提到谢谢的帖子。
猜你喜欢
  • 1970-01-01
  • 2020-06-15
  • 2011-05-12
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
相关资源
最近更新 更多