|
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SubSonic
{
/// <summary>
/// Summary description for CalendarControl.
/// </summary>
public class CalendarControl : TextBox
{
private const string DEFAULT_INVALID_DATE = "Please enter a valid date.";
private const string DEFAULT_FORMAT = "MM/dd/yyyy";
private const string DEFAULT_JAVASCRIPT_FORMAT = "%m/%d/%Y";
private const string DEFAULT_LANGUAGE = "en";
private Image imgCalendar;
private DateTime? selectedDate;
private string displayFormat;
private string javaScriptFormat;
private string language;
private bool showTime = true;
public CalendarControl()
{
//DisplayFormat = DEFAULT_FORMAT;
//JavaScriptFormat = DEFAULT_JAVASCRIPT_FORMAT;
//Language = DEFAULT_LANGUAGE;
DisplayFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
if (!String.IsNullOrEmpty(DisplayFormat))
{
char splitter = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator[0];
string[] format = DisplayFormat.Split(new char[] {splitter});
StringBuilder sbFormat = new StringBuilder();
for(int i = 0; i<format.Length; i++)
{
sbFormat.Append("%");
sbFormat.Append(format[i][0]);
if(i + 1 != format.Length)
{
sbFormat.Append(splitter);
}
}
JavaScriptFormat = sbFormat.ToString().ToLower();
}
else
{
DisplayFormat = DEFAULT_FORMAT;
JavaScriptFormat = DEFAULT_JAVASCRIPT_FORMAT;
}
Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
}
private Image CalendarImage
{
get
{
if (imgCalendar == null)
{
imgCalendar = new Image();
imgCalendar.ID = "imgCalendar" + ClientID;
imgCalendar.ImageAlign = ImageAlign.AbsMiddle;
imgCalendar.ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.skin.calendar.gif");
//StylePath + "calendar.gif";
imgCalendar.Style.Add("margin-left", "5px");
}
return imgCalendar;
}
}
[DefaultValue(DEFAULT_FORMAT)]
public string DisplayFormat
{
get { return displayFormat; }
set { displayFormat = value; }
}
[DefaultValue(DEFAULT_JAVASCRIPT_FORMAT)]
public string JavaScriptFormat
{
get { return javaScriptFormat; }
set { javaScriptFormat = value; }
}
[DefaultValue(DEFAULT_LANGUAGE)]
public string Language
{
get { return language; }
set { language = value; }
}
public bool ShowTime
{
get { return showTime; }
set { showTime = value; }
}
public DateTime? SelectedDate
{
get
{
string selDate = Text.Trim();
if (!String.IsNullOrEmpty(selDate))
{
selectedDate = DateTime.Parse(selDate);
}
else
{
selectedDate = null;
}
return selectedDate;
}
set
{
selectedDate = value;
if (selectedDate.HasValue)
{
DateTime dt = selectedDate.Value;
Text = dt.ToString(DisplayFormat);
}
else
{
Text = String.Empty;
}
}
}
protected override void OnPreRender(EventArgs e)
{
string csslink = "<link href='" + Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.skin.theme.css") + "' rel='stylesheet' type='text/css' />";
Page.Header.Controls.Add(new LiteralControl(csslink));
Page.ClientScript.RegisterClientScriptInclude("CalendarMain", Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.calendar.js"));
Page.ClientScript.RegisterClientScriptInclude("CalendarSetup", Page.ClientScript.GetWebResourceUrl(GetType(), "SubSonic.Controls.Calendar.calendar-setup.js"));
const string langPrefix = "SubSonic.Controls.Calendar.lang.calendar-";
if(String.IsNullOrEmpty(Language))
{
Language = DEFAULT_LANGUAGE;
}
if (Assembly.GetExecutingAssembly().GetManifestResourceStream(langPrefix + Language + ".js") == null)
{
Page.ClientScript.RegisterClientScriptInclude("CalendarLanguage", Page.ClientScript.GetWebResourceUrl(GetType(), langPrefix + DEFAULT_LANGUAGE + ".js"));
}
else
{
Page.ClientScript.RegisterClientScriptInclude("CalendarLanguage", Page.ClientScript.GetWebResourceUrl(GetType(), langPrefix + Language + ".js"));
}
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
if (SelectedDate == DateTime.MinValue)
{
SelectedDate = DateTime.Now;
}
writer.WriteLine("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
writer.WriteLine("<tr>");
writer.WriteLine("<td>");
base.Render (writer);
writer.WriteLine("</td><td>");
CalendarImage.RenderControl(writer); // render CalendarButton object
writer.WriteLine("</td>");
writer.WriteLine("</tr>");
writer.WriteLine("</table>");
Page.ClientScript.RegisterStartupScript(typeof(Page), "Calendar" + ClientID,
"<script type=\"text/javascript\">" +
"Calendar.setup( { " +
"inputField: \"" + ClientID + "\", " +
"ifFormat: \"" + JavaScriptFormat + "\", " +
"button: \"" + CalendarImage.ClientID + "\", " +
"date: \"" + SelectedDate + "\", " +
"showsTime: " + (ShowTime ? "true" : "false") + " " +
"} );" +
"</script>");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.