从母版页上的用户控件获取子页标题
我在母版页中有一个用户控件。在该 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
您尝试过
Page.Title
吗?但是,我认为 head 标签需要在服务器端运行才能使用它
Have you tried
Page.Title
?However, I think the head tag needs to be ran server side to use this
我认为 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.