ContentPlaceHolders:重复内容 [英] ContentPlaceHolders: Repeated Content
问题描述
我使用asp.net母版页中,我想重复在页面的顶部和底部的一些内容的应用程序。目前我使用这样的:
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:
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Bar" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
内容页
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content -->
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
<!-- content repeated -->
</asp:Content>
维护
如你所知,在code重复的东西通常不是很好。它创建维护问题。下面是我想什么做的,但显然不会因为反复id属性的工作:
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:
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
内容页
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content (no repetition) -->
</asp:Content>
可能?
有没有办法做到这一点使用asp.net web表单?该解决方案并不一定必须类似于以上内容,它只需要相同的方式工作。
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.
我在Visual Studio 2008中使用asp.net 3.0
I am using asp.net 3.0 in Visual Studio 2008
推荐答案
正如其他人所说,这取决于内容本身有可以取决于将在那里的内容的类型采取的捷径。
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.
但是假设你所需要的内容是全功能的asp.net code,那么我会建议为包含内容本身每一个ContentPage用户控件,然后你只需要复制code的一行
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.
示例
母版页
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Bar" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
内容页
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<uc1:Page1Control id="page1Control1" runat="server" />
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
<uc1:Page1Control id="page1Control2" runat="server" />
</asp:Content>
这篇关于ContentPlaceHolders:重复内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!