Monday, January 18, 2010

Displaying Field Values Using Server Controls

This post is part 3 of a not-yet-determined number of posts discussing presentation logic and Sitecore. My previous post explained some of the options available for getting content to appear in a Sitecore website. I ended with a promise to cover some of the Sitecore server controls that are used to display content.

Sitecore has a number of server controls that are used for different purposes. For this discussion there are 2 kinds of server controls to consider: those that display a single value and those that display other controls. I will start with the former.

Controls that display content
One of the most fundamental tasks performed by presentation logic is to display values. If you are displaying content about a book, you might want to display the book's title and author and an image of its cover.

In Sitecore, the book would be an item which was created using a data template. The title, author and image are fields defined in the data template. A layout is used when the book item needs to be displayed. Server controls are used in the layout to position and render the field values.

<sc:FieldRenderer ID="FieldRenderer1" runat="server" FieldName="Description" />

The specific type of server control is Sitecore.Web.UI.WebControls.FieldRenderer. The class name reveals this server control's purpose: it renders the value of a field. The various web controls that extend Sitecore.Web.UI.WebControls.FieldControl provide similar functionality.

What's the difference FieldControl classes and FieldRenderer? FieldRenderer is generic. You give it the name of the field and it will render the value. FieldControl classes target specific field types. They recognize the unique capabilities of the different field types. The following examples demonstrate what I mean.

<sc:Image ID="Image1" runat="server" Field="Image" MaxWidth="250" />
<sc:Date ID="Date1" runat="server" Field="Date" Format="MM/dd/yyyy" />

As you can see in the following example, the specific options that are available using FieldControl classes are available using FieldRenderer. You just don't get Visual Studio showing what they are the way you do with FieldControls. One thing to note is that FieldControl classes do not exist for every possible field type, so you'll probably be using FieldRenderer at least some of the time.

<sc:Image ID="Image1" runat="server" Field="Image" MaxWidth="250" MaxHeight="100" />
<sc:FieldRenderer ID="fr1" runat="server" FieldName="Image" Parameters="MaxWidth=250&MaxHeight=100" />

What's next?
That gets us through the server controls that display field values. But most of the time individual field values aren't displayed on their own. Usually a number of fields are displayed together, forming a logical grouping of presentation logic. In my next post I will describe how blocks of presentation logic can be defined in Sitecore.

Want to learn more?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.