using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
namespace MyQuery.MyControl
{
/// <summary>
/// MyDate 选择输入日期控件
/// </summary>
[DefaultProperty("Text"),
ParseChildren(false),
PersistChildren(false),
Description("专用于ASP.Net Web应用程序的日期控件"),
Designer(typeof(MyDateDesigner)),
ToolboxData("<{0}:MyDate runat=server></{0}:MyDate>")]
public class MyDate : Panel, INamingContainer, IPostBackDataHandler
{
#region 属性
/// <summary>
/// 获取/设置日期值。
/// </summary>
[Bindable(true),
Browsable(true),
Description("日期值(格式:YYYY-MM-DD hh:mm或YYYY年MM月DD日hh时ss分)默认为当前日期"),
Category("外观"),
DefaultValue("")]
public string Text
{
get
{
if (ViewState["Text"] != null)
{
return ViewState["Text"].ToString();
}
else
{
if (IsNull)
{
return "";
}
else
{
DateTime date = System.DateTime.Today;
if (IsDate)
{
return date.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
else
{
return date.ToString("yyyy-MM", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
}
}
}
set
{
if (IsDate)
{
DateTime date;
if (!IsNull && String.IsNullOrEmpty(value))
{
value = System.DateTime.Today.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
if (value.IndexOf("年") > 0)
{
value = value.Replace("年", "-").Replace("月", "-").Replace("日", " ").Replace("时", ":").Replace("分", ":");
}
if (String.IsNullOrEmpty(value) || value.IndexOf("1900") == 0)
{
ViewState["Text"] = "";
}
else
{
try
{
date = Convert.ToDateTime(value);
}
catch
{
date = System.DateTime.Today;
}
if (IsHour)
{
ViewState["Text"] = date.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
else
{
ViewState["Text"] = date.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
}
}
else
{
ViewState["Text"] = value;
}
}
}
/// <summary>
/// 获取日期值。
/// </summary>
[Browsable(true),
Description("日期值(日期型)"),
Category("外观"),
DefaultValue(0)]
public DateTime Date
{
get
{
if (ViewState["Text"] != null)
{
return Convert.ToDateTime(ViewState["Text"].ToString());
}
else
{
return System.DateTime.Today;
}
}
}
/// <summary>
/// 获取/设置日期值是否允许空。
/// </summary>
[Browsable(true),
Description("日期值是否允许空"),
Category("布局"),
DefaultValue(false)]
public bool IsNull
{
get
{
object obj = ViewState["IsNull"];
return (obj == null) ? false : (bool)obj;
}
set
{
if (value)
ViewState["IsNull"] = true;
}
}
/// <summary>
/// 获取/设置日期值是否为完整的年-月-日,否为年-月。
/// </summary>
[Browsable(true),
Description("日期值是否为完整的年-月-日,否为年-月"),
Category("布局"),
DefaultValue(true)]
public bool IsDate
{
get
{
object obj = ViewState["IsDate"];
return (obj == null) ? true : (bool)obj;
}
set
{
if (!value)
ViewState["IsDate"] = false;
}
}
/// <summary>
/// 获取/设置日期值是否包含时间。
/// </summary>
[Browsable(true),
Description("日期值中是否包含时间"),
Category("布局"),
DefaultValue(false)]
public bool IsHour
{
get
{
object obj = ViewState["IsHour"];
return (obj == null) ? false : (bool)obj;
}
set
{
if (value)
{
ViewState["IsHour"] = true;
ViewState["IsDate"] = true;
}
}
}
/// <summary>
/// 获取/设置日期值是否包含时间。
/// </summary>
[Browsable(true),
Description("以表格呈现"),
Category("布局"),
DefaultValue(false)]
public bool IsTable
{
get
{
object obj = ViewState["IsTable"];
return (obj == null) ? false : (bool)obj;
}
set
{
if (value)
{
ViewState["IsTable"] = true;
}
}
}
/// <summary>
/// 获取/设置日期值能否编辑。
/// </summary>
[Browsable(true),
Description("能否编辑"),
Category("行为"),
DefaultValue(true)]
public override bool Enabled
{
get
{
object obj = ViewState["Enabled"];
return (obj == null) ? true : (bool)obj;
}
set
{
if (!value)
ViewState["Enabled"] = false;
}
}
/// <summary>
/// 获取/设置日期值中可供选择的年份长度。
/// </summary>
[Browsable(true),
Description("日期值中可供选择的年份长度"),
Category("布局"),
DefaultValue(100)]
public int Length
{
get
{
object obj = ViewState["Length"];
return (obj == null) ? 100 : (int)obj;
}
set
{
ViewState["Length"] = value;
}
}
/// <summary>
/// 获取/设置选择年份的结束值。
/// </summary>
[Browsable(true),
Description("日期值中选择结束年份,当小于100时表示距今年数"),
Category("布局"),
DefaultValue(0)]
public int End
{
get
{
object obj = ViewState["End"];
int y;
if (obj == null)
{
y = System.DateTime.Today.Year;
}
else
{
y = (int)obj;
if (y < 100)
{
y = System.DateTime.Today.Year + y;
}
}
return y;
}
set
{
ViewState["End"] = value;
}
}
/// <summary>
/// 获取选择年份的开始值。
/// </summary>
[Browsable(true),
Description("日期值中选择开始年份"),
Category("布局"),
DefaultValue(2000)]
public int Start
{
get { return End - Length; }
set
{
int s = value;
int y = System.DateTime.Today.Year;
ViewState["Length"] = y - s;
ViewState["End"] = y;
}
}
#endregion
#region 重写事件
/// <summary>
/// 重写OnLoad 方法。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="EventArgs"/> 对象。</param>
protected override void OnLoad(EventArgs e)
{
if (Page.IsPostBack)
{
string y = Page.Request.Form[this.UniqueID + "_year"];
if (y != null)
{
string m = Page.Request.Form[this.UniqueID + "_month"];
string d = Page.Request.Form[this.UniqueID + "_day"];
string h = Page.Request.Form[this.UniqueID + "_hour"];
string mi = Page.Request.Form[this.UniqueID + "_minute"];
if (y == "" || m == "" || d == "")
{
Text = "";
}
else
{
string str = y + "-" + m;
if (d != null)
{
str += "-" + d;
}
if (!String.IsNullOrEmpty(h))
{
if (!String.IsNullOrEmpty(mi))
{
str += " " + h + ":" + mi + ":0";
}
else
{
str += " " + h + ":00:0";
}
}
Text = str;
}
}
}
base.OnLoad(e);
}
/// <summary>
/// 重写<see cref="System.Web.UI.WebControls.WebControl.AddAttributesToRender"/> 方法,验证是否有form(runat=server)控件
/// </summary>
/// <param name="writer"></param>
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
if (this.Page != null)
this.Page.VerifyRenderingInServerForm(this);
base.AddAttributesToRender(writer);
}
/// <summary>
/// 重写<see cref="System.Web.UI.Control.OnPreRender"/>方法。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="EventArgs"/> 对象。</param>
protected override void OnPreRender(EventArgs e)
{
string strJS = @"<script language=javascript>
//原创:贾世义 日期:2005-08-16 免费共享 v1.0
//邮箱:[email protected]
//请尊重版权,可以随意使用,但请注明出处//
function InitYear(pName,start,length,y)
{
var selYear=eval('document.forms[0].'+pName+'_year');
var n=selYear.length;
selYear.length=n+length+1;
for (i=0;i<=length;i++)
{
selYear.options[n+i].value=(i+start);
selYear.options[n+i].text=(i+start);
}
if (y==0)
{
selYear.selectedIndex =0;
}
else if ((y-start)<=length)
{
selYear.selectedIndex = n+Math.abs(y-start);
}
else
{
selYear.selectedIndex = length;
}
}
function InitMonth(pName,m)
{
var selMonth=document.getElementById(pName+'_month');
var n=selMonth.length;
selMonth.length=n+12;
for (i=1;i<10;i++)
{
selMonth.options[n+i-1].value='0'+i;
selMonth.options[n+i-1].text=i;
}
for (i=10;i<=12;i++)
{
selMonth.options[n+i-1].value=i;
selMonth.options[n+i-1].text=i;
}
if (m==0)
{
selMonth.selectedIndex=0;
}
else
{
selMonth.selectedIndex = n+m-1;
}
}
function InitDay(pName,d)
{
var selDay=document.getElementById(pName+'_day');
var n=selDay.length;
selDay.length=n+31;
for (i=1;i<10;i++)
{
selDay.options[n+i-1].value='0'+i;
selDay.options[n+i-1].text=i;
}
for (i=10;i<=31;i++)
{
selDay.options[n+i-1].value=i;
selDay.options[n+i-1].text=i;
}
if (d==0)
{
selDay.selectedIndex = 0;
}
else
{
selDay.selectedIndex = n+d-1;
}
dateChange(pName);
}
function InitHour(pName,h)
{
var selHour=document.getElementById(pName+'_hour');
selHour.length=24;
for (i=0;i<10;i++)
{
selHour.options[i].value='0'+i;
selHour.options[i].text=i;
}
for (i=10;i<24;i++)
{
selHour.options[i].value=i;
selHour.options[i].text=i;
}
selHour.selectedIndex = h;
}
function InitMinute(pName,m)
{
var selMinute=document.getElementById(pName+'_minute');
selMinute.length=60;
for (i=0;i<10;i++)
{
selMinute.options[i].value='0'+i;
selMinute.options[i].text=i;
}
for (i=10;i<60;i++)
{
selMinute.options[i].value=i;
selMinute.options[i].text=i;
}
selMinute.selectedIndex = m;
}
function dateChange(pName)
{
var selYear=eval('document.forms[0].'+pName+'_year');
var year=selYear.options[selYear.selectedIndex].value;
if (year!='')
{
var selMonth=document.getElementById(pName+'_month');
var month=selMonth.options[selMonth.selectedIndex].value;
if (month!='')
{
var date=new Date(year,month,0);
var day=date.getDate();
var selDay=document.getElementById(pName+'_day');
var tmp=1;
if (selDay.selectedIndex>0)
{
tmp=selDay.options[selDay.selectedIndex].value;
}
selDay.length=day;
for (i=1;i<10;i++)
{
selDay.options[i-1].value='0'+i;
selDay.options[i-1].text=i;
}
for (i=10;i<=day;i++)
{
selDay.options[i-1].value=i;
selDay.options[i-1].text=i;
}
if (tmp>day)
{
selDay.selectedIndex=day-1;
}
else
{
selDay.selectedIndex=tmp-1;
}
}
}
}
</script>";
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "EnableDays", strJS);
base.OnPreRender(e);
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="writer"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
if (IsTable)
{
writer.Write("<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap align=left>");
}
int y = 0;
int m = 0;
int d = 0;
int h = 0;
int mi = 0;
string str = Text;
if (str.Length >= 7)
{
y = Convert.ToInt32(str.Substring(0, 4));
m = Convert.ToInt32(str.Substring(5, 2));
if (str.Length >= 10)
{
d = Convert.ToInt32(str.Substring(8, 2));
}
if (str.Length >= 13)
{
h = Convert.ToInt32(str.Substring(11, 2));
}
if (str.Length >= 16)
{
mi = Convert.ToInt32(str.Substring(14, 2));
}
}
bool isDate = IsDate;
if (Enabled)
{
bool isNull = IsNull;
writer.Write("<select name='" + this.UniqueID + "_year'");
if (isDate)
{
writer.Write(" onchange=dateChange('" + this.UniqueID + "')");
}
writer.Write(">");
if (isNull)
{
writer.Write("<option value=''></option>");
}
writer.Write("</select>年");
writer.Write("<select name='" + this.UniqueID + "_month'");
if (isDate)
{
writer.Write(" onchange=dateChange('" + this.UniqueID + "')");
}
writer.Write(">");
if (isNull)
{
writer.Write("<option value=''></option>");
}
writer.Write("</select>月");
writer.Write(@"
<script language=javascript>
InitYear('" + this.UniqueID + "'," + Start.ToString() + "," + Length.ToString() + "," + y.ToString() + @");
InitMonth('" + this.UniqueID + "'," + m.ToString() + @");
</script>");
if (isDate)
{
writer.Write("<select name='" + this.UniqueID + "_day'>");
if (isNull)
{
writer.Write("<option value=''></option>");
}
writer.Write("</select>日");
writer.Write(@"
<script language=javascript>
InitDay('" + this.UniqueID + "'," + d.ToString() + @");
</script>");
if (IsHour)
{
writer.Write("<select name='" + this.UniqueID + "_hour'></select>时");
writer.Write(@"
<script language=javascript>
InitHour('" + this.UniqueID + "'," + h.ToString() + @");
</script>");
writer.Write("<select name='" + this.UniqueID + "_minute'></select>分");
writer.Write(@"
<script language=javascript>
InitMinute('" + this.UniqueID + "'," + mi.ToString() + @");
</script>");
}
}
}
else
{
if (y == 0 || m == 0)
{
writer.Write(" ");
}
else
{
writer.Write(y.ToString() + "年" + m.ToString() + "月");
if (d != 0)
{
writer.Write(d.ToString() + "日");
if (h != 0)
{
writer.Write(h.ToString() + "时");
writer.Write(mi.ToString() + "分");
}
}
}
}
if (IsTable)
{
writer.Write("</td></tr></table>");
}
}
/// <summary>
/// 数据变更触发事件
/// </summary>
public event EventHandler TextChanged;
/// <summary>
/// 当由类实现时,为 ASP.NET 服务器控件处理回发数据。
/// </summary>
/// <param name="postDataKey">控件的主要标识符</param>
/// <param name="postCollection">所有传入名称值的集合</param>
/// <returns>如果服务器控件的状态在回发发生后更改,则为 true;否则为 false。</returns>
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
String presentValue = Text;
String postedValue = postCollection[postDataKey];
if (presentValue == null || !presentValue.Equals(postedValue))
{
Text = postedValue;
return true;
}
return false;
}
/// <summary>
/// 当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
/// </summary>
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
/// <summary>
/// 数据变更函数
/// </summary>
/// <param name="e">事件参数</param>
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
TextChanged(this, e);
}
#endregion
}
}
欢迎访问:http://121.18.78.216 适易查询分析、工作流、内容管理及项目管理演示平台