oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What's New in Beta 2: Web Parts Revisited

by Jesse Liberty

I've been working with Whidbey (.NET 2005) for a little over a year. During that time, I've come to have enormous respect for the engineers at Microsoft, and I do believe that .NET 2005 (2.0) is a great improvement over 1.x. That said, the beta has had a bit of a hard time settling down, and so many of the earlier columns I wrote (and that others wrote) about Whidbey are, at best, a bit out of date (and some are badly broken).

This column will revisit, fix, and expand on one of my favorite 2.0 features: Web Parts.

Before we begin, however, let me note that Microsoft has published a few articles about features they had hoped to get into beta 2, but that didn't make it, including an excellent article titled " Features Postponed in ASP.NET 2.0." In addition to what is listed on that page, however, other features simply changed, or disappeared.

One breaking change, for example, is that the IEnumerable interface now extends the non-generic IEnumerable interface.

public interface IEnumerable<T> : IEnumerable

The net result is that in order to implement the interface you must include a non-generic GetEnumerator method. Since the signatures for the generic and the non-generic GetEnumerator methods are identical, you must implement the non-generic form explicitly:

IEnumerator IEnumerable.GetEnumerator()
	throw new NotImplementedException();

There are a few other breaking changes here and there that make moving to beta 2 from the CTP an adventure, but one of the more significant, to me, is the change to how Web Parts work. The key change here is that the Web Part Page Menu is now removed, and thus you must supply that functionality yourself. This article will walk you though creating a Web Part Page and will expand on my previous article to show you how to add a catalog and an editor menu to give your users even greater control over your page.

To begin, please download the WebPartsBeta2Starter kit and use that as the starting point for this article. This starter kit assumes you have already installed SQL Express on your local machine, and set it up to support personalization (see my previous column on personalization for details). You start with a single login account: jliberty. The password is Liberty! (that is an exclamation point [bang] at the end of my last name).

Note: In beta 2, Microsoft implemented the System.Web.UI.WebControls.CreateUserWizard.PasswordRegularExpression property, which defaults to the following "strong" password requirements: it must be at least six characters long, and must include at least one element each from three of the four types of characters: English upper case, English lower case, Arabic numerals, and special characters. (See " Strong Password Enforcement" for more information.)

The first step is to open the starter kit and make sure it works. Log in and you should see a screen very much like Figure 1:

Starter Kit Running
Figure 1.

You'll create a new page, WebPartsDemo, and a link to that page for the logged in user. To begin, modify Welcome.aspx and add a third hyperlink within the LoggedInTemplate:

   <asp:LoginName ID="LoginName1" runat="server" />
   <br />
   <asp:HyperLink ID="HyperLink2" NavigateUrl="CreateAccount.aspx" 
      runat="server">Create User</asp:HyperLink>
   <asp:HyperLink ID="HyperLink3" NavigateUrl="ManageRoles.aspx" 
      runat="server">Manage Roles</asp:HyperLink>
   <asp:HyperLink ID="HyperLink4" NavigateUrl="WebPartsDemo.aspx" 
      runat="server">Web Parts</asp:HyperLink>

You are now ready to create the WebPartsDemo.aspx page.

Creating Zones

A page that uses Web Parts is divided into zones: areas of the page that can contain content and controls that derive from the Part class (Part controls). They can also contain consistent UI elements (header and footer styles, border styles, etc.) known as the chrome of the control. For this example, you'll arrange your zones in a table (to make layout easier). Here are the steps:

  1. Create a new page called WebPartsDemo.aspx.
  2. Open the WebParts section of your Toolbox, and drag a WebPartsManager onto your page from the WebParts panel in the toolbox. (The job of the WebPartsManager is to track and coordinate all of the web part controls on the page. It will not be visible when the page is running.)
  3. Add a new table, with two rows and three columns. Rearrange the columns so that they are not of even size.
  4. Drag a WebPartZone into each of the six table cells. Each WebPartZone will have a default name (e.g., "WebPartZone6") and a default heading. You can easily modify either or both of these properties in the properties window. Your page should now look like Figure 2:

Laying out the zones
Figure 2.

Adding Controls To Zones

Set the HeaderText property on the first zone to News. Drag a Label control into the zone; it should now look like Figure 3:

Label As Web Part
Figure 3.

The <asp:label> web control does not have a title attribute, but because it is now wrapped in a Web Part control, which does have a title attribute, you can set the title (though you will confuse Intellisense). To see this at work, switch to Source view and modify the label as shown:

Runat="server" Title="Today's News" ID= 
"Label1"><br />Thousands die. Thousands more die at eleven.</asp:Label>

While you are in Source view, set the label's text to "Thousands die. Thousands more die at eleven."

Switch back to Design view, set the page title to "Web Parts," and then drag a ListBox control into WebPartZone3. Click on the list box, and then on its smart tag and Edit Items to open the ListItems Collection Editor. Add a few items to the list box. Back in Source view, set the Title property to "Our Sponsors." Run the application, log in as jliberty, and your page should look more or less like Figure 4:

Two Web Zones displayed
Figure 4.

Minimizing and Restoring

Click on the Minimize tag and a menu appears allowing you to minimize or close the Web Part, as shown in Figure 5:

Minimize or Close
Figure 5.

If you choose Minimize, the Web Part is minimized to its title, and the Minimize tag offers the choices of Restore or Close. If you stop the application, then restart and log back in and return to the page, it will still be minimized (personalization at work).

Pages: 1, 2

Next Pagearrow