天天看点

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

以下是组件代码:

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.collections;

usingsystem.data;

usingsystem.drawing;

usingsystem.web;

usingsystem.web.sessionstate;

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

///设计器支持所需的方法-不要使用代码编辑器修改

///此方法的内容。

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

继续阅读