.net控件编程-事件

完成事件功能有两种方法:“冒泡”和“包含”

具体实现代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Web.UI;
  5using System.Web.UI.WebControls;
  6using System.ComponentModel;
  7using System.ComponentModel.Design;
  8
  9namespace OnClick
 10ExpandedBlockStart.gifContractedBlock.gif{
 11    public class OnClick : CompositeControl
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 13        private Button _button;
 14        private TextBox _textBox;
 15        //定义事件
 16         private static readonly object EventSubmitKey = new object();
 17        [
 18        Bindable(true), Category("Appearance"), DefaultValue(""), Description("")
 19        ]
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        public string ButtonText {
 21ExpandedSubBlockStart.gifContractedSubBlock.gif            get {
 22                EnsureChildControls();
 23                return _button.Text;
 24            }

 25ExpandedSubBlockStart.gifContractedSubBlock.gif            set {
 26                EnsureChildControls();
 27                _button.Text = value;
 28            }

 29        }

 30        [
 31        Bindable(true), Category("Appearance"), DefaultValue(""), Description("")
 32        ]
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Text {
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            get {
 35                EnsureChildControls();
 36                return _textBox.Text;
 37            }

 38ExpandedSubBlockStart.gifContractedSubBlock.gif            set {
 39                EnsureChildControls();
 40                _textBox.Text = value;
 41            }

 42        }

 43        //定义事件属性
 44        public event EventHandler Submit
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            add {
 47                Events.AddHandler(EventSubmitKey, value);
 48            }

 49ExpandedSubBlockStart.gifContractedSubBlock.gif            remove {
 50                Events.AddHandler(EventSubmitKey, value);
 51            }

 52        }

 53        //定义事件接受方
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        protected virtual void OnSubmit(EventArgs e) {
 55            EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
 56            if (SubmitHandler != null)
 57                SubmitHandler(this, e);
 58        }

 59ExpandedSubBlockStart.gifContractedSubBlock.gif        /**///// 实现Submit事件引发的事件处理程序 
 60        //private void _button_Click(Object source, EventArgs e)
 61        //{
 62        //    OnSubmit(EventArgs.Empty);
 63        //}
 64        // 重写OnBubbleEvent方法,执行事件冒泡,响应用户操作发起事件
 65        protected override bool OnBubbleEvent(object source, EventArgs e)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 67            bool handled = false;
 68            if (e is CommandEventArgs)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 70                CommandEventArgs ce = (CommandEventArgs)e;
 71                if (ce.CommandName == "Submit")
 72ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 73                    OnSubmit(EventArgs.Empty);
 74                    handled = true;
 75                }

 76            }

 77            return handled;
 78        }

 79      // 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
 80        protected override void RecreateChildControls()
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 82            EnsureChildControls();
 83        }

 84
 85      //重写CreateChildControls方法,将子控件添加到复合控件中 
 86
 87        protected override void CreateChildControls()
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 89            Controls.Clear();
 90            _button = new Button();
 91            _textBox = new TextBox();
 92            _button.ID = "btn";
 93            //_button.Click += new EventHandler(_button_Click);
 94            _button.CommandName = "Submit";
 95            this.Controls.Add(_button);
 96            this.Controls.Add(_textBox);
 97        }

 98
 99      //重写Render方法,呈现控件中其他的HTML代码 
100
101        protected override void Render(HtmlTextWriter output)
102ExpandedSubBlockStart.gifContractedSubBlock.gif        {
103            output.AddAttribute(HtmlTextWriterAttribute.Border, "0px");
104            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "5px");
105            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0px");
106            output.RenderBeginTag(HtmlTextWriterTag.Table);
107                output.RenderBeginTag(HtmlTextWriterTag.Tr);
108                    output.RenderBeginTag(HtmlTextWriterTag.Td);
109                    _textBox.RenderControl(output);
110                    output.RenderEndTag();
111                    output.RenderBeginTag(HtmlTextWriterTag.Td);
112                    _button.RenderControl(output);
113                    output.RenderEndTag();
114                output.RenderEndTag();
115            output.RenderEndTag();
116        }

117    }

118}

119

转载于:https://www.cnblogs.com/zhou5791759/archive/2009/02/18/1392966.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值