Using dynamically loaded User Controls in SharePoint 2007 / ASP.NET 2.0 Portals

I recently checked a couple of blogs about using dynamically loaded User Controls. Some of the blogs mentioned SmartPart v2 (aka SonOfSmartPart) to load ASP.NET 2.0 User Controls. This WebPart is completely targeted to SharePoint 2003, but it will remain working in MOSS 2007. When creating new Web Parts, you have the option of creating Web Parts that inherit from System.Web.UI.WebControls.WebParts.WebPart (recommended) or Microsoft.SharePoint.WebPartPages.WebPart (inherits from). The Windows SharePoint Services WebPart class exists primarily for the purpose of backward compatibility (Web Parts continue to work in Windows SharePoint Services 3.0 without modification), and to provide a small set of features that are not available in the other class.

Besides the targeting it's also not possible to use this Web Part in a ASP.NET 2.0 Portal. I know it's really easy to drag and drop a User Control in ASP.NET 2.0 Portal, but I also want to have this ability in MOSS 2007. Furthermore I like to have an option to add User Controls dynamically (not included in my Portal project). Scott Guthrie has written some astonishing info in one of his blogs about whether it will be possible to host Web Parts built as ASP.NET 2.0 User Controls within SharePoint 2007. He checked with the SharePoint team, and they told him that this will be a supported scenario via an additional component you add to SharePoint and that they'll have a Whitewater and sample on how to-do this published later this year.

I couldn't imagine this option wasn't available in MOSS 2007 and that the SharePoint team will give a additional component only later this year. As Joris Poelmans wrote in his blog Usercontrols (ascx), webparts and SharePoint 2007, it isn't that hard to dynamically load a User Control. Reading this blog I decided to write my own SmartPart fully ASP.NET 2.0 compatible. I heard some rumors that Jan Tielens is writing a Revival of SmartPart too, but I couldn't wait that long.

I'm starting a series of posts about my own UserControl WebPart. The goal of the series is to create a DLL which is public to everyone who wants to use it. While other articles sometimes are too long, I want to keep them very short. That way you're able to read them before your workday begins or perhaps just before you shut down. 🙂

From every post I'll refer to this post and I'll keep an index here so you can easily find your way.

Complete index

  1. part I (Startup project)
  2. part II (Class Diagram and test UserControl project)
  3. part III (WebPart project)
  4. part IV (About EditorPart, displaying a info about the WebPart)
  5. part V (WebPart EditorPart, selecting a UserControl)
  6. part VI (UserControl EditorPart, discovering properties of UserControl)
  7. part VII (Consumer / Provider)

I try to keep you informed and eventually will post my UserControl WebPart as a DLL.

6 thoughts on “Using dynamically loaded User Controls in SharePoint 2007 / ASP.NET 2.0 Portals

  1. Muhammad Fasih

    I have created a user control that loads some data on click event of a button. Userr contol works fine when i use the control within an aspx page. But button's click event does not fire when i embed it in a Web part using Page.LoadControl()method. Any idea about what I am doing wrong.

    Any help will be much appreciated.

    Muhammad Fasih

  2. Muhammad (and everyone with the same problem)...

    All dynamically loaded controls are unloaded from the page when a postback occurs. They will thus not remain on the page if you press a button or perform a PostBack in any way.

    This is quite annoying, since you manually have to reload any dynamically loaded controls to the page after the postback. You must also make sure to reload them at the right time and with the correct ID...otherwise they will not work properly. For instance, events may not fire.

    This can become quite a problem for large, complex web applications where dynamically loaded controls are essential to keep down byte size and avoid a lot of unnecessary functionality.

    A very simple and handy solution to this issue is described in this blog:

    It features a slimmed, downloadable class which, hopefully, will take care of this issue for you.

  3. This works fine but now I'm to the point of creating connections between controls like this and I'm wondering if there are any best practices or pointers. I'm doing the majority of the work in the inherited class for the user control - data binding, handling control events etc. The web part is pretty dumb. It's really just a host for the user control. I'm assuming I can't simply define Connection Consumer / Connection Provider methods inside of the user control (unless the web part class has some sort of mechanism for searching it's sub controls for connection points). So what do a do? Do I expose some methods from my user control that the web part can use to satisfy the connection? For example, the user control has a grid view that is bound to data that it retrieves internally (the web part class knows nothing about it). But now I want to expose that data for a connection.


Leave a Reply

Your email address will not be published. Required fields are marked *