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