从母版页上的用户控件获取子页标题

发布于 2024-12-10 18:06:15 字数 67 浏览 0 评论 0原文

我在母版页中有一个用户控件。在该 ascx 后面的代码中,我想获取页面的标题。标题设置在标题部分,并在子页面中设置标签。

I have a user control in the Master page. In the code behind of that ascx, I want to get the title of the page. The title is being set in the head section with tag in the child pages.

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

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

发布评论

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

评论(3

蓦然回首 2024-12-17 18:06:15

试试这个

var obj = this.Parent.Page;
var title= obj.Title;

Try this

var obj = this.Parent.Page;
var title= obj.Title;
胡大本事 2024-12-17 18:06:15

您尝试过Page.Title吗?

但是,我认为 head 标签需要在服务器端运行才能使用它

Have you tried Page.Title?

However, I think the head tag needs to be ran server side to use this

清引 2024-12-17 18:06:15

我认为 Page.Title 就足够了。如果没有,您必须稍微浏览一下对象模型,直到到达该页面。上面已经详细介绍了这两种解决方案。唯一的变化可能是 ascx 是在母版页而不是页面上设置的。最糟糕的情况是在母版页中获取标题并在页面呈现时将其提供给 ascx。

现在,了解为什么这会变得有点令人困惑。大多数人认为该页面位于母版页上。但是,从技术上讲,母版页被设置为页面上的控件。这主要是为了避免在引入母版页时对 ASP.NET 进行彻底的重新架构。这意味着页面已被请求并开始呈现。然后母版页链接标签被点击,“控件”被渲染,等等。在某些情况下,微软提供了简单的捷径,在其他情况下,你必须导航,而导航与许多人的期望是颠倒的。

I would think Page.Title would be enough. If not, you have to ride up through the object model a bit until you get to the page. Both solutions have been detailed above. The only variation may be if the ascx is set on the master page rather than the page. Worst case here is getting the title in the master page and feeding to the ascx as the page is rendered.

Now, an understanding of why this gets a bit confusing. Most people think the page sits on the master page. But, technically, the master page is set up as a control on the page. This is largely to avoid a complete rearchitecture of ASP.NET as master pages were introduced. This means the page is requested and starts to render. Then the master page linked tags are hit and that "control" is rendered, etc. In some cases, Microsoft has provided easy short cuts, in others, you have to navigate and the navigation is upside down from many people's expectation.

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