以下是组件代码:
usingSystem;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.ComponentModel;
namespaceNSEventStudy
{
publicdelegatevoidTwoEventHandle(intflag);
publicclassEventStudy:System.Web.UI.WebControls.WebControl
{
///////////////第一种定义事件的方法////////////////////
publiceventTwoEventHandleTwoEvent;
publicvoidExecute(intflag)
{
TwoEvent(flag);
}
////////////////第二种定义事件的方法////////////////////
privatestaticobject_Process=newobject();
publiceventTwoEventHandleThreeEvent
{
add
{
Events.AddHandler(_Process,value);
}
remove
{
Events.RemoveHandler(_Process,value);
}
}
publicvoidInnerExecute(intflag)
{
TwoEventHandlehandle=(TwoEventHandle)Events[_Process];
if(handle!=null)
{
handle(flag);
}
else
{
this.RaiseBubbleEvent(this,null);
}
}
protectedoverridevoidRender(HtmlTextWriterwriter)
{
base.Render(writer);
writer.WriteLine("我爱你,中国");
}
}
}
测试程序:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceTestEvent
{
///<summary>
///WebForm1的摘要说明。
///</summary>
publicclassWebForm1:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonButton1;
protectedNSEventStudy.EventStudyEventStudy1;
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处放置用户代码以初始化页面
}
#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.EventStudy1.ThreeEvent+=newNSEventStudy.TwoEventHandle(this.EventStudy1_ThreeEvent);
this.EventStudy1.TwoEvent+=newNSEventStudy.TwoEventHandle(this.EventStudy1_TwoEvent);
this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidEventStudy1_TwoEvent(intflag)
{
this.Response.Write("<script>javascript:alert('TwoEvent事件触发')</script>");
}
privatevoidEventStudy1_ThreeEvent(intflag)
{
this.Response.Write("<script>javascript:alert('ThreeEvent事件触发')</script>");
}
privatevoidButton1_Click(objectsender,System.EventArgse)
{
this.EventStudy1.Execute(6);
this.EventStudy1.InnerExecute(10);
}
}
}