Andrew Connell [MVP SharePoint]
1728 Posts |  46 Articles |  5281 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
AC's Blog Quick Links
SharePoint Quick Links
Article Categories
Archives
February, 2012 (2)
January, 2012 (12)
December, 2011 (6)
November, 2011 (3)
October, 2011 (17)
September, 2011 (8)
August, 2011 (6)
July, 2011 (7)
June, 2011 (13)
May, 2011 (9)
April, 2011 (15)
March, 2011 (1)
February, 2011 (6)
January, 2011 (5)
December, 2010 (11)
November, 2010 (6)
October, 2010 (12)
September, 2010 (5)
August, 2010 (4)
July, 2010 (5)
June, 2010 (6)
May, 2010 (11)
April, 2010 (11)
March, 2010 (9)
February, 2010 (9)
January, 2010 (3)
December, 2009 (10)
November, 2009 (15)
October, 2009 (15)
September, 2009 (7)
August, 2009 (4)
July, 2009 (10)
June, 2009 (8)
May, 2009 (2)
April, 2009 (9)
March, 2009 (6)
February, 2009 (16)
January, 2009 (6)
December, 2008 (12)
November, 2008 (12)
October, 2008 (27)
September, 2008 (13)
August, 2008 (14)
July, 2008 (14)
June, 2008 (12)
May, 2008 (23)
April, 2008 (12)
March, 2008 (15)
February, 2008 (13)
January, 2008 (12)
December, 2007 (10)
November, 2007 (8)
October, 2007 (15)
September, 2007 (20)
August, 2007 (21)
July, 2007 (16)
June, 2007 (8)
May, 2007 (25)
April, 2007 (16)
March, 2007 (18)
February, 2007 (18)
January, 2007 (12)
December, 2006 (16)
November, 2006 (13)
October, 2006 (18)
September, 2006 (22)
August, 2006 (27)
July, 2006 (23)
June, 2006 (23)
May, 2006 (23)
April, 2006 (9)
March, 2006 (17)
February, 2006 (15)
January, 2006 (23)
December, 2005 (31)
November, 2005 (32)
October, 2005 (38)
September, 2005 (53)
August, 2005 (30)
July, 2005 (63)
June, 2005 (30)
May, 2005 (59)
April, 2005 (29)
March, 2005 (74)
February, 2005 (27)
January, 2005 (22)
December, 2004 (32)
November, 2004 (42)
October, 2004 (39)
September, 2004 (20)
August, 2004 (14)
July, 2004 (27)
June, 2004 (40)
May, 2004 (5)
April, 2004 (6)
March, 2004 (16)
February, 2004 (26)
January, 2004 (23)
December, 2003 (7)
November, 2003 (14)
October, 2003 (20)
September, 2003 (4)
Post Categories
Add to Technorati Favorites

Managed Windows Shared Hosting

SharePoint v3 was the huge benefactor of the ASP.NET team taking the Web Part framework and adding it to the core ASP.NET 2.0. Since SharePoint is not now built on top of ASP.NET 2.0, it no longer has its own Web Part implementation... instead it completely relies on the ASP.NET 2.0 framework. Yes, there is a WebPart class in the SharePoint assemblies, but we are not supposed to use it as it is only there for backwards compatibility; it makes Web Parts built in WSS v2 / SPS 2003 work in WSS v3 / MOSS 2007. Instead we should always create ASP.NET 2.0 Web Parts for SharePoint and that is the official recommendation by Microsoft.

When building custom Web Parts, you always hear people say "don't use Render(), only use RenderContents() or use the more OO approach and implement CreateChildControls()." Personally I favor the latter approach of using CreateChildControls(). So why should you not use Render()? Because unbeknownst to you, you are actually breaking something quite powerful.

We all know the browser DOM... but did you know SharePoint has its own DOM? It is called the Web Part Page Services Component, aka WPSC (no, I didn't forget a 'P', don't ask me why there is only one in the acronym :)). I wrote about this in one of my two chapters I contributed to a WSS v3 dev book. Basically the WPSC provides developers with a SharePoint specific client-side DOM that allows developers to listen for events, interact with Web Parts and even set properties... all through script on the client. The WPSC is implemented using a bunch of client-side code. You can see a snippet of it if you look at the source of a SharePoint page... for example, here's the very end of the homepage of the SharePoint Conference 2008 page (implemented using MOSS 2007 WCM... wahoo!):

