Question

I've been looking over the Sitecore documentation for HtmlControls and WebControls, but none of the items have any meaningful descriptions or example code to show how they're used or what they produce.

I understand how to use simple controls like Text, Date, Image, and Link:

<sc:Text runat="server" ID="content" Field="Content" />

Is there a resource that includes examples for more advanced controls like WebControls.ItemList or HtmlControls.TreePicker to show how they'd be used and what output they produce?

Was it helpful?

Solution

The SDN has some code examples. Essentially, WebControls are .NET server controls where you write all business logic and front-end code via C#. Here's the series on the SDN called "Web Controls":

  1. Part 1
  2. Part 2
  3. Part 3

Here's a sample TextControl:

protected override void DoRender(HtmlTextWriter output) {

  if (ClassAttribute.Length > 0) {
    output.AddAttribute(HtmlTextWriterAttribute.Class, ClassAttribute);
  }

  if (StyleAttribute.Length > 0) {
    output.AddAttribute(HtmlTextWriterAttribute.Style, StyleAttribute);
  }

  output.RenderBeginTag(HtmlTextWriterTag.Div);

  string val = string.Empty;

  if(_text.Length == 0) {
    val = GetFieldValue(_textField);
  } else {
    val = _text;
  }

  output.AddAttribute(HtmlTextWriterAttribute.Class, TextClass);
  output.AddAttribute(HtmlTextWriterAttribute.Style, TextStyle);
  output.RenderBeginTag(HtmlTextWriterTag.Div);
  output.Write(val);
  output.RenderEndTag();
  output.RenderEndTag();

}

EDIT: To understand how internal built-in Sitecore components work:

Sitecore is not going to provide the details of how their controls are built. Sitecore is not open source. That being said, I've been told several times by people from Sitecore that if you need to understand how something works to extend it, use the .NET Reflector to de-compile the kernel (Sitecore.Kernel.dll). I've done this many times to figure out how the internal things work. In your case, you can decompile the assembly and look at the classes under Sitecore.Web.UI.WebControls etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top