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

ASP.NET
AspNetPager与Socut.Data使用方法
asp.net UpdaeProgress的简单用法
asp.net ajaxControlToolkit ValidatorCalloutExtender的简单用法
asp.net 简易生成注册码(数字+大小写字母)
asp.net中利用ashx实现图片防盗链代码
ASP.NET程序中常用代码汇总
ASP.NET 2.0/3.5中直接操作Gridview控件插入新记录
ASP.NET Ajax级联DropDownList实现代码
ASP.NET 2.0写无限级下拉菜单
asp.net Web Services上传和下载文件(完整代码)
asp.net DataGrid控件中弹出详细信息窗口
Asp.NET 多层登陆实现代码
利用Asp.Net回调机制实现进度条
ASP.NET Ref和Out关键字区别分析
Javascript调用Webservice的多种方法
.Net下的签名与混淆图文分析
.Net Compact Framework开发小技巧 推荐
.Net连接Oracle数据库的实现代码
js获取.aspx页面里面的服务器控件和.ascx中的服务器控件值
asp.net下 jquery jason 高效传输数据

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 85 ::
收藏到网摘: 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);
}
}
}