给自定义服务器控件或用户控件增加事件处理

/**
 *
 * Description: 为用户提供可视的时间选择控件。
 *
 * Copyright:Copyright (c) 2006
 *
 * Company: SINORY.COM
 *
 * Author:Sinory J. Klaus
 *
 * Version:1.0
 *
 **/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Sinory.Web.UI.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomDateTime runat=server></{0}:CustomDateTime>")]
    public class CustomDateTime : WebControl
    {
        //定义包含的子控件
        public TextBox SelDate=new TextBox();
        public Calendar calendar=new Calendar();
        public Button btnSelect = new Button();

        #region 控件基本属性定义
       
        //定义控件的字体颜色属性
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Color  ForeColor
        {
            get
            {
                object color = ViewState["ForeColor"];
                return (color == null) ? Color.Navy : (Color)color;
            }
            set
            {
             base.ForeColor = value;
            }
        }
        //定义控件的背景色
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Color  BackColor
        {
            get
            {
                object color = ViewState["BackColor"];
                return (color == null) ? ColorTranslator.FromHtml("#C0C0FF") : (Color)color;
            }
            set
            {
             base.BackColor = value;
            }
        }
      
        //定义控件的边框样式
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override BorderStyle BorderStyle
        {
            get
            {
                object style = ViewState["BorderStyle"];
                return (style == null) ? BorderStyle.Inset : (BorderStyle)style;
            }
            set
            {
                base.BorderStyle = value;
            }
        }
        //控件的边框颜色
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Color  BorderColor
        {
            get
            {
                object color = ViewState["BorderColor"];
                return (color == null) ? ColorTranslator.FromHtml("#8080FF") : (Color)color;
            }
            set
            {
             base.BorderColor = value;
            }
        }

        //定义控件宽度
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit  Width
        {
              get
            {
              return base.Width;
            }
              set
            {
             base.Width = value;
            }
        }
        //定义控件高度
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit  Height
        {
              get
            {
              return base.Height;
            }
              set
            {
             base.Height = value;
            }
        }
        //定义控件的值属性
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Value
        {
            get
            {
                object dt=ViewState["Value"];
                return (dt==null) ? DateTime.Now.Date.ToShortDateString():(string)dt;
            }
            set
            {
                ViewState["Value"]=value;
                this.SelDate.Text = value;
            }
        }
        // 定义控件的星期显示格式
        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DayNameFormat dayNameFormat
        {
            get
            {
                object format = ViewState["dayNameFormat"];
                return (format == null) ? DayNameFormat.Shortest : (DayNameFormat)format;
            }
            set
            {
                ViewState["dayNameFormat"] = value;
            }
        }
        ………………       

        #endregion

        //控件初始化
        public CustomDateTime()
        {
            //将控件的事件句柄绑定到相应函数
            calendar.SelectionChanged += new System.EventHandler(this.calendar_SelectionChanged);
            btnSelect.Click += new System.EventHandler(this.SelDate_Click);
           
            //this.SelDate
            #region //设置控件calendar属性
            this.calendar.BackColor = this.BackColor;
            this.calendar.BorderColor = this.BorderColor;
            this.calendar.BorderStyle = this.BorderStyle;
            this.calendar.DayNameFormat = this.dayNameFormat;
            this.calendar.ForeColor = this.ForeColor;
            this.calendar.Font.Size= this.fontSize;
            this.calendar.TodayDayStyle.BackColor = this.TDSBackColor;
            this.calendar.TodayDayStyle.ForeColor = this.TDSForeColor;
            this.calendar.SelectorStyle.BackColor = this.SSBackColor;
            this.calendar.DayStyle.Font.Size = this.DSFontSize;
            this.calendar.DayStyle.ForeColor = this.DSForeColor;
            this.calendar.DayStyle.BorderColor = this.DSBorderColor;
            this.calendar.DayStyle.BorderStyle = this.DSBorderStyle;
            this.calendar.NextPrevStyle.BackColor = this.NPSBackColor;
            this.calendar.NextPrevStyle.BorderColor = this.NPSBorderColor;
            this.calendar.NextPrevStyle.BorderStyle = BorderStyle.Outset;
            this.calendar.NextPrevStyle.ForeColor = Color.Navy;
            this.calendar.NextPrevStyle.BorderWidth = base.BorderWidth;
            this.calendar.DayHeaderStyle.BackColor = ColorTranslator.FromHtml("#C0C0FF");
            this.calendar.DayHeaderStyle.ForeColor = ColorTranslator.FromHtml("ControlText");
            this.calendar.DayHeaderStyle.BorderColor = Color.Lavender;
            this.calendar.TitleStyle.BackColor = ColorTranslator.FromHtml("#C0C0FF");
            this.calendar.TitleStyle.ForeColor = Color.Navy;
            this.calendar.BorderStyle = BorderStyle.Inset;
            //this.calendar.BorderWidth = base.BorderWidth;
            this.calendar.SelectedDayStyle.BackColor =Color.MediumAquamarine;
            this.calendar.SelectedDayStyle.ForeColor = ColorTranslator.FromHtml("#C00000");
            this.calendar.WeekendDayStyle.ForeColor = Color.Red;
            this.calendar.OtherMonthDayStyle.ForeColor = Color.MidnightBlue;

            btnSelect.Text = "选择日期";
            btnSelect.Enabled = true;
            btnSelect.Visible = true;
            #endregion
            //将子控件属性与自定控件属性相关
            SelDate.Width = base.Width;
           
            calendar.Visible = false;
            calendar.TodayDayStyle.BackColor = Color.AliceBlue;
            calendar.Height = base.Height;
            calendar.Width = SelDate.Width;

            this.SelDate.ReadOnly = true;
            this.SelDate.Text = this.Value;
            this.Controls.Add(this.SelDate);
            this.Controls.Add(btnSelect);
            this.Controls.Add(this.calendar);
        }

        protected override void  Render(HtmlTextWriter writer)
        {
            this.SelDate.RenderControl(writer);
            this.btnSelect.RenderControl(writer);
            this.calendar.RenderControl(writer);

          //base.Render(writer);
        }
        protected override void RenderContents(HtmlTextWriter output)
        {
           // output.Write(Text);
        }
        //为子控件添加事件
        private void SelDate_Click(Object sender, EventArgs e)
        {
            if(this.calendar.Visible==false)
                this.calendar.Visible = true;
            this.btnSelect.Enabled = false;
        }

        protected void calendar_SelectionChanged(object sender, EventArgs e)
        {
            this.Value = calendar.SelectedDate.ToShortDateString();
            this.SelDate.Text = this.Value;
            this.btnSelect.Enabled = true;
            this.calendar.Visible = false;
        }
    }
}
注意:本代码并不完整,为节省篇幅其中子控件的属性定义并未给出。不过很容易自己加入。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值