WebPart2
Click the image for more sizing options as this is
obviously quite small to see everything. :)

See all those varPartWPQ# variables? Those are pointing to a specific Web Part in a client-side object. Using this object you can get values from some public properties of the Web Part... very cool!

But wait... there's more! There are also two other properties you can use to get access to the rendered Web Part. First, take a look at how ASP.NET renders Web Parts:

WebPart

Web Parts are rendered in an HTML <TABLE> with two rows, each row containing one cell (shown in red in the previous image). The top cell contains the header of the Web Part, the bottom cell contains a HTML <DIV> (shown in blue in the previous image) which contains the rendered Web Part. When SharePoint renders the page, it adds a unique ID to the HTML <TABLE> as well as a unique ID to the <DIV>... developers can use these ID's to get to the Web Part via client-side code. Great... so why did I just go through all that? Because when you use RenderContents() or CreateChildControls(), the rendered markup produced by those methods is put inside the <DIV>. If you override Render(), you are replacing the entire <TABLE>! Thus, by replacing the entire table, you are breaking the WPSC... the whole SharePoint specific DOM!

This is just one reason, albeit a big one, why we all say you should use RenderContents() or CreateChildControls()... you should never use Render() when creating Web Parts. Never never never use Render() (hey recruiters, this is a great interview question for prospective SharePoint developers)!

Let's switch gears and talk about accessibility. There are always those who believe table-based design is evil and pixel perfect design (ala CSS-based layout) is the way to go. I'm not stepping into that fray in this post. But let's take a look at the CSS based implementation. Some kits like the Accessibility Kit for SharePoint, a fantastic educational tool (note: it is not a turn-key solution), show how you can implement an accessible site using the CSS-layout approach. One thing they do is leverage control adapters to change the rendering of the stock ASP.NET 2.0 and SharePoint controls. However one that is missing is adapters for the Web Part and Web Part Zone controls.

Lots of people complain about this and go about building their own control adapter to replace the Web Part tables as CSS. That's all fine and good, but they are breaking the WPSC. Now... I fully acknowledge that not many people leverage the WPSC. Heck, I bet many people didn't even know it existed. If you elect to build your own control adapter to get rid of the HTML table, just know that you are implementing a solution to a problem, but by doing so, you are introducing a new problem. Granted, the new problem (a broken WPSC) may not outweigh the need for a CSS-based layout so it may be ok. Thus your mileage may very. At least you now know what you're doing.

Technorati Tags: ,,,
posted on Monday, February 18, 2008 11:50 AM

Feedback

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/18/2008 12:17 PM Madhur
Gravatar Great post .. Agree with everything .. apart from the interview thing ..

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/18/2008 3:46 PM Sherman Woo
Gravatar This is exactly what I had been thinking, but had been seeing examples all over the Internet overriding Render(). In fact, I had started a question on my blog about to ask, "what are people using?" My instincts said to use CreateChildControls() or RenderContents(). So I did... but still...

I cracked open some books:

1. Chapter 4 of "Inside WSS 3", Pattison/Larson writes about using CreateChildControls() and RenderContents() if necessary.

2. Chapter 10 of "Developer's Guide to WSS 3", Todd Bleeker says to override RenderContents().

3. Jan Tielens writes in Chapter 7 of "Real World SP2007" that one should use CreateChildControls() as well. (The first time I read this a while back, I got a bit confused b/c he has an example in there for overriding Render(), which is what I thought he suggested.)

So, there you go. Enough proof from the experts. Amazing what a few months away from the books will do; gives a bit of a fresh perpsective.

Time to delete that draft post...

