⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datecontrol.cs

📁 ASP中web自定义控件的使用源码及说明文档
💻 CS
字号:
using System;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Resources;
using System.Reflection;
using System.IO;

namespace Bestcomy.Web.UI.WebControls
{
	/// <summary>
	/// 日历控件,获取或设置的日期值为 System.DateTime.MinValue 时,表示为空值。
	/// </summary>
	[DefaultProperty("Value"),ToolboxData("<{0}:DateControl runat=server></{0}:DateControl>")]
	public class DateControl : System.Web.UI.WebControls.TextBox, System.Web.UI.INamingContainer
	{
		private DateTime _value = DateTime.MinValue;

		/// <summary>
		/// 获取或设置日期值。
		/// </summary>
		[Category("外观"),Description("获取或设置日期值。")]
		public DateTime Value
		{
			get
			{	
				return GetPostBackValue();
			}
			set
			{
				_value = value;
				if(!value.Equals(DateTime.MinValue))
					base.Text = value.ToString("d");
				else
					base.Text = string.Empty;
			}
		}

		/// <summary>
		/// 返回当前值是否为NULL.
		/// </summary>
		public bool IsNull
		{
			get
			{
				return GetPostBackValue().Equals(DateTime.MinValue);
			}
		}

		/// <summary>
		/// 设置当前值为NULL.
		/// </summary>
		public void SetNull()
		{
			_value = DateTime.MinValue;
			base.Text = string.Empty;
		}

		/// <summary>
		/// 引发 Init 事件。
		/// </summary>
		/// <param name="e">包含事件数据的 EventArgs 对象。</param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			if(!base.Page.IsStartupScriptRegistered("Bestcomy_DateControlScript"))
			{
				ResourceManager resx = new ResourceManager(base.GetType());
				base.Page.RegisterStartupScript("Bestcomy_DateControlScript",resx.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture,true,true).GetString("DateScript"));
			}
			/*
			ResourceManager _resource = new ResourceManager(base.GetType());
			Assembly assembly = typeof(ComboBox).Assembly;
			Version version = assembly.GetName().Version;
			string scriptPath = base.MapPathSecure(@"\") + @"webctrl_client\Bestcomy\Calendar\"+version.Major.ToString()+"_"+version.Minor.ToString();
			if(!Directory.Exists(scriptPath))
			{
				Directory.CreateDirectory(scriptPath);
			}
			if(!File.Exists(Path.Combine(scriptPath,"setday.js")))
			{
				Stream s = assembly.GetManifestResourceStream(typeof(DateControl),"setday.js");
				byte[] bt = new Byte[s.Length];
				s.Read(bt,0,bt.Length);
				s.Close();

				FileStream fs = new FileStream(Path.Combine(scriptPath,"setday.js"),FileMode.Create,FileAccess.Write);
				fs.Write(bt,0,bt.Length);
				fs.Flush();
				fs.Close();
			}
			if(!base.Page.IsClientScriptBlockRegistered("Bestcomy_DateControlScript"))
			{
				base.Page.RegisterClientScriptBlock("Bestcomy_DateControlScript","<script language=\"javascript\" type=\"text/javascript\" src=\"/webctrl_client/Bestcomy/Calendar/" + version.Major.ToString()+"_"+version.Minor.ToString() + "/setday.js" + "\"></script>");
			}
			*/
		}
		
		/// <summary>
		/// 该成员重写 Control.OnPreRender。
		/// </summary>
		/// <param name="e">包含事件数据的 EventArgs 对象。</param>
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			base.Attributes.Add("autocomplete","off");
			if(!this.ReadOnly)
				base.Attributes.Add("onfocus","setday(this);");
		}

		private DateTime GetPostBackValue()
		{
			_value = DateTime.MinValue;
			if(base.Text.Trim()==string.Empty)
			{
				_value = DateTime.MinValue;
			}
			else
			{
				try
				{
					Convert.ToDateTime(base.Text.Trim());
				}
				catch
				{
					throw new FormatException();
				}
				_value = Convert.ToDateTime(base.Text.Trim());
			}
			return _value;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -