Render parent control to string snippet


While working with Control Adapters in either ASP.NET or SharePoint 2007 you will quite often want to intercept the output of the parent control and modify it before rendering. As retrieving the output is the same for each and every control I have created the following code snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Render parent control to string</Title>
            <Author>Waldek Mastykarz | https://blog.mastykarz.nl</Author>
            <Description></Description>
            <Shortcut>render</Shortcut>
        </Header>
        <Snippet>
            <Code Language="CSharp">
                <![CDATA[StringBuilder sb = new StringBuilder();
            System.Web.UI.HtmlTextWriter htw =
                new System.Web.UI.HtmlTextWriter(new StringWriter(sb));
            base.Render(htw);
            string renderedOutput = sb.ToString();
            
            $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

To configure the snippet, simply create a file called render.snippet in the %Documents%\Visual Studio 2008\Code Snippets directory and paste the XML above. You can use the snippet by typing render and hitting tab twice.

Others found also helpful: