Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

FieldRenderer

Modified: 2008/11/13 11:17 by mark - Categorized as: Sitecore
Since Sitecore 6 has an inline editing experience for end-users, the way you retrieve field values in C# has been changed. In Sitecore 5.3 you needed to retrieve the values by using the API and then bind those values to an control.

In Sitecore 6 this has been changed. They introduced an new webcontrol called a FieldRenderer. The only thing you need to do is specifying the item and field you want to display, and the control will create the html for you. And in the editmode the inline-edit functionality is also available. Optional you can set parameters by using key-value pairs, for example "key1=value1&key2=value2".

Instead of using this
this.Controls.Add(new LiteralControl(Sitecore.Context.Item.Fields["Text"].Value));

You would better use this
this.Controls.Add(new LiteralControl(FieldRenderer.Render(Sitecore.Context.Item, "Text")));

Besides the FieldRenderer Sitecore also introduced four webcontrols which render specific field types
  • Sitecore.Web.UI.WebControls.Date
  • Sitecore.Web.UI.WebControls.Image
  • Sitecore.Web.UI.WebControls.Link
  • Sitecore.Web.UI.WebControls.Text

The most usefull thing about these controls is the strongly-typed properties they have. For example the image control has an MaxHeigt property which allows you to set the maximum height.

Sitecore.Web.UI.WebControls.Image img = new Sitecore.Web.UI.WebControls.Image();
img.Item = Sitecore.Context.Item;
img.Field = "image";
img.MaxHeight = 100;
img.MaxWidth = 300;
            
this.Controls.Add(img);

Of course you can always use the old method, but then the end-user will NOT have the possibility to use the inline-edit functionality.

 © Evident Interactive BV