Creating Web Parts

There are several different ways to create a Web Part. The first and easiest way is to simply take an existing ASP.NET control and place it inside a ZoneTemplate. At runtime, that control will automatically be wrapped inside a GenericWebPart control. This automatic promotion of regular controls to Web Parts makes using existing controls in Web Part Pages extremely easy.

A second way to create Web Parts is to create a control that derives from the WebPart class. Although this approach provides the developer with the most direct control over the behavior of the Web Part, it also doesn't allow you to use a designer and requires you to have some knowledge of custom control creation, which is covered in Chapter 30, "Developing ASP.NET Controls."

The third way is to create a user control that performs the function you want. After the user control has been created, you can place it within a WebPartZone and it will not only function like a standard Web Part, but it will also have access to all of the Web Part functionality available in the ASP.NET Web Part engine.

Creating user controls isn't the focus of this chapter, but there are some additional things that you can do within a user control or within any other class to make the control compatible with the personalization provider. If you create a Web Part and you want to be able to expose a property of that Web Part so that the data can be stored in the personalization store, you can use the PersonalizableAttribute class. You use this attribute to decorate a property just as you would with any other attribute, as shown in the following code:

[Personalizable(PersonalizationScope.User)] public string ZipCode {    get { return zip; }    set { zip = value; } } 

You can also use the WebDescriptionAttribute and WebDisplayNameAttribute attribute classes to further decorate a property so that it will show up properly in an EditorZone. WebDescriptionAttribute indicates the tooltip that will be used for a Web Part property, whereas WebDisplayName indicates the friendly name that will be displayed for a Web Part property:

[Personalizable(PersonalizationScope.User)] [WebDisplayName("Zip Code")] [WebDescription("Zip Code Used for Local Weather Forecast")] public string ZipCode { ... } 

The next section on building connected Web Parts will give you a thorough walkthrough of creating user control-based Web Parts.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: