您的位置:首页 > 编程语言 > ASP

asp.net WebForm 页面事件注册

2016-03-27 14:01 337 查看
asp.net WebForm开发中,我们可以进行事件注册用来对请求进行过滤操作。

常见的有两种事件注册:

(1)页面级事件注册

(2)管道事件注册

今天我们先来学习第一种事件注册方法,在接下来的文章中在学习第二种事件注册方法。

根据页面的生命周期我们知道,页面在执行的过程中会执行多个事件,具体如下所示:

1.PreIntit事件

2.Init 事件

3.IntiComplete事件

4.PreLoad事件

5.Load事件

当我们新建一个页面的时候,默认只为我们注册了Load事件,我们还可以注册上面列出的其他页面事件,具体操作如下所示。

protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("预初始化。。。。。。");
Response.Write("</br>");
}
protected void Page_Init(object sender, EventArgs e)
{

Response.Write("初始化。。。。。。");
Response.Write("</br>");
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("初始化完成。。。。。。");
Response.Write("</br>");
}
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("预加载。。。。。。");
Response.Write("</br>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("预渲染。。。。。。");
Response.Write("</br>");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("预渲染完成。。。。。。");
Response.Write("</br>");
}


操作规范:注册事件的方法名为Page_方法名,当我们以这样的方式进行命名时,asp.net 就会默认的为我们把当前方法与相应的事件关联起来(前提设置页面的AutoEventWireUp属性为true)。

运行程序,截图如下所示



注意:
上面的页面级事件也是贯穿在Application的管道事件中的。

OK,下面的文章我们会来接着学习另一种事件注册方法:asp.net 管道事件注册


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: