Search code examples
c#asp.nethtmlhtmltextwriter

HTMLTextWriter to render elements in the middle of the page


Using asp .net and C# I've managed to use a HTMLTextWriter in my override Render method to render two divs. But they appear at the end of the page, how can I choose where to render these divs if I needed them to appear in a particular asp panel for example?

Thanks a lot,

Dan

EDIT: My bit of code:

    protected override void Render(HtmlTextWriter writer)
    {
        StringWriter stringWriter = new StringWriter();
        base.Render(writer);
        using (HtmlTextWriter writer2 = new HtmlTextWriter(stringWriter))
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "testDiv");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
        }
    }

Solution

  • I think you´ll need to use a HTML parser like Majestic. You can render your page into a memorystream and then manipulate it as you want.

    If the only problem is to customize the panel rendering, you can write a control that inherits asp:panel and inserts the desired classes.