Access a field using the FieldRenderer
In presentation components, whenever possible, use the FieldRenderer
Web control to output field values. For example, to output the Title field in the context item from a Web control:
namespace Namespace.Web.UI.WebControls
{
public class WebControlName : Sitecore.Web.UI.WebControl
{
protected override void DoRender(System.Web.UI.HtmlTextWriter output)
{
string html = Sitecore.Web.UI.WebControls.FieldRenderer.Render(
Sitecore.Context.Item, "title");
output.Write(html);
}
}
}
Alternatively, you can add the FieldRenderer
Web control to a layout or sublayout. For example:
<sc:FieldRenderer runat="server" ID="fieldControl" />
In the code behind for the layout or sublayout, set properties of the control. For example, to render the Title field in the context item:
namespace Namespace.Web.UI
{
public partial class SublayoutName : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
fieldControl.DataSource = Sitecore.Context.Item.Paths.FullPath;
fieldControl.FieldName = "title";
}
}
}
The FieldRenderer
Web control does not support all field types. For example, you cannot use this type to retrieve Multilist data. See Access Checklist, Multilist, Treelist, and TreelistEx fields.
You can use the FieldRenderer Web control with Date, Datetime, Image, Integer, Multi-Line Text, Number, Rich Text, and Single-Line Text fields.