Practical ASP.NET

Integrating User Controls with WebParts

In his ongoing campaign to convince ASP.NET developers that User Controls are a simple way to start implementing customizable pages, Peter shows how to improve the integration between User Controls and ASP.NET's WebPart framework.

In an earlier column -- "Create a Reporting Dashboard with WebPartZones (But Without WebParts)" -- I described how you could create a reporting dashboard that allows users to select reports from a catalog on the page. To do this, I took advantage of the WebPart framework in ASP.NET but, rather than go through the agony of creating an actual WebPart, I used something that ASP.NET developers do understand: User Controls.

But my solution wasn't complete. While a real WebPart displays descriptive information (a title and a subtitle, for instance), a User Control does not. In addition, I didn't address allowing the user to set values on the User Control to personalize the WebPart.


In my customizable reporting dashboard, these features would be very useful. In the catalog of available User Controls, having each user control display a title provides the easiest way to tell the user what each report does. In addition, a true WebPart would allow the user to provide input values to customize the report. For instance, while the default report might display sales for all regions, a user should be able to customize the report to display just the region the user is interested in.

Both of these options are available even when using user controls.

Implementing Descriptions
My first step in using a User Control as a WebPart is to have it implement the IWebPart interface, as in this example:

Partial Class AnnualSalesReportUserControl
    Inherits System.Web.UI.UserControl
    Implements IWebPart

The result of this change is the addition of six properties to my User Control: Title, Subtitle, Description, TitleUrl, CatalogIconImageUrl and TitleIconImageUrl. The first four are the easiest properties to implement as they only require a string value. In many ways, the Title and Subtitle are the most important because they are automatically displayed on the WebForm either on the WebPart itself or when the control is displayed in a catalog.

The user can change the Title property as part of customizing the WebPart but the Subtitle property is fixed. Because of that difference, you should use the Subtitle property to describe the WebPart independently of any personalization that you'll let the user implement (for instance, I'd give my WebPart report the name "Sales Report"). The Title property should default to a value that reflects the default settings for the report. For a sales report that, by default, shows sales for all regions but allows the user to select a specific reason, a good Title would be "Sales Report." Implementing those settings would require code like this:

Private _title As String = "All Regions"

Public ReadOnly Property Subtitle() As String _
    Implements System.Web.UI.WebControls.WebParts.IWebPart.Subtitle
        Get
            Return "Sales Report"
        End Get
End Property

Public Property Title() As String _
   Implements System.Web.UI.WebControls.WebParts.IWebPart.Title
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
 End Property

Implementing Personalization
The next step is to give the user the ability to customize how the User Control works. For a report, this might mean allowing the user to set the parameters that are passed to the report's selection criteria. The default parameter for the report could be an asterisk (to retrieve all regions) but, through a property, the user could be allowed to supply a specific region.

All that's necessary is to create the property that accepts the region and then add the Personalizable and WebBrowsable attributes to it, as in this example:

Private _region As String = "*"

<Personalizable(True) > _
    <WebBrowsable()> _
    Public Property Region() As String
        Get
            Return _region
        End Get
        Set(ByVal value As String)
            _region = value
	RegenerateReport(_region)
        End Set
    End Property

Adding a personalizable property isn't enough -- you also need to add the controls to the page that let the user set that property. Two controls are required: First you need to add to your page an EditorZone from the WebParts tab of your Toolbox. Then you need to drag a PropertyGridEditorPart into that zone (also from the WebParts tab).

Finally, you need to let the user put the control into Edit mode. To do that, you first need to add a button to the page that adds an Edit choice to the menu control that the WebPart framework adds to your User control. The code for that button looks like this:

Me.WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode

Now, when the user wants to customize the WebPart/User Control, he or she clicks on the button and selects Edit from the menu that appears on the User control. At that point, the PropertyGridEditorPart will appear on the page, populated with a control for every property you've marked as customizable. The user can then enter the values to control the WebPart. ASP.NET's WebPart framework will then save the settings for the next time the user views the page.


About the Author

Peter Vogel is a principal in PH&V Information Services, specializing in ASP.NET development with expertise in SOA, XML, database, and user interface design. His most recent book ("rtfm*") is on writing effective user manuals, and his blog on technical writing can be found at rtfmphvis.blogspot.com.

Reader Comments:

Tue, Jul 28, 2009 Peter Vogel Canada

Ali's right, of course: While User Controls can do many of the things that WebParts can while giving you the benefits of using a designer to create the UI there are things that are--at the very least--awkward to do in a User Control. You have to decide if the benefits of a visual designer outweigh the loss of functionality. I have to say, though, that for many applications, User Controls are "good enough" WebParts for me. But, sadly, not all applications.

Wed, Jul 15, 2009 Ali Kuwait

I am using user-controls to create webparts. Although it is easy path specially with the designer support, I reach a point where I get stuck. Modifying the web-part properties like the title becomes a painful issue and makes me seriously thinking to switch to webparts. As I found out, for the long run, always avoid shortcuts.

Thu, Jun 25, 2009 Shane G.

I agree with the comment posted by PETER VOGEL in the fact that most designer can quickly and effectively drag and drop controls with user controls. I can see it, I can click it, there for I know its there LOL! I am a SharePoint developer and on of my biggest frustrations with developing solutions for it is there is no WYSIWYG designer. But I am hearing good things about Visual Studios 2010 so keep your fingers crossed!

Sun, Jun 21, 2009 Peter Vogel Canada

I think that the major problem that ASP.NET developers have with creating WebParts is the absence of a visual designer. As a result, when creating a WebPart (or a server-side control) in order to add the controls that make up the User Interface, developers have to write code rather than using a WYSIWYG designer. One of the real benefits of creating User Controls is that you use the same tools/techniques for creating a User Control as you do for creating a Web Page. I always tell developers: If you know how to create a Web Page (and that's something that ASP.NET developers do know how to do) then you know how to create a User Control.

Thu, Jun 18, 2009 Steve

I have read this article and the one that came before it. In the first article you made the statement "developers don't use WebParts because they assume it will be 'too hard' or 'too different'" and seem to be using that as the premise for promoting User Controls over WebParts. I have some experience with User Controls and have not yet worked with WebParts. So, I don't know if the alleged developers' assumption that WebParts are too hard or different is a reasonable assumption. I would not want to continue using User Controls and discard WebParts without knowing if WebParts are significantly harder or too different.

I would also want to know what the advantages and disadvantages of using WebParts over User Controls are and vice versa.

I believe that more of a case should have first been made for using User Controls in place of WebParts.

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above