using System; using SystemWebUI;
using SystemWebUIWebControls;
using SystemComponentModel;
using SystemDrawing;
namespace AquaCalendar
{
[DefaultProperty(Text)
//在属性工具箱中显示的默认属性
ToolboxData(<{}:DatePicker runat=server>)]
public class DatePicker :
SystemWebUIWebControlsWebControl
IPostBackEventHandler
{
//选择日期按钮的默认样式
private const string _BUTTONDEFAULTSTYLE = BORDERRIGHT: gray px solid; BORDERTOP: gray px solid; BORDERLEFT: gray px solid; CURSOR: hand; BORDERBOTTOM: gray px solid;;
//按钮默认文本
private const string _BUTTONDEFAULTTEXT = ;
private SystemWebUIWebControlsCalendar _Calendar;
public override ControlCollection Controls
{
get
{
EnsureChildControls(); //确认子控件集都已被创建
return baseControls;
}
}
//创建子控件(服务器日历控件)
protected override void CreateChildControls()
{
ControlsClear();
_Calendar = new Calendar();
_CalendarID = MyCalendarID;
_CalendarSelectedDate = DateTimeParse(Text);
_CalendarTitleFormat = TitleFormatMonthYear;
_CalendarNextPrevFormat = NextPrevFormatShortMonth;
_CalendarCellSpacing = ;
_CalendarFontSize = FontUnitParse(pt);
_CalendarFontName = Verdana;
_CalendarSelectedDayStyleBackColor = ColorTranslatorFromHtml(#);
_CalendarSelectedDayStyleForeColor = ColorTranslatorFromHtml(White);
_CalendarDayStyleBackColor = ColorTranslatorFromHtml(#CCCCCC);
_CalendarTodayDayStyleBackColor = ColorTranslatorFromHtml(#);
_CalendarTodayDayStyleForeColor = ColorTranslatorFromHtml(Aqua);
_CalendarDayHeaderStyleFontSize = FontUnitParse(pt);
_CalendarDayHeaderStyleFontBold = true;
_CalendarDayHeaderStyleHeight = UnitParse(pt);
_CalendarDayHeaderStyleForeColor = ColorTranslatorFromHtml(#);
_CalendarNextPrevStyleFontSize = FontUnitParse(pt);
_CalendarNextPrevStyleFontBold = true;
_CalendarNextPrevStyleForeColor = ColorTranslatorFromHtml(White);
_CalendarTitleStyleFontSize = FontUnitParse(pt);
_CalendarTitleStyleFontBold = true;
_CalendarTitleStyleHeight = UnitParse(pt);
_CalendarTitleStyleForeColor = ColorTranslatorFromHtml(White);
_CalendarTitleStyleBackColor = ColorTranslatorFromHtml(#);
_CalendarOtherMonthDayStyleForeColor = ColorTranslatorFromHtml(#);
_CalendarNextPrevFormat = NextPrevFormatCustomText;
_CalendarNextMonthText = 下月;
_CalendarPrevMonthText = 上月;
_CalendarStyleAdd(displaynone); //默认不显示下拉日历控件
_CalendarSelectionChanged += new EventHandler(_Calendar_SelectionChanged);
thisControlsAdd(_Calendar);
}
[
Category(Appearance) //该属性所属类别参见图
DefaultValue() //属性默认值
Description(设置该日期控件的值) //属性的描述
]
public string Text
{
get
{
EnsureChildControls();
return (ViewState[Text] == null)?SystemDateTimeTodayToString(yyyyMMdd):ViewState[Text]ToString();
}
set
{
EnsureChildControls();
DateTime dt = SystemDateTimeToday;
try
{
dt = DateTimeParse(value);
}
catch
{
throw new ArgumentOutOfRangeException(请输入日期型字符串(例如)!);
}
ViewState[Text] = DateFormat == CalendarEnumLongDateTime?dtToString(yyyyMMdd):dtToString(yyyyMd);
}
}
//重载服务器控件的Enabled属性将选择日期按钮变灰(禁用)
public override bool Enabled
{
get
{
EnsureChildControls();
return ViewState[Enabled] == null?true:(bool)ViewState[Enabled];
}
set
{
EnsureChildControls();
ViewState[Enabled] = value;
}
}
public string ButtonStyle
{
get
{
EnsureChildControls();
object o = ViewState[ButtonSytle];
return (o == null)?_BUTTONDEFAULTSTYLE:oToString();
}
set
{
EnsureChildControls();
ViewState[ButtonSytle] = value;
}
}
[
DefaultValue(CalendarEnumLongDateTime)
]
public CalendarEnum DateFormat
{
get
{
EnsureChildControls();
object format = ViewState[DateFormat];
return format == null?CalendarEnumLongDateTime:(CalendarEnum)format;
}
set
{
EnsureChildControls();
ViewState[DateFormat] = value;
DateTime dt = DateTimeParse(Text);
Text=DateFormat == CalendarEnumLongDateTime?dtToString(yyyyMMdd):dtToString(yyyyMd);
}
}
[
Browsable(false)
DesignerSerializationVisibility(DesignerSerializationVisibilityHidden)
]
public string MyCalendarID //复合控件ID
{
get
{
EnsureChildControls();
return thisClientID+_MyCalendar;
}
}
[
Browsable(false)
DesignerSerializationVisibility(DesignerSerializationVisibilityHidden)
]
public string MyCalendarName //复合控件名称
{
get
{
EnsureChildControls();
return thisUniqueID+:MyCalendar;
}
}
[
Browsable(false)
DesignerSerializationVisibility(DesignerSerializationVisibilityHidden)
]
public string DatePickerInputID //复合控件中输入框的ID
{
get
{
EnsureChildControls();
return thisClientID+_DateInput;
}
}
[
Browsable(false)
DesignerSerializationVisibility(DesignerSerializationVisibilityHidden)
]
public string DatePickerInputName //复合控件中输入框的名称
{
get
{
EnsureChildControls();
return thisUniqueID+:DateInput;
}
}
[
Browsable(false)
DesignerSerializationVisibility(DesignerSerializationVisibilityHidden)
]
public string DatePickerButtonID //复合控件中按钮的ID
{
get
{
EnsureChildControls();
return thisClientID+_DateButton;
}
}
[
Browsable(false)
DesignerSerializationVisibility(DesignerSerializationVisibilityHidden)
]
public string DatePickerButtonName //复合控件中按钮的名称
{
get
{
EnsureChildControls();
return thisUniqueID+:DateButton;
}
}
public string ButtonText
{
get
{
EnsureChildControls();
return ViewState[ButtonText] == null?_BUTTONDEFAULTTEXT:(string)ViewState[ButtonText];
}
set
{
EnsureChildControls();
ViewState[ButtonText] = value;
}
}
/// /// 将此控件呈现给指定的输出参数
///
///
要写出到的 HTML 编写器
protected override void Render(HtmlTextWriter output)
{
//在页面中输出控件时产生一个表格(二行二列)以下是表格的样式
outputAddAttribute(HtmlTextWriterAttributeCellspacing );
outputAddAttribute(HtmlTextWriterAttributeBorder );
outputAddAttribute(HtmlTextWriterAttributeCellpadding );
outputAddStyleAttribute(LEFT thisStyle[LEFT]);
outputAddStyleAttribute(TOP thisStyle[TOP]);
outputAddStyleAttribute(POSITION absolute);
if (Width != UnitEmpty)
{
outputAddStyleAttribute(HtmlTextWriterStyleWidth WidthToString());
}
else
{
outputAddStyleAttribute(HtmlTextWriterStyleWidth px);
}
outputRenderBeginTag(HtmlTextWriterTagTable); //输出表格
outputRenderBeginTag(HtmlTextWriterTagTr); //表格第一行
outputAddAttribute(HtmlTextWriterAttributeWidth %);
outputRenderBeginTag(HtmlTextWriterTagTd);
//以下是第一行第一列中文本框的属性及其样式设置
if (!Enabled)
{
outputAddAttribute(HtmlTextWriterAttributeReadOnly true);
}
outputAddAttribute(HtmlTextWriterAttributeType Text);
outputAddAttribute(HtmlTextWriterAttributeId DatePickerInputID);
outputAddAttribute(HtmlTextWriterAttributeName DatePickerInputName);
outputAddAttribute(HtmlTextWriterAttributeValue Text);
outputAddStyleAttribute(HtmlTextWriterStyleWidth %);
outputAddStyleAttribute(HtmlTextWriterStyleHeight %);
outputAddStyleAttribute(HtmlTextWriterStyleFontFamily FontName);
outputAddStyleAttribute(HtmlTextWriterStyleFontSize FontSizeToString());
outputAddStyleAttribute(HtmlTextWriterStyleFontWeight FontBold?bold:);
outputAddStyleAttribute(HtmlTextWriterStyleBackgroundColor ColorTranslatorToHtml(BackColor));
outputAddStyleAttribute(HtmlTextWriterStyleColor ColorTranslatorToHtml(ForeColor));
outputRenderBeginTag(HtmlTextWriterTagInput); //输出文本框
outputRenderEndTag();
outputRenderEndTag();
outputAddAttribute(HtmlTextWriterAttributeWidth *);
outputRenderBeginTag(HtmlTextWriterTagTd);
//以下是第一行第二列中按钮的属性及其样式设置
if (!Enabled)
{
outputAddAttribute(HtmlTextWriterAttributeDisabled true);
}
outputAddAttribute(HtmlTextWriterAttributeType Submit);
outputAddAttribute(HtmlTextWriterAttributeId DatePickerButtonID);
outputAddAttribute(HtmlTextWriterAttributeName DatePickerButtonName);
outputAddAttribute(HtmlTextWriterAttributeValue ButtonText);
outputAddStyleAttribute(HtmlTextWriterStyleWidth %);
outputAddAttribute(HtmlTextWriterAttributeOnclick PageGetPostBackEventReference(this)); //点击按钮时需要回传服务器来触发后面的OnClick事件
outputAddAttribute(HtmlTextWriterAttributeStyle ButtonStyle);
outputRenderBeginTag(HtmlTextWriterTagInput); //输出按钮
outputRenderEndTag();
outputRenderEndTag();
outputRenderEndTag();
outputRenderBeginTag(HtmlTextWriterTagTr);
outputAddAttribute(HtmlTextWriterAttributeColspan );
outputRenderBeginTag(HtmlTextWriterTagTd);
_CalendarRenderControl(output); //将日历子控件输出
outputRenderEndTag();
outputRenderEndTag();
outputRenderEndTag();
}
//复合控件必须继承IpostBackEventHandler接口才能继承RaisePostBackEvent事件
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgsEmpty);
}
protected virtual void OnClick(EventArgs e)
{
//点击选择日期按钮时如果日历子控件没有显示则显示出来并将文本框的值赋值给日历子控件
if (_CalendarAttributes[display] != )
{
_CalendarSelectedDate = DateTimeParse(Text);
_CalendarStyleAdd(display);
}
}
//复合控件中的日历控件日期变化事件
private void _Calendar_SelectionChanged(object sender EventArgs e)
{
//当选择的日期变化时将所选日期赋值给文本框并将日历子控件隐藏
Text = _CalendarSelectedDateToString();
_CalendarStyleAdd(displaynone);
}
}
}