Field Control - for Developers

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned

Overview

Field Contols are the main building blocks of Content views. They generate the HTML controls responsible for the input or output of the displayed Content's fields. Field controls are implemented as ASP.NET controls, they can be used in the ascx source of the content views.

Details

The field control class is responsible for the data flow between user interface and appropriate Field of the viewed Content.

Role of field control

Field control implementation

A field control is a class that is inherited from the abstract FieldControl (namespace: SenseNet.Portal.UI.Controls). The most simple field control overrides the GetData and SetData methods to define custom logic:

namespace FieldControlSample
{
    public class MyFieldControl : FieldControl
    {
        public override object GetData()
        {
            // return data from UI to field
            return null;
        }
        public override void SetData(object data)
        {
            // field calls this on init, initialize UI from retrieved data here
        }
    }
}

To learn how to create a custom a field control implementation, read the following article:

Field controls in contentviews

Field conrols can be embedded in content views just like any arbitrary ASP.NET control. In the following content view fragment every element is a field control that uses the sn prefix and has a FieldName attribute:

...
<div class="sn-content sn-content-inlineview">
    <sn:ErrorView ID="ErrorView1" runat="server" />
    <h1><%= GetValue("FullName") %></h1>
    <sn:ShortText ID="ShortText0" runat="server" FieldName="Name" ControlMode="Edit">
      <EditTemplate>
        <asp:TextBox ID="InnerShortText" Class="sn-ctrl sn-ctrl-text sn-ctrl-username" runat="server"></asp:TextBox>
      </EditTemplate>
    </sn:ShortText>
    <sn:ShortText ID="ShortText1" runat="server" FieldName="FullName" ControlMode="Edit" />
</div>
...

Manually registering the sn prefix for built-in controls is unnecessary, since it is registered in the web.config. If you create custom field controls you will have to register a tagprefix for the corresponding namespace.

Built in field controls

There are numerous built-in field controls you can use in content views out-of-the-box. Any built-in field has at least one corresponding built-in field control in the Sense/Net Content Repository. Here is a list of all of them:

Field control properties

In addition to the standard properties every ASP.NET server control shares (e.g. ID, runat, etc.), field controls provide several other ways to control their behavior. The most important properties are the following:

  • FieldName (*) - Gets or sets the fieldname of the Field Control. This is the name with which the control is mapped to a Field of the underlying Content.
  • ControlMode (*) - Gets or sets the control mode: Browse, Edit or None (see Field_Control#Common_properties and Field_Control#Field_Control_modes).
  • FrameMode (*) - Gets or sets the frame mode: NoFrame, ShowFrame or None (see Field_Control#Common_properties and Field_Control#Field_Control_modes).
  • Field - Gets or sets the underlying field.
  • ContentView - Gets the content view that is the parent of this control.
  • Content - Gets the content that belongs to the underlying field.
  • ContentHandler - Gets the Node that belongs to the underlying field.
  • ReadOnly (*) - Gets or sets whether the control is read-only. If true control will not be editable on the user interface.
  • InputUnitCssClass (*) - Gets or sets the class of the wrapper tag of the control.
  • FieldIsCompulsory - Indicates whether the underlying field is compulsory.
  • HasError - Gets whether the field control is invalid (has the ErrorMessage set).
  • ErrorMessage - Gets error messages related to the field control.

(*) Some properties can be set as an attribute of the field control element. For example the following field control is connected to the FullName field and it is rendered as an editable control:

<sn:ShortText ID="ShortText1" runat="server" FieldName="FullName" ControlMode="Edit" />

Field control templates

Field controls can be templated, that define the layout of the field control. Global templates can be found in the /Root/Global/fieldcontroltemplates folder and can be overridden in the content view markup, for example:

    <sn:ShortText ID="ShortText0" runat="server" FieldName="Name" ControlMode="Edit">
      <EditTemplate>
        <asp:TextBox ID="InnerShortText" Class="sn-ctrl sn-ctrl-text sn-ctrl-username" runat="server"></asp:TextBox>
      </EditTemplate>
    </sn:ShortText>

See info on default templates at the individual reference articles of built-in field controls.

Example/Tutorials

There are no examples for this article.

Related links

References

There are no related external articles for this article.