【问题标题】:How to determine current Page.Title value from UserControl如何从 UserControl 确定当前 Page.Title 值
【发布时间】:2010-08-02 22:19:36
【问题描述】:

我有一个 MasterPage,几个内容页面 - 每个内容页面都有一个 UserControl。

在 MasterPage 中,我基于 Page_Init "Request.UserAgent.Contains...." 以编程方式设置 Page.Title,以便内容页面最终具有基于此的不同页面标题。

每个内容页面的标题设置为空白 (Title=" "),但是当页面呈现时,实际生成的页面标题会显示在浏览器选项卡中。

在后面的UserControl代码中,我需要访问内容页面(父页面)的Page.Title值,并在此基础上显示一些文字:

If(Page.Title == "something") 
{
Lable.Text = "something"; 
} 
else if (Page.Title == "somethingElse")
{
lable.Text = "somethingElse";
}

但是,使用此代码时,我的标签仍为空白。我需要以什么方式确定当前 Page.Title 到 MasterPage 设置的内容?

请使用 C#

【问题讨论】:

  • 你使用的是asp.net表单还是mvc

标签: c# asp.net user-controls master-pages


【解决方案1】:
    If(this.Parent.Page.Title == "something") 
    {
        Label.Text = "something"; 
    } 
        else if (this.Parent.Page.Title == "somethingElse")
    {
        label.Text = "somethingElse";
    }

如果您总是在母版页中设置标题,也可以使用this.Master.Page.Title

另外,请记住每一页都是Life Cycle。如果您在设置标题之前检查标题,那么它将返回一个空白字符串。

要测试代码,请尝试在母版页中对标题进行硬编码,然后查看您的代码是否正常工作。一旦您知道代码实际上是从父级中提取标题,那么您需要确保在尝试和轮询之前设置页面标题。

【讨论】:

  • 您好 rockinthesixstring,标题仍为空白。还有其他想法吗?谢谢,道格
  • 请看我的编辑。您需要查看页面生命周期 | msdn.microsoft.com/en-us/library/ms178472.aspx
  • 再次感谢,看看你的生命周期链接。我更改了我的母版页代码,并且还使用了 this.Master.Page.Title。现在一切正常。生命周期链接也很有帮助。
【解决方案2】:

this.Master.Page.Title 应该为您提供母版页的标题,您可以对其进行测试并设置为您喜欢的任何内容。

this.Parent.Page.Title 只是为您提供页面的父控件,不一定是主控件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2011-06-29
    • 2010-12-16
    • 2011-06-05
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多