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

ASP.NET
在ASP.NET中自动给URL加上超级链接
在ASP.NET中怎么用Session判断用户是否登录?
C#是一种新的语言?或者仅仅只是Java
在网页中动态的生成一个图片
C#实现的18位身份证格式验证算法
Asp.net中的页面乱码的问题
ASP.NET中利用存储过程实现模糊查询
ASP.NET 2.0中构造个性化网页
.net教程:ASP.NET GridView的分页功能
ASP.Net中无刷新执行Session身份验证
用事实说话!AJAX应用程序开发七宗罪
迁移你的Web页面到ASP.NET AJAX 1.0
SQL Server 2005中插入XML数据方法
编程技巧 用Asp.net动态生成html页面
在asp.net 2.0 中使用的存储过程解析
用 asp.net 动态设置 WebService 引用
新手入门之ASP.NET2.0中的缓存技术解析
asp.net编程中实现 MD5 加密
ASP.NET备份恢复SqlServer数据库
Asp.Net输出数据到EXCEL表格中

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


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