Referencing the template in the control
To reference the template in the date picker control, perform the following:
-
Reference the embedded template by pasting the following code into DatePickerField.cs:
protected override string LayoutTemplateName
{
get
{
return "DatePicker.DatePickerTemplate.ascx";
}
}
-
Provide reference to the RadDatePicker control by pasting the following code:
protected virtual RadDatePicker DatePicker
{
get
{
return this.Container.GetControl<RadDatePicker>("datePicker", true);
}
}
-
In the same way, provide properties for the additional fields you present:
protected override WebControl DescriptionControl
{
get
{
return this.Container.GetControl<Label>("descriptionLabel", true);
}
}
protected override WebControl ExampleControl
{
get
{
return this.Container.GetControl<Label>("exampleLabel", true);
}
}
protected override WebControl TitleControl
{
get
{
return this.Container.GetControl<Label>("titleLabel", true);
}
}
-
In the InitializeControls method, provide default values for the properties by pasting the following code:
protected override void InitializeControls(GenericContainer container)
{
this.Value = DateTime.Now;
if (this.Minimum != null)
{
this.DatePicker.MinDate = (DateTime)serializer.Deserialize(this.Minimum, typeof(DateTime));
}
if (this.Maximum != null)
{
this.DatePicker.MaxDate = (DateTime)serializer.Deserialize(this.Maximum, typeof(DateTime));
}
(this.TitleControl as Label).Text = this.Title;
(this.DescriptionControl as Label).Text = this.Description;
(this.ExampleControl as Label).Text = this.Example;
}
-
Save the file.
To specify a default template for the control, you must override the LayoutTemplateName property in the DatePickerField class. It must return a string that is a reference to the embedded resource that represents your template.
The template that you defined in Creating the template of the control, uses the RadDatePicker control for its main UI element. You must create an instance of the control to access the selected date. To reference the controls from the DatePickerTemplate you must callGetControl and return the controls’ values.
In the InitializeControls method, you provide default values for every control in the template.
NOTE: When using templates make sure you address the controls after they have been initialized. The moment, where the template is initialized and ready to use, is the InitializeControls method. In it and after its execution, you can safely call your controls. You can call theEnsureChildControls() method before every reference to a template control.
Next, you must implement the features of the date picker control. For more information, see Implementing the features of the control.