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

发布于 2024-09-12 08:53:05 字数 568 浏览 2 评论 0原文

我有一个 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#

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2024-09-19 08:53:05
    If(this.Parent.Page.Title == "something") 
    {
        Label.Text = "something"; 
    } 
        else if (this.Parent.Page.Title == "somethingElse")
    {
        label.Text = "somethingElse";
    }

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

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

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

    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.

对不⑦ 2024-09-19 08:53:05

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文