using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
namespace ClassLibrary1
{
public class ChangeColorDateControl : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer, System.Web.UI.IPostBackEventHandler
{
private static readonly object ClickKey = new object();
[Description("单击日期触发的时间!")]
public event EventHandler Click
{
add
{
Events.AddHandler(ClickKey, value);
}
remove
{
Events.RemoveHandler(ClickKey, value);
}
}
private static readonly string[] header = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
//private DateTime showDate;
/// <summary>
/// Gets or sets the show date.
/// </summary>
/// <value>The show date.</value>
[Description("格式必须为yyyy-MM一样")]
public string ShowDate
{
get
{
if (ViewState["showdate"] != null)
{
return ViewState["showdate"].ToString();
}
else
{
DateTime showDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
ViewState["showdate"] = showDate.ToString("yyyy-MM");
return ViewState["showdate"].ToString();
}
}
set
{
DateTime showDate;
if (value == null)
{
showDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
}
else
{
showDate = Convert.ToDateTime(value + "-01");
}
ViewState["showdate"] = showDate.ToString("yyyy-MM");
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
//为table标签定义属性和样式
// writer.AddAttribute(HtmlTextWriterAttribute.Width, "287px");
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
writer.AddAttribute(HtmlTextWriterAttribute.Border, "1px");
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
base.AddAttributesToRender(writer);
}
/// <summary>
/// 获取与此 Web 服务器控件相对应的 <see cref="T:System.Web.UI.HtmlTextWriterTag"></see> 值。此属性主要由控件开发人员使用。
/// </summary>
/// <value></value>
/// <returns><see cref="T:System.Web.UI.HtmlTextWriterTag"></see> 枚举值之一。</returns>
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Table;
}
}
/// <summary>
/// 将控件的内容呈现到指定的编写器中。此方法主要由控件开发人员使用。
/// </summary>
/// <param name="writer"><see cref="T:System.Web.UI.HtmlTextWriter"></see>,表示要在客户端呈现 HTML 内容的输出流。</param>
protected override void RenderContents(HtmlTextWriter writer)
{
DateTime showDate = Convert.ToDateTime(this.ShowDate + "-01");
writer.RenderBeginTag(HtmlTextWriterTag.Thead);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "7");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(showDate.Year + "年" + showDate.Month + "月");
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < ChangeColorDateControl.header.Length; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(ChangeColorDateControl.header[i]);
writer.RenderEndTag();
}
writer.RenderEndTag();
DateTime end = showDate.AddMonths(1);
int days = ((TimeSpan)end.Subtract(showDate)).Days;
int spaceday = Convert.ToInt32(showDate.DayOfWeek);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < spaceday; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.WriteLine(" ");
writer.RenderEndTag();
}
for (int i = 1; i <= days; i++)
{
writer.Write("<td onclick=\"if(this.style.backgroundColor!='red'){this.style.backgroundColor='red';}else{this.style.backgroundColor='blue';}");
//关键在于style.backgroundColor, 非background-color
//writer.Write(Page.GetPostBackEventReference(this, this.ShowDate + "-" + i.ToString().PadLeft(2, '0')));
writer.Write("\">" + i.ToString() + "</td>");
//writer.RenderEndTag();
if ((i + spaceday) % 7 == 0)
{
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
}
if ((spaceday + days) % 7 != 0)
{
int allcount = spaceday + days > 35 ? 42 : 35;
for (int i = 0; i < allcount - spaceday - days; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag();
}
writer.RenderEndTag();
}
}
protected virtual void OnClick(StringEventArgs e)
{
EventHandler clickEventDelegate =
(EventHandler)Events[ClickKey];
if (clickEventDelegate != null)
{
clickEventDelegate(this, e);
}
}
IPostBackEventHandler 成员
}
}
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
namespace ClassLibrary1
{
public class ChangeColorDateControl : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer, System.Web.UI.IPostBackEventHandler
{
private static readonly object ClickKey = new object();
[Description("单击日期触发的时间!")]
public event EventHandler Click
{
add
{
Events.AddHandler(ClickKey, value);
}
remove
{
Events.RemoveHandler(ClickKey, value);
}
}
private static readonly string[] header = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
//private DateTime showDate;
/// <summary>
/// Gets or sets the show date.
/// </summary>
/// <value>The show date.</value>
[Description("格式必须为yyyy-MM一样")]
public string ShowDate
{
get
{
if (ViewState["showdate"] != null)
{
return ViewState["showdate"].ToString();
}
else
{
DateTime showDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
ViewState["showdate"] = showDate.ToString("yyyy-MM");
return ViewState["showdate"].ToString();
}
}
set
{
DateTime showDate;
if (value == null)
{
showDate = Convert.ToDateTime(System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-01");
}
else
{
showDate = Convert.ToDateTime(value + "-01");
}
ViewState["showdate"] = showDate.ToString("yyyy-MM");
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
//为table标签定义属性和样式
// writer.AddAttribute(HtmlTextWriterAttribute.Width, "287px");
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
writer.AddAttribute(HtmlTextWriterAttribute.Border, "1px");
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
base.AddAttributesToRender(writer);
}
/// <summary>
/// 获取与此 Web 服务器控件相对应的 <see cref="T:System.Web.UI.HtmlTextWriterTag"></see> 值。此属性主要由控件开发人员使用。
/// </summary>
/// <value></value>
/// <returns><see cref="T:System.Web.UI.HtmlTextWriterTag"></see> 枚举值之一。</returns>
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Table;
}
}
/// <summary>
/// 将控件的内容呈现到指定的编写器中。此方法主要由控件开发人员使用。
/// </summary>
/// <param name="writer"><see cref="T:System.Web.UI.HtmlTextWriter"></see>,表示要在客户端呈现 HTML 内容的输出流。</param>
protected override void RenderContents(HtmlTextWriter writer)
{
DateTime showDate = Convert.ToDateTime(this.ShowDate + "-01");
writer.RenderBeginTag(HtmlTextWriterTag.Thead);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "7");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(showDate.Year + "年" + showDate.Month + "月");
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < ChangeColorDateControl.header.Length; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(ChangeColorDateControl.header[i]);
writer.RenderEndTag();
}
writer.RenderEndTag();
DateTime end = showDate.AddMonths(1);
int days = ((TimeSpan)end.Subtract(showDate)).Days;
int spaceday = Convert.ToInt32(showDate.DayOfWeek);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < spaceday; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.WriteLine(" ");
writer.RenderEndTag();
}
for (int i = 1; i <= days; i++)
{
writer.Write("<td onclick=\"if(this.style.backgroundColor!='red'){this.style.backgroundColor='red';}else{this.style.backgroundColor='blue';}");
//关键在于style.backgroundColor, 非background-color
//writer.Write(Page.GetPostBackEventReference(this, this.ShowDate + "-" + i.ToString().PadLeft(2, '0')));
writer.Write("\">" + i.ToString() + "</td>");
//writer.RenderEndTag();
if ((i + spaceday) % 7 == 0)
{
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
}
if ((spaceday + days) % 7 != 0)
{
int allcount = spaceday + days > 35 ? 42 : 35;
for (int i = 0; i < allcount - spaceday - days; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag();
}
writer.RenderEndTag();
}
}
protected virtual void OnClick(StringEventArgs e)
{
EventHandler clickEventDelegate =
(EventHandler)Events[ClickKey];
if (clickEventDelegate != null)
{
clickEventDelegate(this, e);
}
}
IPostBackEventHandler 成员
}
}