Search code examples
c#.netsharepointuser-controlsweb-parts

Render WebPart in a UserControl without containing DIV


I am building a usercontrol to place webparts in my pagelayouts.

What i'm basicly doing:

<uc:WebPartInclude WPName="WebPartName" runat="server"</uc:WebPartInclude>

And this all works fine, except for one thing: It now surrounds the content of the webpart with <div> ... </div>.

Since i'm a sucker for clean code, this div has got to go ^_^

public class WebPartInclude : Control
{
    public string WPName = "";
    private WebControl webPartControl = null;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        webPartControl = (WebControl)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("full path..." + WPName);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        base.RenderChildren (writer);
    }
    protected override void CreateChildControls()
    {
        if (webPartControl != null)
            Controls.Add(webPartControl);
        ChildControlsCreated = true;
    }

    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return base.Controls;
        }
    }
}

So the webpart renders the following:

<div>
    ... webpart contents
</div>

And i want the surrounding divs to go, got any idea?


Solution

  • Well, since there was no way of fixing it in a nice way, i just used the HtmlTextWriter to write the contents to a string and strip the surrounding <div>...</div>.

    Problem solved, just not in a nice way.