using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.drawing;
namespace aquacalendar
{
[defaultproperty("text"), //在属性工具箱中显示的默认属性
toolboxdata("<{0}:datepicker runat=server>")]
public class datepicker : system.web.ui.webcontrols.webcontrol , ipostbackeventhandler
{
//选择日期按钮的默认样式
private const string _buttondefaultstyle = "border-right: gray 1px solid; border-top: gray 1px solid; border-left: gray 1px solid; cursor: hand; border-bottom: gray 1px solid;";
//按钮默认文本
private const string _buttondefaulttext = "...";
private system.web.ui.webcontrols.calendar _calendar;
public override controlcollection controls
{
get
{
ensurechildcontrols(); //确认子控件集都已被创建
return base.controls;
}
}
//创建子控件(服务器日历控件)
protected override void createchildcontrols()
{
controls.clear();
_calendar = new calendar();
_calendar.id = mycalendarid;
_calendar.selecteddate = datetime.parse(text);
_calendar.titleformat = titleformat.monthyear;
_calendar.nextprevformat = nextprevformat.shortmonth;
_calendar.cellspacing = 0;
_calendar.font.size = fontunit.parse("9pt");
_calendar.font.name = "verdana";
_calendar.selecteddaystyle.backcolor = colortranslator.fromhtml("#333399");
_calendar.selecteddaystyle.forecolor = colortranslator.fromhtml("white");
_calendar.daystyle.backcolor = colortranslator.fromhtml("#cccccc");
_calendar.todaydaystyle.backcolor = colortranslator.fromhtml("#999999");
_calendar.todaydaystyle.forecolor = colortranslator.fromhtml("aqua");
_calendar.dayheaderstyle.font.size = fontunit.parse("8pt");
_calendar.dayheaderstyle.font.bold = true;
_calendar.dayheaderstyle.height = unit.parse("8pt");
_calendar.dayheaderstyle.forecolor = colortranslator.fromhtml("#333333");
_calendar.nextprevstyle.font.size = fontunit.parse("8pt");
_calendar.nextprevstyle.font.bold = true;
_calendar.nextprevstyle.forecolor = colortranslator.fromhtml("white");
_calendar.titlestyle.font.size = fontunit.parse("12pt");
_calendar.titlestyle.font.bold = true;
_calendar.titlestyle.height = unit.parse("12pt");
_calendar.titlestyle.forecolor = colortranslator.fromhtml("white");
_calendar.titlestyle.backcolor = colortranslator.fromhtml("#333399");
_calendar.othermonthdaystyle.forecolor = colortranslator.fromhtml("#999999");
_calendar.nextprevformat = nextprevformat.customtext;
_calendar.nextmonthtext = "下月";
_calendar.prevmonthtext = "上月";
_calendar.style.add("display","none"); //默认不显示下拉日历控件
_calendar.selectionchanged += new eventhandler(_calendar_selectionchanged);
this.controls.add(_calendar);
}
[
category("appearance"), //该属性所属类别,参见图
defaultvalue(""), //属性默认值
description("设置该日期控件的值。") //属性的描述
]
public string text
{
get
{
ensurechildcontrols();
return (viewstate["text"] == null)?system.datetime.today.tostring("yyyy-mm-dd"):viewstate["text"].tostring();
}
set
{
ensurechildcontrols();
datetime dt = system.datetime.today;
try
{
dt = datetime.parse(value);
}
catch
{
throw new argumentoutofrangeexception("请输入日期型字符串(例如:1981-04-29)!");
}
viewstate["text"] = dateformat == calendarenum.longdatetime?dt.tostring("yyyy-mm-dd"):dt.tostring("yyyy-m-d");
}
}
//重载服务器控件的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:o.tostring();
}
set
{
ensurechildcontrols();
viewstate["buttonsytle"] = value;
}
}
[
defaultvalue(calendarenum.longdatetime),
]
public calendarenum dateformat
{
get
{
ensurechildcontrols();
object format = viewstate["dateformat"];
return format == null?calendarenum.longdatetime:(calendarenum)format;
}
set
{
ensurechildcontrols();
viewstate["dateformat"] = value;
datetime dt = datetime.parse(text);
text=dateformat == calendarenum.longdatetime?dt.tostring("yyyy-mm-dd"):dt.tostring("yyyy-m-d");
}
}
[
browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)
]
public string mycalendarid //复合控件id
{
get
{
ensurechildcontrols();
return this.clientid+"_mycalendar";
}
}
[
browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)
]
public string mycalendarname //复合控件名称
{
get
{
ensurechildcontrols();
return this.uniqueid+":mycalendar";
}
}
[
browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)
]
public string datepickerinputid //复合控件中输入框的id
{
get
{
ensurechildcontrols();
return this.clientid+"_dateinput";
}
}
[
browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)
]
public string datepickerinputname //复合控件中输入框的名称
{
get
{
ensurechildcontrols();
return this.uniqueid+":dateinput";
}
}
[
browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)
]
public string datepickerbuttonid //复合控件中按钮的id
{
get
{
ensurechildcontrols();
return this.clientid+"_datebutton";
}
}
[
browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)
]
public string datepickerbuttonname //复合控件中按钮的名称
{
get
{
ensurechildcontrols();
return this.uniqueid+":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)
{
//在页面中输出控件时,产生一个表格(二行二列),以下是表格的样式
output.addattribute(htmltextwriterattribute.cellspacing, "0");
output.addattribute(htmltextwriterattribute.border, "0");
output.addattribute(htmltextwriterattribute.cellpadding, "0");
output.addstyleattribute("left", this.style["left"]);
output.addstyleattribute("top", this.style["top"]);
output.addstyleattribute("position", "absolute");
if (width != unit.empty)
{
output.addstyleattribute(htmltextwriterstyle.width, width.tostring());
}
else
{
output.addstyleattribute(htmltextwriterstyle.width, "200px");
}
output.renderbegintag(htmltextwritertag.table); //输出表格
output.renderbegintag(htmltextwritertag.tr); //表格第一行
output.addattribute(htmltextwriterattribute.width, "90%");
output.renderbegintag(htmltextwritertag.td);
//以下是第一行第一列中文本框的属性及其样式设置
if (!enabled)
{
output.addattribute(htmltextwriterattribute.readonly, "true");
}
output.addattribute(htmltextwriterattribute.type, "text");
output.addattribute(htmltextwriterattribute.id, datepickerinputid);
output.addattribute(htmltextwriterattribute.name, datepickerinputname);
output.addattribute(htmltextwriterattribute.value, text);
output.addstyleattribute(htmltextwriterstyle.width, "100%");
output.addstyleattribute(htmltextwriterstyle.height, "100%");
output.addstyleattribute(htmltextwriterstyle.fontfamily, font.name);
output.addstyleattribute(htmltextwriterstyle.fontsize, font.size.tostring());
output.addstyleattribute(htmltextwriterstyle.fontweight, font.bold?"bold":"");
output.addstyleattribute(htmltextwriterstyle.backgroundcolor, colortranslator.tohtml(backcolor));
output.addstyleattribute(htmltextwriterstyle.color, colortranslator.tohtml(forecolor));
output.renderbegintag(htmltextwritertag.input); //输出文本框
output.renderendtag();
output.renderendtag();
output.addattribute(htmltextwriterattribute.width, "*");
output.renderbegintag(htmltextwritertag.td);
//以下是第一行第二列中按钮的属性及其样式设置
if (!enabled)
{
output.addattribute(htmltextwriterattribute.disabled, "true");
}
output.addattribute(htmltextwriterattribute.type, "submit");
output.addattribute(htmltextwriterattribute.id, datepickerbuttonid);
output.addattribute(htmltextwriterattribute.name, datepickerbuttonname);
output.addattribute(htmltextwriterattribute.value, buttontext);
output.addstyleattribute(htmltextwriterstyle.width, "100%");
output.addattribute(htmltextwriterattribute.onclick, page.getpostbackeventreference(this)); //点击按钮时需要回传服务器来触发后面的onclick事件
output.addattribute(htmltextwriterattribute.style, buttonstyle);
output.renderbegintag(htmltextwritertag.input); //输出按钮
output.renderendtag();
output.renderendtag();
output.renderendtag();
output.renderbegintag(htmltextwritertag.tr);
output.addattribute(htmltextwriterattribute.colspan, "2");
output.renderbegintag(htmltextwritertag.td);
_calendar.rendercontrol(output); //将日历子控件输出
output.renderendtag();
output.renderendtag();
output.renderendtag();
}
//复合控件必须继承ipostbackeventhandler接口,才能继承raisepostbackevent事件
public void raisepostbackevent(string eventargument)
{
onclick(eventargs.empty);
}
protected virtual void onclick(eventargs e)
{
//点击选择日期按钮时,如果日历子控件没有显示则显示出来并将文本框的值赋值给日历子控件
if (_calendar.attributes["display"] != "")
{
_calendar.selecteddate = datetime.parse(text);
_calendar.style.add("display","");
}
}
//复合控件中的日历控件日期变化事件
private void _calendar_selectionchanged(object sender, eventargs e)
{
//当选择的日期变化时,将所选日期赋值给文本框并将日历子控件隐藏
text = _calendar.selecteddate.tostring();
_calendar.style.add("display","none");
}
}
}