发布一个日期选择控件(ASPNET2.0)
时间:2022-04-23
本文章向大家介绍发布一个日期选择控件(ASPNET2.0),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
The Coolest DHTML Calendar,这是一个在GPL下发布的JS日历程序,具有极高的可配置性,包括外观样式、显示格式、显示内容等等。默认程序是只提供日期选择的,需要设置几个showtime参数才能显示时间选择。下载的程序包(zip)里面已经有详细的说明文档和例子. 这个控件的aspnet 1.1版本的代码在我的donews blog上,代码可以自由修改发布.
代码和示例下载地址:demo
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Globalization;
namespace DotnetClubPortal.WebControls
{
/// <summary>
/// An ASP.NET Server Control that wraps mishoo's javascript calendar
/// (http://www.dynarch.com/projects/calendar/).
/// </summary>
[DefaultProperty("Text"), ToolboxData("<{0}:Calendar runat=server></{0}:Calendar>")]
[ValidationProperty("Text")]
public class Calendar : CompositeControl, INamingContainer
{
private TextBox _dateTextBox;
private Image _calendarImage;
#region properties
[Bindable(true), Category("Appearance"), DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return this._dateTextBox.Text;
}
set
{
EnsureChildControls();
this._dateTextBox.Text = value;
}
}
[Bindable(true), Category("Appearance"), DefaultValue(CalendarTheme.system)]
public CalendarTheme Theme
{
get { return (ViewState["Theme"] != null ? (CalendarTheme)ViewState["Theme"] : CalendarTheme.system); }
set { ViewState["Theme"] = value; }
}
[Bindable(true), Category("Behavior"), DefaultValue(CalendarLanguage.en)]
public CalendarLanguage Language
{
get { return (ViewState["Language"] != null ? (CalendarLanguage)ViewState["Language"] : CalendarLanguage.en); }
set { ViewState["Language"] = value; }
}
[Bindable(true), Category("Behavior"), DefaultValue("~/Support/JsCalendar")]
public string SupportDir
{
get { return (ViewState["SupportDir"] != null ? (string)ViewState["SupportDir"] : ""); }
set { ViewState["SupportDir"] = value; }
}
[Bindable(true), Category("Behavior"), DefaultValue(false)]
public bool DisplayTime
{
get { return (ViewState["DisplayTime"] != null ? (bool)ViewState["DisplayTime"] : false); }
set { ViewState["DisplayTime"] = value; }
}
[Browsable(false), Bindable(false)]
public string DateFormat
{
get { return (ViewState["DateFormat"] != null ? (string)ViewState["DateFormat"] : ""); }
set { ViewState["DateFormat"] = value; }
}
[Browsable(false), Bindable(false)]
public string TimeFormat
{
get { return (ViewState["TimeFormat"] != null ? (string)ViewState["TimeFormat"] : ""); }
set { ViewState["TimeFormat"] = value; }
}
[Bindable(true), Category("Behavior")]
public DateTime SelectedDate
{
get
{
EnsureChildControls();
if (this.Text.Length > 0)
{
try
{
return DateTime.Parse(this.Text);
}
catch (FormatException ex)
{
System.Diagnostics.Trace.WriteLine("Invalid datetime: " + this._dateTextBox.Text + " " + ex.Message, this.GetType().FullName);
return DateTime.MinValue;
}
}
else
{
return DateTime.MinValue;
}
}
set
{
EnsureChildControls();
this.Text = value.ToShortDateString();
if (this.DisplayTime)
{
this.Text += " " + value.ToShortTimeString();
}
}
}
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
[TypeConverter(typeof(UnitConverter))]
public override Unit Width
{
get
{
EnsureChildControls();
return base.Width;
}
set
{
EnsureChildControls();
base.Width = value;
this._dateTextBox.Width = Unit.Pixel((int)value.Value - 24);
}
}
#endregion
public event System.EventHandler DateChanged;
protected virtual void OnDateChanged(object sender)
{
if (DateChanged != null)
{
DateChanged(sender, System.EventArgs.Empty);
}
}
public Calendar()
{
// Set defaults
this.Theme = CalendarTheme.system;
this.SupportDir = "~/Support/JsCalendar";
this.DisplayTime = false;
this.DateFormat = ConvertDateFormat(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
this.TimeFormat = ConvertTimeFormat(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
SetLanguage();
}
protected override void CreateChildControls()
{
this._dateTextBox = new TextBox();
this._calendarImage = new Image();
this._dateTextBox.EnableViewState = true;
this._dateTextBox.ID = "dateTextBox";
this._dateTextBox.TextChanged += new EventHandler(DateTextBox_TextChanged);
this._calendarImage.EnableViewState = false;
this._calendarImage.ID = "trigger";
this._calendarImage.ImageUrl = GetClientFileUrl("cal.gif");
this._calendarImage.Attributes["align"] = "top";
this._calendarImage.Attributes["hspace"] = "4";
Controls.Add(this._dateTextBox);
Controls.Add(this._calendarImage);
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
if (this.Site != null && this.Site.DesignMode)
{
this._dateTextBox.RenderControl(output);
output.Write("[" + this.ID + "]");
}
else
{
base.Render(output);
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
string themeCss = GetClientCssImport(String.Format("calendar-{0}.css", this.Theme.ToString().Replace("_", "-")));
Page.ClientScript.RegisterClientScriptBlock(themeCss.GetType(),"calendarcss", themeCss);
string calendarScripts = "";
calendarScripts += GetClientScriptInclude("calendar.js",this.Language);
calendarScripts += GetClientScriptInclude("calendar-setup.js",this.Language);
string languageFile = String.Format("lang/calendar-{0}.js", this.Language.ToString());
calendarScripts += GetClientScriptInclude(languageFile,this.Language);
Page.ClientScript.RegisterClientScriptBlock(calendarScripts.GetType(), "calendarscripts", calendarScripts);
string setupScript = GetCalendarSetupScript(this._dateTextBox.ClientID, GetFormatString(), this.ClientID);
Page.ClientScript.RegisterStartupScript(setupScript.GetType(), this.ClientID + "script", setupScript);
}
private void SetLanguage()
{
string currentLanguage = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
try
{
CalendarLanguage cl = (CalendarLanguage)Enum.Parse(typeof(CalendarLanguage), currentLanguage);
this.Language = cl;
}
catch
{
// Default is 'en'
this.Language = CalendarLanguage.en;
}
}
private string GetClientFileUrl(string fileName)
{
return ResolveUrl(this.SupportDir + "/" + fileName);
}
private string GetClientScriptInclude(string scriptFile,CalendarLanguage language)
{
string strScript = string.Empty;
if(language == CalendarLanguage.zh)
{
strScript = "<script language="JavaScript" src="" +
GetClientFileUrl(scriptFile) + "" charset="gb2312"></script>n";
}
else
{
strScript = "<script language="JavaScript" src="" +
GetClientFileUrl(scriptFile) + ""></script>n";
}
return strScript;
}
private string GetClientCssImport(string fileName)
{
return "<style type="text/css">@import url(" + GetClientFileUrl(fileName) + ");</style>";
}
private string GetCalendarSetupScript(string inputField, string format, string trigger)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script type="text/javascript">Calendar.setup( { inputField : "");
sb.Append(inputField);
sb.Append("", ifFormat : "");
sb.Append(format);
sb.Append("", button : "");
sb.Append(trigger);
sb.Append("", showsTime : ");
sb.Append(this.DisplayTime.ToString().ToLower());
sb.Append(" } ); </script>");
return sb.ToString();
}
public string GetFormatString()
{
if (this.DisplayTime)
{
return this.DateFormat + " " + this.TimeFormat;
}
else
{
return this.DateFormat;
}
}
private void DateTextBox_TextChanged(object sender, EventArgs e)
{
OnDateChanged(sender);
}
private string ConvertDateFormat(string shortDateFormat)
{
string tempFormat = shortDateFormat.Replace("yyyy", "%Y");
tempFormat = tempFormat.Replace("M", "%m");
tempFormat = tempFormat.Replace("d", "%d");
return tempFormat;
}
private string ConvertTimeFormat(string shortTimeFormat)
{
string tempFormat = shortTimeFormat.Replace("H", "%H");
tempFormat = tempFormat.Replace("mm", "%M");
tempFormat = tempFormat.Replace("h", "%I");
tempFormat = tempFormat.Replace("tt", "%p");
return tempFormat;
}
}
public enum CalendarTheme
{
blue,
blue2,
brown,
green,
win2k_1,
win2k_2,
win2k_cold_1,
win2k_cold_2,
system
}
public enum CalendarLanguage
{
af,
br,
ca,
da,
de,
du,
el,
en,
es,
fi,
fr,
hr,
hu,
it,
jp,
ko,
lt,
nl,
no,
pl,
pt,
ro,
ru,
sl,
si,
sk,
sp,
sv,
tr,
zh
}
}
- eclipse: workspace出错导致无法启用的解决
- 【node错误】/usr/bin/env: node: No such file or directory
- Django比较相等或者不相等的模板语法ifequal / ifnotequal
- 使用testNGListenter来自定义日志
- 通过代码去执行testNG用例
- $.cookie is not a function;原因及解决办法
- 为何学习以及如何理解SSH框架?内含Hibernate学习指南
- 生成唯一标识 字符串跟时间戳的结合
- git命令中带有特殊符号如@
- centos下安装jenkins
- Django实现任意文件上传(最简单的方法)
- 微信小程序HCE能力全面开放,手机秒变公交卡
- spring-自动加载配置文件使用属性文件注入
- 约束
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Laravel 路由使用入门
- Go 数据存储篇(六):数据表之间的关联关系和关联查询
- 搭建nextcloud私有云存储网盘的教程详解
- Laravel 路由使用进阶
- 使用dig/nslookup命令查看dns解析的方法步骤
- Go 数据存储篇(七):GORM 使用入门
- 在 Linux 上使用 Multitail命令的教程
- 深入理解linux执行文件提示No such file or directory的背后原因
- Laravel 控制器:从 MVC 模式聊起
- 基于yolo5工地安全帽和禁入危险区域识别系统,附数据集
- Go 常见并发模式实现(一):调度后台处理任务的作业程序
- 路由使用进阶(二)
- linux No space left on device由索引节点(inode)爆满引发500问题
- linux中批量添加文件前缀的操作方法
- Go 常见并发模式实现(二):通过缓冲通道实现共享资源池