简体   繁体   English

如何从UserControl确定当前的Page.Title值

[英]How to determine current Page.Title value from UserControl

I have a MasterPage, several content pages - each of the content pages have a UserControl. 我有一个MasterPage,几个内容页面-每个内容页面都有一个UserControl。

In the MasterPage, I'm setting the Page.Title programmatically based on Page_Init "Request.UserAgent.Contains....", so that the content pages end up with different page titles based on this. 在MasterPage中,我将基于Page_Init“ Request.UserAgent.Contains ....”以编程方式设置Page.Title,以便基于此内容页面以不同的页面标题结尾。

Each content page has its Title set to blank (Title=" "), but when the page renders, the pragmatically generated page title shows up in the browser tab. 每个内容页面的标题设置为空白(Title =“”),但是当页面呈现时,实用生成的页面标题会显示在浏览器选项卡中。

In the UserControl code behind, I need to access the content page's (parent page) Page.Title value, and base on this, display some text: 在后面的UserControl代码中,我需要访问内容页面(父页面)的Page.Title值,并在此基础上显示一些文本:

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

However, my label remains blank when using this code. 但是,使用此代码时,我的标签仍为空白。 What is the way I need to determine the current Page.Title to what the MasterPage has set it as? 确定当前Page.Title的方法是什么,以便将MasterPage设置为它?

C# Please C#请

    If(this.Parent.Page.Title == "something") 
    {
        Label.Text = "something"; 
    } 
        else if (this.Parent.Page.Title == "somethingElse")
    {
        label.Text = "somethingElse";
    }

you can also use this.Master.Page.Title if you are ALWAYS setting the title in the masterpage. 如果您总是在this.Master.Page.Title中设置标题,则也可以使用this.Master.Page.Title

Also, remember that every page as a Life Cycle . 另外,请记住,每个页面都是生命周期 If you are checking for the title before the title is set, then it will return a blank string. 如果要在设置标题之前检查标题,则它将返回一个空白字符串。

To test the code, try hard-coding a title in the master page and see if your code is working. 要测试代码,请尝试在母版页中对标题进行硬编码,然后查看代码是否正常工作。 Once you know the code is in fact pulling the title from the parent, then you need to make sure that you are setting the page title BEFORE you try and poll for it. 一旦知道代码实际上是从父级获取标题,那么在尝试轮询之前,需要确保已设置页面标题。

this.Master.Page.Title should give you the Master Page's title which you can test and set to whatever you like. this.Master.Page.Title应该为您提供母版页的标题,您可以对其进行测试并将其设置为所需的任何内容。

this.Parent.Page.Title just gives you the Page's parent control which isn't necessarily the Master. this.Parent.Page.Title只是为您提供Page的父控件,不一定是Master。

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

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