【问题标题】:Accessing control on page with master page from user control从用户控件访问具有母版页的页面上的控件
【发布时间】:2013-10-05 21:46:09
【问题描述】:

当从不是用母版页制作的用户控件访问父页面上的控件时,我所要做的就是:

Page sample = this.Page;

我可以访问父页面及其上的控件。但是,当该页面是使用母版页制作时,相同的代码不起作用,并且该控件出现 null 异常。

这里需要改变什么?

【问题讨论】:

  • 你试过this.Master还是this.Page.Master

标签: c# asp.net visual-studio web


【解决方案1】:

在你的主代码后面

    public partial class SiteMaster : MasterPage
    {
        public string PropertyInMaster { get; set; }

        protected void Page_Init(object sender, EventArgs e)
        {
            PropertyInMaster = "test";
...

在您的用户控件中:

protected void Page_Load(object sender, EventArgs e)
{
    var mst = this.Page.Master as SiteMaster;

    Response.Write(mst.PropertyInMaster);

...

您还可以对您的母版内容占位符以及其中的控件进行查找控制; mst.FindControl("ContentPlaceHolder1").FindControl("MyTextBox")...

【讨论】:

  • 这很有帮助。谢谢。
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
相关资源
最近更新 更多