以下是组件代码:
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
namespace nseventstudy
{
public delegate void twoeventhandle(int flag);
public class eventstudy : system.web.ui.webcontrols.webcontrol
{
///////////////第一种定义事件的方法////////////////////
public event twoeventhandle twoevent;
public void execute(int flag)
{
twoevent(flag);
}
////////////////第二种定义事件的方法////////////////////
private static object _process = new object();
public event twoeventhandle threeevent
{
add
{
events.addhandler(_process,value);
}
remove
{
events.removehandler(_process,value);
}
}
public void innerexecute(int flag)
{
twoeventhandle handle = (twoeventhandle)events[_process];
if(handle != null)
{
handle(flag);
}
else
{
this.raisebubbleevent(this,null);
}
}
protected override void render(htmltextwriter writer)
{
base.render (writer);
writer.writeline("我爱你,中国");
}
}
}
测试程序:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace testevent
{
/// <summary>
/// webform1 的摘要说明。
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.button button1;
protected nseventstudy.eventstudy eventstudy1;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.eventstudy1.threeevent += new nseventstudy.twoeventhandle(this.eventstudy1_threeevent);
this.eventstudy1.twoevent += new nseventstudy.twoeventhandle(this.eventstudy1_twoevent);
this.button1.click += new system.eventhandler(this.button1_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void eventstudy1_twoevent(int flag)
{
this.response.write("<script>javascript:alert('twoevent事件触发')</script>");
}
private void eventstudy1_threeevent(int flag)
{
this.response.write("<script>javascript:alert('threeevent事件触发')</script>");
}
private void button1_click(object sender, system.eventargs e)
{
this.eventstudy1.execute(6);
this.eventstudy1.innerexecute(10);
}
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器