she swears <i>geek</i> is a term of endearment

Adding Embedded ascx Content a Second Time

Nothing like self-correction.  Following up on this post

I tried something else.  It also has html dependencies but its less icky than the "endMarker"

In the ascx file

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ChildRenderer.ascx.cs" Inherits="MvcTestApp.Views.ChildRenderer" %>
<asp:PlaceHolder ID="ContentRoot" runat="server">
    I am the top stuff
    <div>
    <asp:PlaceHolder ID="ChildContent" runat="server"></asp:PlaceHolder>
    </div>
    I am the bottom
</asp:PlaceHolder>

and the ascx.cs file

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
 
namespace MvcTestApp.Views
{
    [ParseChildren( false )] 
    public partial class ChildRenderer : System.Web.Mvc.ViewUserControl
    {
        protected override void AddParsedSubObject( object obj )
        {
            if( obj is Control && ( obj as Control ).ID == "ContentRoot" )
            {
                base.AddParsedSubObject( obj );
            }
            else 
            {
                this.ChildContent.Controls.Add( obj as Control );
            }
            
        }
    }
}

While not perfect, it is pretty darn easy and takes very little code to accomplish.

Leave a Reply