This section of the documentation is going to describe a specific kind of controls used in Sitefinity called Field Controls. It is going to explain what are the intended usage scenarios and supported functionality of field controls, what are the field controls available out of the box and how you can create custom ones.
What are Field Controls?
Field controls are controls, which have two primary tasks:
- Provide UI to read the value of a field
- Provide UI to write the value of a field
A field in this context is the property of any content item in Sitefinity. When you create dynamic modules and dynamic types, you can also create fields for them. The built-in modules and content types also have fields. Examples of fields are:
- All items of type News Item have a field called Title.
- All items of type Event have a field called StartDate.
Those are the fields, whose values field controls have the job of reading and writing.
Note: When we talk about items in the context of field controls, we mean content items, whose fields are edited or displayed by the field control (i.e. news items, events, images, etc.)
Where are field controls used?
We mentioned that there are two primary tasks each field control has to solve – read and write the value of a single field. When we think about this, we can guess where those field controls can be used.
- When reading the value of a field, field controls are used to display that value in Sitefinity widget templates
- When writing the value of a field, field controls are used in the Sitefinity backend forms (when creating and editing items).
You can also use field controls in any other scenario, where you want to read or write the value of a single field. We will look at how we can use them in the section for building field controls.
What are the benefits of using field controls?
You may be wondering why we need to introduce a new concept for field controls. Can’t we just use normal ASP.NET controls and HTML tags for these needs? Can’t we use a normal dropdown to select the value of a field or a normal textbox? The answer is – you can. However, there are advantages that field controls give you, which are not available when working with normal controls.
- Easily switch between different views of a content item
When you’re building user interface for editing and showing an item, often you need to provide several views for the same item – edit, preview, insert. Those usually contain all item fields in some form. With field controls, you can build all views of an item by using the same controls, and just switching from edit to preview mode.
- Ability to define structured user interfaces
Field controls let you build user interfaces declaratively. Instead of implementing a separate control for each scenario and form, you can define what forms you want and what field controls should be there in each form. This is possible due to the concept of definitions, which needs its own documentation section.
- Built-in ready-to-use UI for complex property types
Instead of reinventing the wheel, you can reuse user interface components already available in other areas of Sitefinity. It is likely that a similar scenario like the one you are implementing has been done before, and you can just reuse it.
- Out of the box compatibility with the Sitefinity Look
We advise that you stick to the Sitefinity look and feel for custom modules that have backend user interface. For the purpose, you need to use the same UI elements and styles. With field controls, you get this out of the box. Your UI will look the same as the UI of the default modules in Sitefinity.
DisplayMode in field controls
As mentioned, you can (and should) provide many views to work with an item in your custom modules. You can have different UI for inserting, editing and viewing an item. To support these different views, field controls have a DisplayMode property, which can be either Read or Write. Read mode is used when you want to display the value of a field (view), Write mode is used when you want to edit the value of a field (insert, edit).
The UI of a field controls is always different in Read and Write mode. The mode is switched automatically by Sitefinity, depending on the context in which the field control is used. Sometimes the UI in the different modes is drastically different, and usually more complex in Write mode. If we have a field, which will associate an item with a category, then it should only display the category’s name in Read mode, maybe as a link. But when in Write mode, the field control should make it possible for the user to select from pre-existing categories or create new ones to associate the item with.
It is good to have this concept in mind every time when dealing with field controls.