ContentPlaceHolders:重复内容

发布于 2024-07-18 07:30:24 字数 1413 浏览 5 评论 0原文

场景

我有一个使用 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 技术交流群。

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

发布评论

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

评论(5

抹茶夏天i‖ 2024-07-25 07:30:24

嘿,如果有人仍然需要解决方案,这就是我所做的:
将其放入母版页的代码文件中

protected override void RenderChildren(HtmlTextWriter writer)
{
    Control mainPlaceHolder = FindControl("MainContentPlaceHolder");
    Control doublePlaceHolder = FindControl("ContentDoublePlaceHolder");

    StringBuilder sb = new StringBuilder();

    using (StringWriter sw = new StringWriter(sb))
    using (HtmlTextWriter tw = new HtmlTextWriter(sw))
    {
        mainPlaceHolder.RenderControl(tw);
    }

    LiteralControl lc = new LiteralControl(sb.ToString());
    doublePlaceHolder.Controls.Add(lc);

    base.RenderChildren(writer);
}

我不确定这里的性能问题,但它确实有效。

Hey, if someone still needs a solution for this here is what I did:
Put it in the Code File of your master page

protected override void RenderChildren(HtmlTextWriter writer)
{
    Control mainPlaceHolder = FindControl("MainContentPlaceHolder");
    Control doublePlaceHolder = FindControl("ContentDoublePlaceHolder");

    StringBuilder sb = new StringBuilder();

    using (StringWriter sw = new StringWriter(sb))
    using (HtmlTextWriter tw = new HtmlTextWriter(sw))
    {
        mainPlaceHolder.RenderControl(tw);
    }

    LiteralControl lc = new LiteralControl(sb.ToString());
    doublePlaceHolder.Controls.Add(lc);

    base.RenderChildren(writer);
}

I'm not sure about performance issues here, but it certainly works.

云归处 2024-07-25 07:30:24

如果您只想重复一个字符串,这对我有用:

<asp:ContentPlaceHolder ID="TitleContent" runat="server" /> 

可以在其他地方引用为:

<%  Dim title As LiteralControl = TitleContent.Controls.Item(0)
    Response.Write(title.Text)
%>

但不保证这是正确的行动方针:)

In the case where you just want to repeat a string, this worked for me:

<asp:ContentPlaceHolder ID="TitleContent" runat="server" /> 

Can be referenced elsewhere as:

<%  Dim title As LiteralControl = TitleContent.Controls.Item(0)
    Response.Write(title.Text)
%>

No warranty on that being the right course of action though :)

巴黎盛开的樱花 2024-07-25 07:30:24

正如其他人所说,根据内容本身,可以根据内容的类型采取一些捷径。

然而,假设您需要“内容”是功能齐全的 ASP.NET 代码,那么我建议为每个包含内容本身的 ContentPage 使用一个 UserControl,然后您只需要复制一行代码。

示例

母版页

<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>

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

<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">
    <uc1:Page1Control id="page1Control1" runat="server" />
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
    <uc1:Page1Control id="page1Control2" runat="server" />
</asp:Content>
ゞ记忆︶ㄣ 2024-07-25 07:30:24

您可以为其创建一个通用用户控件,并将其添加到顶部和顶部的母版页中。 底部。 您可以创建一个属性来描述控件是在顶部还是底部加载。

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.

如若梦似彩虹 2024-07-25 07:30:24

根据内容,您也许可以在代码中执行此操作。 如果它只是 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.

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