Changing behavior of ASP.NET 2.0 wrapped WebPart

When you create a webpart in ASP.NET 2.0 by adding a user control to the WebPartZone, te control will be wrapped inside a GenericWebPart during runtime. This saves you from the trouble of having to derive from WebPart or implement IWebPart yourself.

You do not get the full set of possibilities with such a wrapped WebPart, but you can set some additional properties on the user control, that is itself does not have. In the fragment below you can see how a user control has a Title attribute that does not correspond to a property of the ServerStatus control.

<asp:WebPartZone ID=”ctlRightZone” runat=”server” Width=”100%” HeaderText=”Padding=”0″>
  <ZoneTemplate>
    <
clan:ServerStatus ID=”ctlServerStatus” runat=”server”
      GameEngine
=”HalfLife” Port=”27015″ ServerAddress=”80.84.250.88″
      ShowPlayers=”True” Title=”Server Status/>

At runtime the values of the following attributes (see also):

  • AuthorizationFilter
  • CatalogIconImageUrl
  • Description
  • ExportMode
  • Height
  • Subtitle
  • Title
  • TitleIconImageUrl
  • TitleUrl
  • Width

will be transferred to the GenericWebPart. Visual Studio will complain with green spell-check wriggleys, because it doesn’t believe that the user control has these properties. It will work though. That still leaves some interesting properties of WebPart that cannot be set this way, like the AllowClose and other Allow… properties. But …

Inside your user control you can get at the parent of the control through the Parent property. If this is a GenericWebPart you know that the control has been wrapped and you can access all properties of the part. This is best done in the Init phase.

Instead of accessing these properties in the Init event handler for each and every control, I suggest creating a new WebPartBase class, derived from UserControl that is the base class for all user controls that will be candidates for wrapped parts. It looks a little something like this:

public class WebPartBase: UserControl
{
  protected override void OnInit(EventArgs e)
  {
    GenericWebPart part = Parent asGenericWebPart;
    if (part != null)
    {
      part.AllowClose = this.AllowClose;
      part.AllowMinimize = this.AllowMinimize;
      part.AllowZoneChange = this.AllowZoneChange;
    }
  }

  private bool allowClose;
  private bool allowMinimize;
  private bool allowZoneChange;
  public bool AllowClose
  {
    get { return allowClose; }
    set { allowClose = value; }
  }

  // Rest of implementation omited for brevity

You probably get the picture. I showed only some of the properties of the GenericWebPart that are interesting to expose as properties of your to-be-wrapped control. Final result is that the aforementioned control now can have extra attributes like so:

<clan:ServerStatus ID=”ctlServerStatus” runat=”server” AllowClose=”False” AllowMinimize=”false” GameEngine=”HalfLife” Port=”27015″ ServerAddress=”80.84.250.88″ ShowPlayers=”True” Title=”Server Status/>

A nice overview on Web Parts is this MSDN Magazine article.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s