Checkbox Values

Checkbox Values

Checkbox Field Values

A Checkbox field stores the value 1 when a user checks the checkbox. Check for a value of “1” to determine if the checkbox is checked

This snippet uses the context database instead of specifying the database to use - presentation components that retrieve content for display almost always access the context database rather than referencing a database by name

This snippet uses a fields collection to reference the field

  • Returns null if the field does not exist or the field value is null
  • Does not generate inline editing controls for the Page Editor
  • Useful for field types other than simple field types

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.CheckboxField
  • Sitecore.Data.Items.Item
  • Sitecore.MainUtil

Checkbox Values C# Web Control Code

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
//get Simple Fields metadata item from context database using item path
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/TIC-Meta/FieldsDemos/Simple Fields");
if (item == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
string itemName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", itemName);
output.RenderEndTag(); //P

Sitecore.Data.Fields.CheckboxField checkbox = item.Fields["example checkbox"];
if (checkbox != null) 

    string check =  checkbox.Checked.ToString();
    if (!string.IsNullOrEmpty(check))
    {
//display field value and add linebreak 
output.Write(checkbox.InnerField.DisplayName + " Field Value: <b>" + checkbox.Value + "</b><br/>"); 
if(checkbox.Checked) 
output.Write("Checkbox on Simple Fields page is checked"); 
else 
output.Write("Checkbox on Simple Fields page is not checked");    }
}
output.RenderEndTag(); //Div


Checkbox Values XSLT Rendering Code

<xsl:variable name="simpleFields" select="sc:item('{EC44470F-FF45-4023-95C2-EC886FD33D0C}',.)" />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">
<p><b>Item: <xsl:value-of select="$simpleFields/@name"/></b></p>
<!-- alternative to <xsl:value-of select="sc:fld('example checkbox',$simpleFields)" /> -->
<xsl:value-of select="'Example Checkbox Field Value: '"/>
<b><sc:text field="example checkbox" select="$simpleFields" /></b><br/>

<xsl:choose>
    <xsl:when test="sc:fld('example checkbox',$simpleFields)='1'">
        <xsl:value-of select="'Checkbox on Simple Fields page is checked'"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="'Checkbox on Simple Fields page is not checked'"/>
    </xsl:otherwise> 
</xsl:choose>
</div>


Checkbox Values C# Web Control Output

Example Checkbox Field Value: 1
Checkbox on Simple Fields page is checked


Checkbox Values XSLT Rendering Output

 

Example Checkbox Field Value: 1
Checkbox on Simple Fields page is checked