.net控件编程-事件
完成事件功能有两种方法:“冒泡”和“包含”
具体实现代码如下:
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
10{
11 public class OnClick : CompositeControl
12 {
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 ]
20 public string ButtonText {
21 get {
22 EnsureChildControls();
23 return _button.Text;
24 }
25 set {
26 EnsureChildControls();
27 _button.Text = value;
28 }
29 }
30 [
31 Bindable(true), Category("Appearance"), DefaultValue(""), Description("")
32 ]
33 public string Text {
34 get {
35 EnsureChildControls();
36 return _textBox.Text;
37 }
38 set {
39 EnsureChildControls();
40 _textBox.Text = value;
41 }
42 }
43 //定义事件属性
44 public event EventHandler Submit
45 {
46 add {
47 Events.AddHandler(EventSubmitKey, value);
48 }
49 remove {
50 Events.AddHandler(EventSubmitKey, value);
51 }
52 }
53 //定义事件接受方
54 protected virtual void OnSubmit(EventArgs e) {
55 EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
56 if (SubmitHandler != null)
57 SubmitHandler(this, e);
58 }
59 /**///// 实现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)
66 {
67 bool handled = false;
68 if (e is CommandEventArgs)
69 {
70 CommandEventArgs ce = (CommandEventArgs)e;
71 if (ce.CommandName == "Submit")
72 {
73 OnSubmit(EventArgs.Empty);
74 handled = true;
75 }
76 }
77 return handled;
78 }
79 // 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
80 protected override void RecreateChildControls()
81 {
82 EnsureChildControls();
83 }
84
85 //重写CreateChildControls方法,将子控件添加到复合控件中
86
87 protected override void CreateChildControls()
88 {
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)
102 {
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
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
10{
11 public class OnClick : CompositeControl
12 {
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 ]
20 public string ButtonText {
21 get {
22 EnsureChildControls();
23 return _button.Text;
24 }
25 set {
26 EnsureChildControls();
27 _button.Text = value;
28 }
29 }
30 [
31 Bindable(true), Category("Appearance"), DefaultValue(""), Description("")
32 ]
33 public string Text {
34 get {
35 EnsureChildControls();
36 return _textBox.Text;
37 }
38 set {
39 EnsureChildControls();
40 _textBox.Text = value;
41 }
42 }
43 //定义事件属性
44 public event EventHandler Submit
45 {
46 add {
47 Events.AddHandler(EventSubmitKey, value);
48 }
49 remove {
50 Events.AddHandler(EventSubmitKey, value);
51 }
52 }
53 //定义事件接受方
54 protected virtual void OnSubmit(EventArgs e) {
55 EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
56 if (SubmitHandler != null)
57 SubmitHandler(this, e);
58 }
59 /**///// 实现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)
66 {
67 bool handled = false;
68 if (e is CommandEventArgs)
69 {
70 CommandEventArgs ce = (CommandEventArgs)e;
71 if (ce.CommandName == "Submit")
72 {
73 OnSubmit(EventArgs.Empty);
74 handled = true;
75 }
76 }
77 return handled;
78 }
79 // 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
80 protected override void RecreateChildControls()
81 {
82 EnsureChildControls();
83 }
84
85 //重写CreateChildControls方法,将子控件添加到复合控件中
86
87 protected override void CreateChildControls()
88 {
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)
102 {
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