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

ASP.NET
为T-SQL添加intellisense功能
SQL Server 2005安装过程中出现错误的解决办法
SQL Server 2005 RTM 安装错误 :The SQL Server System Configuration Checker cannot be executed due to
有关于JSON的一些资料
不能忽略c#中的using和as操作符的用处
JavaScript系列之―同步还是异步?
获取远程网页的内容之一(downmoon原创)
获取远程网页的内容之二(downmoon原创)
ASP.Net中防止刷新自动触发事件的解决方案
asp.net下用js实现鼠标移至小图,自动显示相应大图
Asp.Net 和 AJAX.Net 的区别
提交页面的定位--scrollIntoView的用法
利用AJAX与数据岛实现无刷新绑定
asp.net下判断用户什么时候离开,以什么方式离开
DataSet 添加数据集、行、列、主键和外键等操作示例
读写xml所有节点个人小结 和 读取xml节点的数据总结
收藏的asp.net文件上传类源码
asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧
一个可以让.net程序在非WIN平台上运行的软件Mono
使用ASP.NET 2.0 CSS 控件适配器生成CSS友好的HTML输出

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


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