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

📄 calendaruc.ascx.cs

📁 办公室系统包括上下班打卡
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 日期格式的枚举值
/// </summary>
public enum DateFormatStyle
{
	Default,
	ShortDate,
	LongDate,
	ShortTime,
	LongTime	
}

public partial class UserControls_CalendarUC:System.Web.UI.UserControl
{   ///日期格式的枚举属性
	private DateFormatStyle dateFormat = DateFormatStyle.Default;
	public DateFormatStyle DateFormat
	{
		get
		{
			return dateFormat;
		}
		set
		{
			dateFormat = value;
		}
	}

	/// <summary>
	/// 控件是否已经选择了值
	/// </summary>
	public bool HaveValue
	{
		get
		{
			return (tbDate.Text.Length > 0 ? true : false);
		}
	}

	/// <summary>
	/// 控件选择的日期
	/// </summary>
	public DateTime SelectedDate
	{
		get
		{
			return DateTime.Parse(tbDate.Text);
		}
		set
		{
			switch(dateFormat)
			{
				case DateFormatStyle.LongDate:
					tbDate.Text = value.ToLongDateString();
					break;
				case DateFormatStyle.LongTime:
					tbDate.Text = value.ToLongTimeString();
					break;
				case DateFormatStyle.ShortDate:
					tbDate.Text = value.ToShortDateString();
					break;
				case DateFormatStyle.ShortTime:
					tbDate.Text = value.ToShortTimeString();
					break;
				default:
					tbDate.Text = value.ToString();
					break;
			}		
		}
	}

	/// <summary>
	/// 选择日期时触发的事件
	/// </summary>
	private event EventHandler clickEvent;
	public event EventHandler ClickEvent
	{
		add
		{
			clickEvent += value;
		}
		remove
		{
			clickEvent -= value;
		}
	}

	protected void Page_Init(object sender,EventArgs e)
	{   ///控件初始化
		cDate.SelectedDate = DateTime.Now;
		btnSelectDate.Text = "▼";
	}

	protected void Page_Load(object sender,EventArgs e)
	{
		///
	}
	
	protected void cDate_SelectionChanged(object sender,EventArgs e)
	{   ///选择一个日期
		switch(dateFormat)
		{
			case DateFormatStyle.LongDate:
				tbDate.Text = cDate.SelectedDate.ToLongDateString();
				break;
			case DateFormatStyle.LongTime:
				tbDate.Text = cDate.SelectedDate.ToLongTimeString();
				break;
			case DateFormatStyle.ShortDate:
				tbDate.Text = cDate.SelectedDate.ToShortDateString();
				break;
			case DateFormatStyle.ShortTime:
				tbDate.Text = cDate.SelectedDate.ToShortTimeString();
				break;
			default:
				tbDate.Text = cDate.SelectedDate.ToString();
				break;
		}		
		cDate.Visible = !cDate.Visible;
		SetButtonText();
	}
	protected void btnSelectDate_Click(object sender,EventArgs e)
	{   ///设置日期控件的可见性
		cDate.Visible = !cDate.Visible;
		if(HaveValue == true)
		{   ///设置可见的日期
			cDate.VisibleDate = SelectedDate;
		}
		///设置按钮的可用性
		SetButtonText();
		if(clickEvent != null)
		{   ///触发事件
			clickEvent(this,EventArgs.Empty);
		}
	}

	private void SetButtonText()
	{   ///设置按钮的图标
		if(cDate.Visible == true)
		{
			btnSelectDate.Text = "▲";
		}
		else
		{
			btnSelectDate.Text = "▼";
		}
	}
}

⌨️ 快捷键说明

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