White space and line breaks inside the ContentPlaceholder control on a Master page (via a Content tag on a regular page) are converted to a System.Web.UI.LiteralControl control. So if you've got a single control inside a Content tag and plan on iterating through it, expect to find more than one control. Be sure to check the type or ID to know you've got the right control.

The Placeholder tag, on the other hand, appears to ignore white space (spaces, line breaks, tabs), and when you iterate through it, you only get actual server controls inside the placeholder. (Although if there is a word or character, not only will it be inserted inside a LiteralControl, but that control will also include all of the adjacent white space, line breaks, etc.)

This came up when building the IsControlEmpty function to help know when I could hide a fixed page header ContentPlaceholder on a master page if no content was added to it. I found that multiple System.Web.UI.LiteralControl were created, each with nothing but spaces, tabs, and line breaks.

Any plain text, spaces, or line breaks inside the Content tag that is not inside a server tag will be thrown into a LiteralControl. If text/linebreaks appear before and after a server tag inside a Content tag, it will be added to two separate LiteralControl tags.

Example:

<asp:Content ID="StickyHeader" ContentPlaceHolderID="FixedPageHeader" runat="server">
    <asp:Button ID="btnTest" runat="server" Text="Click Me" />
    <asp:Literal ID="litTest" runat="server"></asp:Literal>
    test text
</asp:Content>

When you iterate through the list of controls inside FixedPageHeader, you will see 5:

System.Web.UI.LiteralControl with Text="\r\n    "  (line break after opening Content tag then 4 spaces before button)
System.Web.UI.WebControls.Button with Text="Click Me"
System.Web.UI.LiteralControl with Text="\r\n    "  (line break after button then 4 spaces before empty literal
System.Web.UI.WebControls.Literal with Text=""     (empty literal)
System.Web.UI.LiteralControl with Text="\r\n    test text\r\n"   (line break after empty literal, 4 spaces before text, then line break after)
Published On: February 6, 2019Categories: ASP.NetTags: ,