Custom Web Parts


At present, two Web Part classes are shipped with ASP.NET: ContentWebPart for visually editing content, and GenericWebPart, which will implicitly be used if you place other regular (user) controls into a WebPartZone. Additionally, you may create your own Web Parts by deriving a custom control class from the abstract base class WebPart. This is a regular web control deriving from the Panel control you already know.

You may ask yourself what the advantages of Web Parts are in comparison to a custom or user control. Well, with a Web Part control you have a direct access to the Web Part infrastructure. For example, you can implement properties that can be personalized as the following example shows.

Creating a Custom Web Part

Once more I've borrowed a small example from the intranet project template of VS .NET to demonstrate custom Web Parts. This particular Web Part can be used to show the actual weather conditions—a typical scenario for personalization because all users want their local weather conditions to be shown instead of generic ones. What could be more convenient than asking users for their zip code?

I've actually modified the original sample slightly so that it's structured quite simply. The overridden RenderContents() method holds a small JavaScript source snippet as a constant value that takes care of the weather data being taken over from another web site on the client side. The JavaScript method receives the content of the ZipCode property.

The property is marked with three attributes. Personalizable makes sure that the property is in fact personalizable and is stored individually for each user. Another attribute is named WebBrowsable, and it ensures that the value can be edited through the Web using the already mentioned PropertyGridEditorPart control. The third attribute, DisplayName, is assigned with the text that is shown as a title within the editor. Without an explicit definition, the name of the property would be displayed instead.

Listing 8-4 shows the source code—excluding the JavaScript—of the control. This can be registered with the page as usual and afterward placed in the ZoneTemplate of the WebPartZone control. After a click of the Edit button, the PropertyGridEditorPart control appears, and you can then use it to enter your own zip code (see Figure 8-11).

click to expand
Figure 8-11: Maybe they'll get better weather tomorrow.

Listing 8-4: Creating Custom Web Parts with Only a Few Lines of Code

start example
 using System; using System.ComponentModel; using System.Web.Personalization; using System.Web.UI; using System.Web.UI.WebControls; namespace PGK.Web.UI.WebControls.WebParts {     public sealed class WeatherWebPart : WebPart     {         private const string HtmlFormat = @"             <div id=""weatherView""></div>             <script id=""weatherScript"" language=""javascript""></script>             <script language=""javascript"">             ...             CreateWeather('{0}');             </script>             ";         private string _zipCode = String.Empty;         [Personalizable]         [WebBrowsable]         [System.ComponentModel.DisplayName("Your ZIP Code")]         public string ZipCode         {             get { return _zipCode; }             set { _zipCode = value; }         }         protected override void RenderContents(HtmlTextWriter writer)         {             string text;             if ((_zipCode == null) ||                 (_zipCode.Length == 0))             {                 writer.Write(                     "Please enter a zip code by personalizing this WebPart!");             }              else             {                 writer.Write(String.Format(HtmlFormat, _zipCode));             }         }     } } 
end example




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net