首页 > 编程 > .NET > 正文

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

2024-07-10 12:57:08
字体:
来源:转载
供稿:网友
以下是组件代码:
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);
}
}
}



最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表