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"?>
    <CodeSnippet Format="1.0.0">
            <Title>Render parent control to string</Title>
            <Author>Waldek Mastykarz |</Author>
            <Code Language="CSharp">
                <![CDATA[StringBuilder sb = new StringBuilder();
            System.Web.UI.HtmlTextWriter htw =
                new System.Web.UI.HtmlTextWriter(new StringWriter(sb));
            string renderedOutput = sb.ToString();

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.


comments powered by Disqus