在将 Page.Title 设置为 asp:contentplaceholder 的一部分后引用它
我在标题标签内设置了一个带有 contentplaceholder 控件的母版页,如下所示:
<head id="head1" runat="server">
<style type="text/css">
body { font-family: Tahoma; font-size: 9pt; }
</style>
<title><asp:contentplaceholder id="title" runat="server" /></title>
</head>
该 contentplaceholder 在使用该母版页的页面内实现,如下所示:
<asp:content runat="server" contentplaceholderid="title">
Welcome: <%= this.BasketID %>
</asp:content>
我试图在母版页正文中获取替换标题的副本(也在页面内尝试过 - 这也不起作用)例如:
<p>
<strong>Subject:</strong> <%# Page.Title %>
</p>
在所有情况下 Page.Title
和 Page.Header.Title
都是 ""
(我尝试过数据绑定和使用 <%= %>
语法,但均无济于事。
有谁知道这里发生了什么以及如何克服这个问题?
谢谢。
I've got a master page setup with a contentplaceholder control inside the title tag as so:
<head id="head1" runat="server">
<style type="text/css">
body { font-family: Tahoma; font-size: 9pt; }
</style>
<title><asp:contentplaceholder id="title" runat="server" /></title>
</head>
That contentplaceholder is implemented inside a page that uses that masterpage as so:
<asp:content runat="server" contentplaceholderid="title">
Welcome: <%= this.BasketID %>
</asp:content>
I'm trying to then get a copy of the substituted title inside the masterpage body (also tried inside the page - and this doesnt work either) like:
<p>
<strong>Subject:</strong> <%# Page.Title %>
</p>
In all cases Page.Title
and Page.Header.Title
are ""
(I've tried both databinding and using the <%= %>
syntax to no avail.
Does anyone know what is going on here and how I can overcome this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题是因为您“欺骗”了页面周期。您最好在页面的代码隐藏中使用它:
但是您可以这样做;在母版页上:创建一个 HtmlTextWriter,将其配置为写入 MemoryStream。将“标题”内容占位符渲染到 HtmlTextWriter,将 StreamReader 附加到流以获取字符串形式的内容,并将其输出到您的页面。然而,这效率不高,而且工作量太大:-)
The problem you're getting is because you are 'tricking' the page cycle. You'd better use this in the codebehind of the page:
You could do it this way however; on the masterpage: create a HtmlTextWriter, configure it to write to a MemoryStream. Render the 'title' contentplaceholder to the HtmlTextWriter, attach a StreamReader to the stream to grab the content as a string, and output this to your page. Yet, this is not efficient, and way too much work :-)