Dynamically adding WebParts using WebPartManagerInternals class
January 10, 2008I’m currently working on a project the requires the use of WebParts to allow a user to construct custom reports by dragging and dropping WebParts into WebPartZones. I needed to dynamically create these WebParts on the fly and add them to the page programmatically. I tried to do this by using the standard AddWebPart() method of the WebPartManager control but found that using this method the WebPart doesn’t seem to be added to the page correctly which seems to be because the AddWebPart() methods gives the WebPart a random ID therefore it cannot participate in ViewState management. Furthermore because of the need to add the WebPartZones within the Page OnInit event the WebParts were getting recreated each time the page did a postback.
I searched around the web and found lots of people having the same problem but very little in the way of solutions to the problem. Eventually I stumbled across a forum post that mentioned the WebPartManagerInternals class. The documentation fails to mention exactly how to use this class but contained the following text:
“Isolates into a separate class methods that are used by the WebPartManager control and can be overridden by developers who extend the control, but are rarely needed by page developers.”
It also mentions that the class is a sealed class meaning you cannot derive from it and a quick look in Reflector showed me that it’s contructors were internal. This meant I needed to access the class from within the WebPartManager class.
I created a new DynamicWebPartManager class that derived from WebPartManager and in this class I created a method called AddDynamicWebPart(WebPart webPart, WebPartZone zone) which as you can see accepts a WebPart and a WebPartZone. In this method I called two methods on the Internals property (beware this Internals property DOE’S NOT show up in Intellisense) AddWebPart() which accepts a WebPart and SetZoneID() which tells it which WebPartZone to add the WebPart to.
public class DynamicWebPartManager : WebPartManager
{
public void AddDynamicWebPart(WebPart webPart, WebPartZone zone)
{
Internals.AddWebPart(webPart);
Internals.SetZoneID(webPart, zone.ID);
}
}
Now by using this DynamicWebPartManager class on your page instead of the out of the box WebPartManager class you can add WebParts by calling the AddDynamicWebPart() method.
WebPartZone reportZone = new WebPartZone();
reportZone.ID = “reportZone1″
reportZone.HeaderText = “My Report”;
ToolBoxPanel.Controls.Add(reportZone);
TestTableWebPart webPart = new TestTableWebPart();
webPart.ID = “webPart1″
webPart.Title = “My Report - Table”;
ReportBuilderWebPartManager.AddDynamicWebPart(webPart, reportZone);

February 15, 2008 at 3:59 pm
Hi…
How do I to instance the class WebPartManagerInternals ?
Because in my class thorw the excepion that my Internals is null
thanks
February 17, 2008 at 9:13 am
thanks!
February 19, 2008 at 1:53 pm
As I make to instance the class WebPartManagerInternals ?
Thanks
ps:sorry, I don’t speak english, but I need so much know this
February 20, 2008 at 9:44 am
Hi Jonathan,
I think you need to re-read the article a bit more in depth as it mentions this.
“It also mentions that the class is a sealed class meaning you cannot derive from it and a quick look in Reflector showed me that it’s contructors were internal. This meant I needed to access the class from within the WebPartManager class.”
You cannot create an instance of the class as it’s sealed. However it is exposed within the WebPartManager class. Hope that helps.
April 3, 2008 at 2:19 am
Hi Lee,
What is ReportBuilderWebPartManager? is it the name of the namespace of the class? another question is that how can you grad the id of the dynamically added webparts in the zone? how can i get the webparts id in the Page?
Waiting for your replies..
Thanks and Regards,
May 8, 2008 at 9:35 am
bud, just wanted to post to thank you for this f***ing awesome article, it worked perfectly and made my day definitely since i have been for more than a working day trying to fix this problem…
BIG THANKS
May 8, 2008 at 6:23 pm
I had the same problem, I had looked for it through many sites but they don’t have a right solution. Now by your suggestion I’ve finished with it.
Thank you very much !