Feed Icon  

Contact

  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics

Tags

Open Source Projects

Archives

Ads

Rendering Regret

Posted in Reporting Services | SharePoint at Tuesday, February 1, 2005 2:42 AM Pacific Standard Time

From Barry's Blog via Serge van den Oever on RenderWebPart:

When things go the way you expect, you want to render an entire batch of HTML. However, you don't want to render any of that HTML in the case of an exception when the logic in your Web Part determines that it should display an error message instead. Things can really get ugly if you render the opening tags for an HTML table and do not properly close them due to an exception. This can upset the high-level rendering logic of the page as well as other Web Parts.

The solution to this problem is to use some technique that allows you to write HTML into a buffer and then send it all in a single batch once you know that all the rendering logic has executed successfully. You can create a string buffer using an instance of the System.Text.StringBuffer class. You can then create an instance of the HtmlTextWriter class and use that to render HTML into the buffer.

Barry then gives some code that will allow you to buffer your HTML output which lets you manage your exceptions gracefully. I liked the idea so much that I've already added it to the latest version of the RsWebParts (version 1.2 which I'm still trying to tidy up for release on sf.net). Since the RsWebParts all inherit from the RsBasePart I simply did this:

        /// 
        /// Renders the HTML contents of the webpart. 
        /// The base webparts calls this and buffers the output in order to 
        /// prevent the webpart causing the SharePoint page to fail. This code
        /// was take from Barry's Blog (http://www.barracuda.net/barrysblog.aspx?Date=10/22/2004)
        /// 
        /// HtmlTextWriter to writer content with.
        protected override void RenderWebPart(HtmlTextWriter output) 
        { 
            // create buffer for output 
            StringBuilder buffer = new StringBuilder(10240);
            StringWriter InnerWriter = new StringWriter(buffer);
            HtmlTextWriter BufferWriter = new HtmlTextWriter(InnerWriter); 
            try 
            { 
                // call method to write HTML to buffer
                WriteWebPartContent(BufferWriter);
                // write HTML btach back to browser
                output.Write(buffer);
            }
            catch(Exception ex) 
            { 
                output.Write("Web Part Error: " + ex.Message); 
            } 
        }
        /// 
        /// The webpart must call this method to render its content instead of
        /// overriding RenderWebPart.
        /// 
        /// HtmlTextWriter to writer content with.
        protected abstract void WriteWebPartContent(HtmlTextWriter output);

Next I just renamed the RenderWebPart methods of each web part to WriteWebPartContent and now I have buffered output! Very cool.

Barry has some other great articles on his blog and his company offers SharePoint training. My boss and one of my coworkers took the class and both learned quite a bit (I didn't take the class because I'm not really the SharePoint admin, just a web part developer).

Comments are closed.