当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.net组件编程中的两种事件编写方法

ASP.NET
ASP.NET创建XML Web服务全接触(3)
ASP.NET创建XML Web服务全接触(4)
ASP.NET创建XML Web服务全接触(5)
ASP.NET创建XML Web服务全接触(6)
ASP.NET创建XML Web服务全接触(7)
ASP.NET创建XML Web服务全接触(8)
ASP.NET创建XML Web服务全接触(9)
ASP.NET创建XML Web服务全接触(10)
ASP.NET创建XML Web服务全接触(11)
ASP.NET创建XML Web服务全接触(12)
ASP.NET创建XML Web服务全接触(13)
ASP.NET创建XML Web服务全接触(14)
ASP.NET创建XML Web服务全接触(15)
asp.net高级教程(一)-asp.net or asp+
asp.net高级教程(二)-转换编程思维
asp.net高级教程(三)-对象
asp.net高级教程(四)-实战篇
asp.net高级教程(五)-实战篇(中)
ASP.NET 打造互联网未来空间站(1)
ASP.NET 打造互联网未来空间站(2)

ASP.NET 中的 ASP.net组件编程中的两种事件编写方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 76 ::
收藏到网摘: n/a

以下是组件代码:


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);
}
}
}