asp.net – Is there a simple way to have multiple ContentPlaceHolder controls update independently of each other? – Education Career Blog

This is a very simple ASP.NET master page example. The master page displays 4 hyperlinks and has two ContentPlaceholder controls. The first two links are to content pages that will display in ContentPlaceHolder1, the second two links are to content pages that will display in ContentPlaceHolder2.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="ProofOfConcept.Site1"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>
        Demo site
    </h1>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/HeadContent1.aspx">Head Content 1 in ContentPlaceHolder1</asp:HyperLink>
    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/HeadContent2.aspx">Head Content 2 in ContentPlaceHolder1</asp:HyperLink>
    <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/MainContent1.aspx">Main Content 1 in ContentPlaceHolder2</asp:HyperLink>
    <asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/MainContent2.aspx">Main Content 2 in ContentPlaceHolder2</asp:HyperLink>
    <br />
    <div style="border: 1px dotted blue;">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            This is default text for ContentPlaceholder1
        </asp:ContentPlaceHolder>
    </div>
    <br />
    <div style="border: 1px dotted red;">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
            This is default text for ContentPlaceholder2
        </asp:ContentPlaceHolder>
    </div>
    </div>
    </form>
</body>
</html>

The four content pages themselves all look like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.master" AutoEventWireup="true" CodeBehind="MainContent1.aspx.cs" Inherits="ProofOfConcept.MainContent1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceholder2" runat="server">
    This is text from MainContent1 in ContentPlaceholder2
</asp:Content>

…with the ContentPlaceholder ID setas appropriate. In other words, each content page only contains one Content control linked to the one of the ContentPlaceHolders on the Master page.

If I build the site and load HeadContent1.aspx (the first link), for instance, only the content from HeadContent1 is displayed (plus markup from the master page, obviously). If I click the third link, content in the second ContentPlaceHolder is displayed, but the first Placeholder reverts to its default markup.

This behaviour all appears to be as designed and is undoubtedly very useful in many scenarios, but what I’d like to do is have the two ContentPlaceholders refresh independently of each other. To work like old-fashioned HTML frames, more or less? Is this possible, or should I be using some other control (or a different setup instead of Master/Content)?

,

I would say you want to use iframes for your situation. Master pages don’t function the way you are looking for.

,

I agree with Mike. You are going the long way around. In order for your example to work with Master Pages, you will need 16 different content pages. Each page would need to fill the content for PlaceHolder1 and PlaceHolder2 depending on which links were clicked and in what order.

An IFrame would be a better way to handle something like this, but I would first recommend you review your design to determine if there is a way that it can be simplified.

Leave a Comment