SPMonthPickerControl Class
System.Object
System.Web.UI.Control
Microsoft.SharePoint.WebControls.SPDatePickerControl
Microsoft.SharePoint.WebControls.SPMonthPickerControl
System.Web.UI.Control
Microsoft.SharePoint.WebControls.SPDatePickerControl
Microsoft.SharePoint.WebControls.SPMonthPickerControl
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
TimeZone
As an addendum to the previous comment, you'll need to add a TimeSpan property to the TimeZone property. This value is the difference between UTC time and your local time. (For instance, for the Central Time Zone, the offset is 5 hours). If you don't populate that property, you might get an exception. Here's an example of a web part that contains the month picker control:
{
protectedoverridevoid CreateChildControls(){
SPMonthPickerControl monthPicker = newSPMonthPickerControl();monthPicker.Calendar =
SPCalendarType.Gregorian;monthPicker.LocaleId = 1033;
monthPicker.StartOffset = -12;
monthPicker.EndOffset = 12;
monthPicker.TimeZone =
newTimeSpan(5, 0, 0); this.Controls.Add(monthPicker);}
}
- 5/3/2012
- Becky Bertram [MVP]
SPMonthPickerControl
Description
The Microsoft.SharePoint.WebControls.SPMonthPickerControl class inherits from the Microsoft.SharePoint.WebControls.SPDatePickerControl class which is responsible for providing a Control using Microsoft.SharePoint.WebControls.DatePicker to build a date selection interface. The base SPDatePickerControl class supplies a majority of the functionality to the derived SPMonthPickerControl with base default functionality, however the selector options are constrained to month values.
The control functions in a simple fashion using a DatePicker object, and casting it to a Microsoft.SharePoint.WebControls.MonthPicker object to support proper constraints to trim appropriate display values. The DatePicker.DateTimeOptions are set with Microsoft.SharePoint.Utilities.DateOptions deriving initialization values from the inherited SPDatePickerControl class. The control is rendered to the output stream lastly using Microsoft.SharePoint.WebControls.DatePicker.RenderAsHtml which will push the HTML representation to a StringBuilder which can subsequently use StringBuilder.ToString() to append respective data.
The Usage Scenario
The most frequent use of the SPMonthPickerControl is within custom SharePoint field types; however since it is a regular Control can be extended into several custom interfaces such as WebParts or User Controls. Primary internal representation can be found in date selection pieces such as Microsoft.SharePoint.WebControls.SPCalendarNavigator.
In the below, we are creating a new SPMonthPickerControl object, settings some of the basic properties, and adding it the current instance control collection. It should be noted that while SPMonthPickerControl is simple to use since it adheres to unpretentious object use congruent to orthodox controls, depending on where the control is called may require additional attributes written to the output stream, notably the JavaScript and CSS references. Notice the localization (English/1033) attributes that might have to be accounted for as well with a particular environment, depending.
C# Code Example
private SPMonthPickerControl _monthPicker;
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<script type=\"text/javascript\"src=\"/_layouts/datepicker.js\"></script>");
writer.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/_layouts/1033/styles/datepicker.css\" />");
_monthPicker = new SPMonthPickerControl();
_monthPicker.Calendar = SPCalendarType.Gregorian;
Controls.Add(_monthPicker);
base.CreateChildControls();
}
Visual Basic .NET Code Example
private _monthPicker As SPMonthPickerControl
Protected Overloads Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
writer.Write("<script type=""text/javascript""src=""/_layouts/datepicker.js""></script>")
writer.Write("<link rel=""stylesheet"" type=""text/css"" href=""/_layouts/1033/styles/datepicker.css"" />")
_monthPicker = New SPMonthPickerControl()
_monthPicker.Calendar = SPCalendarType.Gregorian
Controls.Add(_monthPicker)
MyBase.CreateChildControls()
End Sub
The Microsoft.SharePoint.WebControls.SPMonthPickerControl class inherits from the Microsoft.SharePoint.WebControls.SPDatePickerControl class which is responsible for providing a Control using Microsoft.SharePoint.WebControls.DatePicker to build a date selection interface. The base SPDatePickerControl class supplies a majority of the functionality to the derived SPMonthPickerControl with base default functionality, however the selector options are constrained to month values.
The control functions in a simple fashion using a DatePicker object, and casting it to a Microsoft.SharePoint.WebControls.MonthPicker object to support proper constraints to trim appropriate display values. The DatePicker.DateTimeOptions are set with Microsoft.SharePoint.Utilities.DateOptions deriving initialization values from the inherited SPDatePickerControl class. The control is rendered to the output stream lastly using Microsoft.SharePoint.WebControls.DatePicker.RenderAsHtml which will push the HTML representation to a StringBuilder which can subsequently use StringBuilder.ToString() to append respective data.
The Usage Scenario
The most frequent use of the SPMonthPickerControl is within custom SharePoint field types; however since it is a regular Control can be extended into several custom interfaces such as WebParts or User Controls. Primary internal representation can be found in date selection pieces such as Microsoft.SharePoint.WebControls.SPCalendarNavigator.
In the below, we are creating a new SPMonthPickerControl object, settings some of the basic properties, and adding it the current instance control collection. It should be noted that while SPMonthPickerControl is simple to use since it adheres to unpretentious object use congruent to orthodox controls, depending on where the control is called may require additional attributes written to the output stream, notably the JavaScript and CSS references. Notice the localization (English/1033) attributes that might have to be accounted for as well with a particular environment, depending.
C# Code Example
private SPMonthPickerControl _monthPicker;
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<script type=\"text/javascript\"src=\"/_layouts/datepicker.js\"></script>");
writer.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/_layouts/1033/styles/datepicker.css\" />");
_monthPicker = new SPMonthPickerControl();
_monthPicker.Calendar = SPCalendarType.Gregorian;
Controls.Add(_monthPicker);
base.CreateChildControls();
}
Visual Basic .NET Code Example
private _monthPicker As SPMonthPickerControl
Protected Overloads Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
writer.Write("<script type=""text/javascript""src=""/_layouts/datepicker.js""></script>")
writer.Write("<link rel=""stylesheet"" type=""text/css"" href=""/_layouts/1033/styles/datepicker.css"" />")
_monthPicker = New SPMonthPickerControl()
_monthPicker.Calendar = SPCalendarType.Gregorian
Controls.Add(_monthPicker)
MyBase.CreateChildControls()
End Sub
- 6/3/2010
- Adam Buenz - MVP