代码之家  ›  专栏  ›  技术社区  ›  Doug

如何从UserControl确定当前页标题值

  •  0
  • Doug  · 技术社区  · 15 年前

    我有一个母版页,几个内容页-每个内容页都有一个用户控件。

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

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

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

    但是,使用此代码时,我的标签仍为空。我需要用什么方法来确定当前页面。母版页设置的标题?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Chase Florell    15 年前
        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

        2
  •  0
  •   Eric H    15 年前

    this.Master.Page.Title 应该给你的母版页的标题,你可以测试和设置为任何你喜欢的。

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