- Sherm.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/18/2008 3:50 PM AC [MVP MOSS]
Gravatar Sherman-
So the #2 in your list... that is Bleeker's book, but what you read isn't from Bleeker. I wrote two of the three Web Part chapters in that book. :)

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/18/2008 5:29 PM Aaron Powell
Gravatar Thanks for the post AC, I've always wondered where the HTML table structure was generated from in the web part rendering engine and now I know.

Time to bring on some control adapters to make the HTML a bit more light weight (I don't use the WPSC so I'm not concerned about breaking it).

And also, thanks for the link to the conference page, I've been looking for an example of a MOSS generated page with Web Parts that has the HTML of a MOSS page so I can have a look at how you'd do an advanced layout with MOSS.

Although I wish I could view the master page, would make reverse engineering a whole lot easier :P

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/18/2008 10:57 PM clayton
Gravatar Hi AC

Very informative.

The link you provided
http://msdn2.microsoft.com/en-us/library/ms477066.aspx

states that WPSC only applies to web parts that derive from Microsoft.SharePoint.WebPartPages.WebPart and doesn't relate to System.Web.UI.WebControls.WebParts.WebPart.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/19/2008 6:02 AM Dirk
Gravatar We have just spent ages removing any unnessisary table tags and replacing them with divs. But now we have to put back in the bloat back in?

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/19/2008 11:02 AM Eric Shupps
Gravatar Dirk,

I don't think that's what AC is saying. If the need for fully-compliant CSS design in your implementation overrides the need to leverage (now or in the future) the WPSC, then so be it - but at least you're making an educated decision. This way, when you do try to implement client-side code down the road, you'll know why it's not working and you can fix it on a case-by-case basis.

I would also add that our work in tracing server control performance has shown that overriding Render() can have strange effects on component processing in the page lifecycle. In effect, it "breaks" the defined inheritance model wherein the page inokes the web part Render() method from it's own implementation of RenderChildControls() - these calls are processed in a particular order and overriding Render() directly can have unexpected results. It's best to let the page handle that method on it's own and use a downstream method - such as RenderContents() or CreateChildControls() (I favor the former but that's just me) - to render the required output.


# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/19/2008 12:16 PM Frode Magnussen
Gravatar I'm in the middle of reading Inside WSS 3 a second time, and this gave me a better understanding of using the CreateChildControls() and RenderContents() methods. Thanks AC.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/19/2008 2:58 PM JJ
Gravatar I would feel much worse about ruining the SharePoint-specific DOM if the SharePoint-specific DOM was more useful.
Unfortunately WPSC appears to be unavailable in non-IE browsers, and anyone who's concerned about accessiblity is likely programming cross-browser as well.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/19/2008 3:38 PM Buzza
Gravatar Whenever you override any method, you should always call its base class method, unless you know what that base class is doing and you know for a fact you done need that functionality.

So if this is done you would have an issue that you described above.


# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/19/2008 4:04 PM AC [MVP MOSS]
Gravatar Dirk-
Eric is spot on. I'm not saying you have to do it one way or the other... the point of this post was to simply educate you in what is going on when you remove all those tables for Web Parts. If you don't use the WPSC, or any products that leverage it, then is it really an issue for you? You could make the argument that it isn't.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/26/2008 5:33 AM Matthias Glubrecht
Gravatar Hi Andrew,
that was an interesting post, mainly because I had never heard about WPSC before ;-)
As Clayton stated, according to http://msdn2.microsoft.com/en-us/library/ms477066.aspx the WPSC applies only to Parts derived from Microsoft.SharePoint.WebPartPages.WebPart.
Since this base class is only provided for backwards compatibility reasons (as you wrote), it seems to me that it is not a good idea to use WPSC at all and hence it wouldn't matter if you break it by overriding Render().
By the way, will Microsoft.SharePoint.WebPartPages.WebPart be removed in later versions of WSS? I can't really imagine that.
On the other hand I would suggest to _never_ override Render() for the sake of compliance with the framework.
For example, any accessibility enhancement could target the base classes used by SharePoint and I would expect it to change the complete page structure and thus also remove the TABLE layout surrounding Web Parts. Render() should be a private method of the WebPart class.
Any thoughts on the future of WPSC or Microsoft.SharePoint.WebPartPages.WebPart?
Regards, Matthias

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/27/2008 11:55 AM AC [MVP MOSS]
Gravatar Mathias-
This is true... only Web Parts that use the SharePoint Web Part class affect the WPSC, but you still shouldn't use Render() :)

