using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace WebControlLibrary{
public class CompositeEvent : CompositeControl {
//声明变量
private Button _button;
private TextBox _textBox;
private static readonly object EventSubmitKey = new object();
//定义属性ButtonText,用于指定按钮上的文字
[
Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置显示显示在按钮上的文字")
]
public string ButtonText {
get {
EnsureChildControls();
return _button.Text;
}
set {
EnsureChildControls();
_button.Text = value;
}
}
//定义属性Text,表示文本框的输入
[
Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本")
]
public string Text {
get {
EnsureChildControls();
return _textBox.Text;
}
set {
EnsureChildControls();
_textBox.Text = value;
}
}
// 实现事件属性结构
public event EventHandler Submit {
add {
Events.AddHandler(EventSubmitKey, value);
}
remove {
Events.RemoveHandler(EventSubmitKey, value);
}
}
// 实现OnSubmit
protected virtual void OnSubmit(EventArgs e) {
EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
if (SubmitHandler != null) {
SubmitHandler(this, e);
}
}
// 实现Submit事件引发的事件处理程序
private void _button_Click(Object source, EventArgs e) {
OnSubmit(EventArgs.Empty);
}
// 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
protected override void RecreateChildControls() {
EnsureChildControls();
}
//重写CreateChildControls方法,将子控件添加到复合控件中
protected override void CreateChildControls() {
Controls.Clear();
_button = new Button();
_textBox = new TextBox();
_button.ID = "btn";
_button.Click += new EventHandler(_button_Click);
this.Controls.Add(_button);
this.Controls.Add(_textBox);
}
//重写Render方法,呈现控件中其他的HTML代码
protected override void Render(HtmlTextWriter output) {
output.AddAttribute(HtmlTextWriterAttribute.Border, "0px");
output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "5px");
output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0px");
output.RenderBeginTag(HtmlTextWriterTag.Table);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
_textBox.RenderControl(output);
output.RenderEndTag();
output.RenderBeginTag(HtmlTextWriterTag.Td);
_button.RenderControl(output);
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="Sample" Assembly="WebControlLibrary" Namespace="WebControlLibrary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
void demo1_Submit(object sender, EventArgs e) {
lbMessage.Text = "您刚才输入的是:" + demo1.Text;
}
</script>
<html xmlns="">
<head id="Head1" runat="server">
<title>为复合控件实现事件-包含法</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<Sample:CompositeEvent ID="demo1" runat="server" ButtonText="提交" OnSubmit="demo1_Submit" />
<br />
<asp:Label ID="lbMessage" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
// OnBubbleEvent方法定义
protected virtual bool OnBubbleEvent(object source,EventArgs args){ return false;}
// RaiseBubbleEvent方法定义
protected void RaiseBubbleEvent(object source,EventArgs args){
Control currentTarget = _parent;
while(currentTarget != null) {
if(currentTarget.OnBubbleEvent(source,args) { return; }
currentTarget = currentTarget.Parent;
}
}
protected override bool OnBubbleEvent(object sender,EventArgs e){
bool handled = false;
if(e is CommandEventArgs) {
CommandEventArgs ce = (CommandEventArgs)e;
if(ce.CommandName == "ButtonClick") {
OnButtonClick(EventArgs.Empty);
handled =true;
}
}
return handled;
}
protected override bool OnBubbleEvent(object sender,EventArgs e){
if(e is CommandEventArgs) {
CommandEventArgs ce = (CommandEventArgs)e;
RaiseBubbleEvent(this,ce);
return true;
}
return false;
}
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace WebControlLibrary{
public class CompositeEvent : CompositeControl {
//声明变量
private Button _button;
private TextBox _textBox;
private static readonly object EventSubmitKey = new object();
//定义属性ButtonText,用于指定按钮上的文字
[
Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置显示显示在按钮上的文字")
]
public string ButtonText { ...... }
//定义属性Text,表示文本框的输入
[
Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本")
]
public string Text { ...... } // 实现事件属性结构
public event EventHandler Submit
{
add {
Events.AddHandler(EventSubmitKey, value);
}
remove {
Events.RemoveHandler(EventSubmitKey, value);
}
}
// 实现OnSubmit
protected virtual void OnSubmit(EventArgs e) {
EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
if (SubmitHandler != null) { SubmitHandler(this, e); }
}
// 删除_button_Click
// 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
protected override void RecreateChildControls() { ...... }
//重写CreateChildControls方法,将子控件添加到复合控件中
protected override void CreateChildControls() {
Controls.Clear();
_button = new Button();
_textBox = new TextBox();
_button.ID = "btn";
_button.CommandName = "Submit";
this.Controls.Add(_button);
this.Controls.Add(_textBox);
}
// 重写OnBubbleEvent方法,执行事件冒泡
protected override bool OnBubbleEvent(object source, EventArgs e) {
bool handled = false;
if (e is CommandEventArgs) {
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "Submit") {
OnSubmit(EventArgs.Empty);
handled = true;
}
}
return handled;
}
//重写Render方法,呈现控件中其他的HTML代码
protected override void Render(HtmlTextWriter output) { ...... }
}
}