ContentPlaceHolders:重复内容
场景
我有一个使用 asp.net 母版页的应用程序,我想在其中重复页面顶部和底部的一些内容。 目前我使用这样的东西:
Master Page<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Bar" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content -->
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
<!-- content repeated -->
</asp:Content>
维护
如您所知,在代码中重复事物通常不好。 它会产生维护问题。 以下是我想做的事情,但由于重复的 id 属性,显然不会起作用:
Master Page<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content (no repetition) -->
</asp:Content>
可能吗?
有没有办法使用 ASP.NET WebForms 来做到这一点? 解决方案不一定必须与上述内容相似,只需以相同的方式工作即可。
注释
我在 Visual Studio 2008 中使用 asp.net 3.0
Scenario
I have an application using asp.net Master Pages in which I would like to repeat some content at the top and bottom of a page. Currently i use something like this:
Master Page
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Bar" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content -->
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
<!-- content repeated -->
</asp:Content>
Maintenance
As you know, repeating things in code is usually not good. It creates maintenance problems. The following is what I would like to do but will obviously not work because of the repeated id attribute:
Master Page
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content (no repetition) -->
</asp:Content>
Possible?
Is there a way to do this using asp.net webforms? The solution does not necessarily have to resemble the above content, it just needs to work the same way.
Notes
I am using asp.net 3.0 in Visual Studio 2008
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嘿,如果有人仍然需要解决方案,这就是我所做的:
将其放入母版页的代码文件中
我不确定这里的性能问题,但它确实有效。
Hey, if someone still needs a solution for this here is what I did:
Put it in the Code File of your master page
I'm not sure about performance issues here, but it certainly works.
如果您只想重复一个字符串,这对我有用:
可以在其他地方引用为:
但不保证这是正确的行动方针:)
In the case where you just want to repeat a string, this worked for me:
Can be referenced elsewhere as:
No warranty on that being the right course of action though :)
正如其他人所说,根据内容本身,可以根据内容的类型采取一些捷径。
然而,假设您需要“内容”是功能齐全的 ASP.NET 代码,那么我建议为每个包含内容本身的 ContentPage 使用一个 UserControl,然后您只需要复制一行代码。
示例
母版页
内容页
As others have said, depending on the content itself there are shortcuts that can be taken depending on the type of content that will be there.
Assuming however you need the "content" to be fully functioning asp.net code then I would suggest a UserControl for each ContentPage that contains the content itself and then you only need to duplicate one line of code.
Example
Master Page
Content Page
您可以为其创建一个通用用户控件,并将其添加到顶部和顶部的母版页中。 底部。 您可以创建一个属性来描述控件是在顶部还是底部加载。
You can create a COMMON user control for the same and add it to your master page in the top & bottom. You can create a property describing whether the control has loaded at the top or bottom.
根据内容,您也许可以在代码中执行此操作。 如果它只是 HTML,只需将其放入一个变量中并将其分配给另一个变量。 如果您有控件,则可以循环遍历控件集合并创建一个重复控件并将其放置在其他区域中。 在任何一种情况下,第二个位置都不是 div 应有的 contentplaceholder。
Depending on the content you may be able to do this in code. If its just HTML just put it in a variable and assign it to the other. If you have controls, you can loop over the controls collection and create a duplicate control and place it in the other area. In either case the second location would not be a contentplaceholder a div should due.