9.1. Understanding Web PartsWeb parts are based on ASP.NET web controls. In fact, the WebPart class inherits from System.Web.UI.Control . That means most of the programming issues of composition, lifetime, state, and server versus client-side processing are the same. In case you're not familiar with those concepts, here's a brief synopsis:
Figure 9-1 illustrates these concepts in terms of the life cycle of a web part on the server and how that is presented on the client. animal 9-1. Key events in the life cycle of a web part9.1.1. Extending ASP.NETThe previous section described high-level concepts that are constant for web parts and ASP.NET custom controls, but web parts extend the ASP.NET Control class to allow members to modify, save, and connect web part properties, as described here:
The SharePoint documentation calls web parts designable that is, authorized members can change their appearance and function from within the browser as shown in Figure 9-2. animal 9-2. Designing a web part in the browserUnder ASP.NET those changes can be made only by editing a control's HTML, and there is no built-in way to save settings or connect parts. The SharePoint WebPart and WebPartZone classes provide the infrastructure for those features. 9.1.2. Using Web PartsThat said, not all web parts are designable. The navigation bar and Quick Launch areas on the default SharePoint home pages are examples of nondesignable NavBar web parts. Whether or not a web part is designable depends on where it is placed on a page:
In Chapter 8, I showed you how to import dynamic web parts into a web part zone on a page. To add a static web part, edit the . aspx page in FrontPage to register the web part assembly and include an element for the web part. For example, the following line registers sample assembly used throughout this chapter: <%@ Register TagPrefix="Ch09" Namespace="Ch09Samples" Assembly="Ch09Samples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fb6919fe58e4ba63" %> All the attributes of the @ Register directive match those defined for the web parts in the SafeControl element in Web.config , with the exception of TagPrefix . The TagPrefix attribute defines the prefix you'll use to qualify the web part element on the page. For example, the following element adds Webpart1 to the page: <ch09:WebPart1 text="New text property setting." title="WebPart1"> </ch09:WebPart1> Properties of the web part are included as attributes in the element. In this case, the text attribute sets the Text custom property and the title attribute sets the web part's built-in Title property. Figure 9-3 shows the result. animal 9-3. Setting a static web part's propertiesIn this context, the terms static and dynamic are misleading. Nondesignable (static) web parts can accept entries and display results dynamically the same way that designable parts can (Figure 9-4). animal 9-4. Is this interactive part static or dynamic?See the StaticParts.aspx sample for examples of including web parts outside of web part zones. 9.1.3. Programming TasksIn ASP.NET you can create three types of custom controls: user controls, composite controls, and rendered controls. Because SharePoint doesn't directly support user controls (. ascx ), all web parts are either made up of other web controls (composite controls) or rendered directly as HTML (rendered controls). In most cases, web parts use a combination of the two techniques, so that's what I show in my examples. Table 9-1 summarizes the major tasks I cover in this chapter and provides a guide to some of the key members, clauses, attributes, and interfaces used to complete the task. Table 9-1. Web part programming tasks
You can find reference information for these keywords in the SharePoint SDK or in the Visual Studio Helpthe SharePoint SDK omits items that aren't part of their namespace. For instance, it doesn't cover the RenderControl method because that is part of the .NET Framework. |