简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM