开发者

How to determine current Page.Title value from UserControl

I have a MasterPage, several content pages - each of the content pages have a 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.

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.

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:

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?

C# Please


    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.

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.Parent.Page.Title just gives you the Page's parent control which isn't necessarily the Master.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