Dynamically adding WebParts using WebPartManagerInternals class


I’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);

kick it on DotNetKicks.com

23 thoughts on “Dynamically adding WebParts using WebPartManagerInternals class

  1. Hi…

    How do I to instance the class WebPartManagerInternals ?

    Because in my class thorw the excepion that my Internals is null

    thanks

  2. As I make to instance the class WebPartManagerInternals ?

    Thanks

    ps:sorry, I don’t speak english, but I need so much know this

  3. 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.

  4. 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,

  5. 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

  6. 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 !

  7. Nice one!!, i was exactly looking for this.
    I actually created my own editors within my parts, so i have inline editing but since i do not wont to use the AJAX framework i decided to use Jquery to achieve drag and drop for all browsers. now that i found out of this Internals my life will be easier, it even solve my next feauture… part templated page with part properties loaded. so a user can save a page as a templated page and use it over again.
    Anyways,
    thanks.

  8. Hi Lee,
    I have done exactly what you have described in this article. But the web part is not added in the zone. I am instantiating DynamicWebPartManager calling AddDynamicWebPart() from Init(). I am not getting any error though. It just displays empty zone. Am I missing something?

    Thanks.

  9. I also did code described in this article. But the web part is not added in the zone.

    DynamicWebPartManager WebPartobj = new DynamicWebPartManager();
    Microsoft.Web.Preview.UI.Controls.WebParts.WebPartZone zone1 = new Microsoft.Web.Preview.UI.Controls.WebParts.WebPartZone();
    zone1.ID = “zone1″;
    zone1.HeaderText = “zone1″;
    MyZone.Controls.Add(zone1);
    Control UserControl1 = LoadControl(“WebUserControl1.ascx”);
    UserControl1.ID = “WebUserControl11″;
    WebPart myWebPart1 = WebPartManager1.CreateWebPart(UserControl1);
    myWebPart.ID = “myWebPart1″;

    WebPartobj.AddDynamicWebPart(myWebPart, zone1);

    It just displays empty zone. Please Help me to do this.

  10. Hi Lee,

    In your article everything is fine, but i can’t understand TestTableWebPart webpart = new TestTableWebPart(); how to i use this.
    please give me the solution. last two days i am working this issue.

    Regrads,
    Dravid

  11. hi,

    Could you please tell me what is TestTableWebPart? you have created one object for the TestTableWebPart, but you didnt mention any thing about that. please explain me.

    Thanks
    JACK

  12. I also am having the problem that the Web Part isn’t being created after the methods are called – anyone find a solution to this? I’ve tried loading both a user control, as well as a simple Calendar control just to get it to work and it doesn’t create the Web Part on the page atall.

  13. Hi,

    I am in great touble, please help me out.

    I tried your code is working 100% correctly. now i will tell you my scenario.

    i am dynamically adding usercontrol as webpart. Inside this usercontrol i have content that should be update as user saves the content by pressing saving button inside this usercontrol. Problem arises when postback even of that save button fired, two time creation of webparts methods are called, because our creating webparts should be called whenever any server side event occurs and another time i am manually calling it so that content is updated.

    If you got any question in understanding the problem please do reply me.

    My email id is sumanmodi@gmail.com

    Waiting to hear from you.

  14. I created webpart site, It run through VS 2008, but I make virtual directory in IIS 6.0 but it not run the site , mean it not add the webpart dymanicaly

  15. Damn, You made my day ! Turns out as you mentioned the .AddWebPart() provides a random client ID for the webpart causing all controls in the webpart unable to participate in viewstate, and even causing server side events to not fire.

    With your method, it’s working fine now!

    Only thing wasn’t easy coming across your article from search on google. Lucky for me, it was referenced from http://geekswithblogs.net/dotNETvinz/archive/2009/07/09/asp.net-webpart–faq.aspx

    • I am using the same method explained here.But not able to set design display mode.Though I set it in the code,I am not able to drag and drop the controls b/t zones.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s