Return to doc.sitecore.com

5.  Override the OnLoad Method
Prev Next

You must override the Sitecore.Web.UI.HtmlControls.Control.OnLoad method, define the controls you use, initialize them and add them to HtmlControls.Control.Controls.

 

For the sake of performance controls should only be created once (during the first load). To achieve this generate them when IsEvent property of the Sitecore.Context.ClientPage is set to false.

 

The value of the control is stored in the HtmlControls.Control.Value property. It should be renewed every time the OnLoad method is called (that is, when IsEvent property is true).

 

To save the value of the control you should get the HtmlControls.Control.Value property derived from HtmlControls.Control, or you can implement the Sitecore.Shell.Applications.ContentEditor.IContentField interface (both variants have the same result).

1 protected override void OnLoad(EventArgs e)
2       {
3          if(!Sitecore.Context.ClientPage.IsEvent)
4          {
5             isEvent = false;
6             Sitecore.Shell.Applications.ContentEditor.Checklist list = new Sitecore.Shell.Applications.ContentEditor.Checklist();
7             this.Controls.Add(list);
8             list.ID = GetID("list");
9             list.Source = this.Source;
10             list.ItemID = ItemID;
11             list.FieldName = FieldName;
12             list.TrackModified = TrackModified;
13             list.Disabled = this.Disabled;
14             list.Value = this.Value;
15             
16
17             Sitecore.Shell.Applications.ContentEditor.Text text = new Sitecore.Shell.Applications.ContentEditor.Text();
18             this.Controls.AddAt(0, text);
19             text.ID = GetID("text");
20             text.ReadOnly = true;
21             text.Disabled = this.Disabled;
22
23             this.Controls.Add(new LiteralControl(Sitecore.Resources.Images.GetSpacer(0x18, 16)));
24          }
25          else
26          {
27             Sitecore.Shell.Applications.ContentEditor.Checklist list = FindControl(GetID("list")) as Sitecore.Shell.Applications.ContentEditor.Checklist;  
28             if(list != null)
29             {
30                ListString valueList = new ListString();
31                foreach (DataChecklistItem item in list.Items)
32                {
33                   if (item.Checked)
34                   {
35                      valueList.Add(item.ItemID);
36                   }
37                }
38                if (this.Value != valueList.ToString())
39                {
40                   this.TrackModified = list.TrackModified;
41                   this.SetModified();
42                }
43                this.Value = valueList.ToString();
44             }
45          }
46          base.OnLoad (e);
47       }

Prev Next