# WebParts, Micrososft and dreadful HTML 4/7/2008 9:40 AM Ric
Gravatar While I take your point regarding breaking the WPSC, the basic fact remains that in this day and age and with the understand we all have about accessibility, there is absolutely no excuse for Microsoft to build the HTML the way they have. I am no MS basher, but they have consistently written truly dreadful client side code/markup; whether that be HTML, CSS or JavaScript (even their own JScript examples are stunningly poor).

In a test example recently, I dropped a web part (based on a ASCX) into a WebPartZone and it managed to generate 4 (yes, four) levels of nested tables. There is not a single justification that can be given for that.

Ric.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 11/21/2008 9:23 PM Wal
Gravatar The article was great (atleast for me). I did get some good information out of this.


Thank you

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 1/3/2009 12:43 AM Praful Goyal
Gravatar We have a custom webpart on 100 pages. Now we renamed this webpart dll and changed publickeytokan in safecontrol entry but we kept webpart same name. After deleting old webpart from galary and adding new one, its throwing error on all pages. Is any way to do this? I know one way, go to each page and add new webpart, that will work. Any other way?

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 1/3/2009 8:02 AM AC [MVP MOSS]
Gravatar Praful-
You changed two of the five components in the strong name that makes it unique so all your current references are no longer valid. You'll either need to replace all instances on your website, or you could write something that gets an instance of the Limited Web Part Manager on each page and does it for you. Not a trivial task though...

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 1/3/2009 1:05 PM Praful Goyal
Gravatar Hi AC: I have a custom page layout with custom webpart and many pages created out of it. Now I am deleting this custom webpart and adding one new custom webpart but it is not reflecting into my existing pages. Though if I create a new page I can see the change. What is reason of that.


# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 1/4/2009 8:56 AM AC [MVP MOSS]
Gravatar Praful-
That's because Web Part stuff is not saved with the page, it's saved in the personlization store. Thus, when a page is created, if uses the Web Parts in the page layout as a starting place. But going forward, there's zero reference to the Web Parts in the page layouts. You'll need to make changes on every single page.

Suggest you read this: http://andrewconnell.com/blog/archive/2008/10/09/Publishing-sites-field-controls-or-Web-Parts.-that-is-the.aspx

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 1/20/2009 11:03 AM Andreas Grabner
Gravatar Great article.
I've seen customers rendering their own html in the Render method. The problem that we see as that many use simple string concatenations instead of string buffers. This causing a lot of small object allocations which results in more frequent GC runs.
WebParts are a great thing - especially since SharePoint uses the ASP.NET WebParts that allows you to also use it in non-sharepoint environments.

In terms of performance I just created a series of blog entries that cover custom web part development and how those custom webparts should be careful when accessing the underlying SharePoint Object Model

check it out here: http://blog.dynatrace.com/category/net/sharepoint-net/

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/1/2012 7:53 AM Rikard Uppström
Gravatar Great post!

In my project we elected to implement a controladapter to get rid of all the nasty tables that the webpartzone produces.

This essentially means, as you state, that the WPSC is broken, so my question is:
Do you know if there is some way to disable the WPSC, never having to render the javascript at all? This would also make the pages quite a bit lighter, which is always nice..

We are using SharePoint 2010.

# re: Understanding how Web Parts are rendered, why to never use Render() and a bit on accessibility 2/1/2012 8:39 AM AC [MVP SharePoint]
Gravatar @Rikard - Personally I don't think that's a good approach as you might be ok for 2007, since i wrote this SP2010 introduce the CSOM which has a way of looking at the WP's on the page. Thus you're breaking that as well.

To answer your question, no you can't disable the JS.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 2 and 8 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2012 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
SharePoint 2010 Training
Looking for SharePoint 2010 training for developers, administrators, SharePoint Designer 2010 and end users? Look no further! My company, Critical Path Training offers the best SharePoint training around!