何时连接事件处理程序 asp.net [英] When to wire up event handlers asp.net

查看:15
本文介绍了何时连接事件处理程序 asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个非常标准的表单,带有一个文本框和一个按钮(为简单起见).你想处理一个 Click 事件并根据用户的输入做一些事情.

Let's say we have a pretty standard form with a textbox and a button (for simplicity). You want to handle a Click event and do some stuff based on user's input.

我想知道,究竟何时在代码隐藏中为 Click 事件连接事件处理程序有关系吗?如果是这样,最好的放置位置在哪里?页面加载?页面初始化?这两个地方我都试过了,但没有发现任何区别.还是只是程序员的个人喜好?我已经在网上搜索了几次,但没有找到满意的答案.

I was wondering, does it matter, when exactly you wire up an event handler for the Click event in a code-behind? If it does, where is the best place to put it? Page load? Page init? I've tried both places, but didn't notice any difference. Or it's just a personal preference of the programmer? I've already searched the internet couple of times, but haven't found any satisfactory answer.

我知道实际方法何时执行,只是不确定接线部分.

I know when the actual method execute, just not sure about the wiring-up part.

推荐答案

如你所知,Page_xxx 事件处理程序有好几个,如 InitLoad, Prerender...这些事件存在于控件、页面以及用户控件中(实际上它们是从 Control 派生的,它包含所有这些事件).

As you know, there are several Page_xxx event handlers, like Init, Load, Prerender... This events exist in Controls, and Pages as well as User controls (in fact they're derived form Control, which holds all these events).

此事件与 ASP 有关.NET 页面生命周期

如果您仔细阅读此链接指向的页面,您将了解事件何时触发.因此,如果您将事件处理程序绑定到在事件触发之前发生的任何页面生命周期事件中,则可以保证您的事件处理程序将及时绑定以被触发.

If you read the page pointed to by this link carefully you will understand when the events are triggered. So, if you bind your event handler in any page lifecycle event that happens before the events are triggered, it's guaranteed that your event handlers will be bound in time to be triggered.

这些是主要的生命周期步骤:

These are the main lifecycle steps:

PreInit -> Init -> InitComplete -> PreLoad -> Load -> [Control events] ->
LoadComplete -> PreRender -> SaveStateComplete -> Render -> Unload

并非所有的事件都有关联的事件,但是,如果有必要,您可以覆盖相应的 OnXxx() 函数,例如 OnPreInit().(这通常只在自定义服务器控件上完成).

Not all of them have associated events, but, if it's necessary you can override the corresponding OnXxx() function, like OnPreInit(). (This is usually only done on custom server controls).

您可以在Page_InitPage_Load中绑定事件,因为控件事件是在所有控件加载完成后触发的.Load 步骤以自上而下的方式发生,首先在 Page 中,然后在所有子控件中递归.

You can bind events in Page_Init or Page_Load, because the control events are triggerd after the loading of all the controls has finished. The Load step happens in top-bottom way, first in the Page, and then recursively in all the children controls.

Load 完成后,最先触发的事件是Change Events,如TextChangedSelectionChanged.然后触发所有其他事件,例如 Click.

After Load finishes, the first events which are triggered are the Change Events, like TextChanged or SelectionChanged. Then are triggered all the other events, like Click.

如果您在 PreRender 或 Unload 中绑定事件,它们将不会被触发.如果您在 Init 或 Load 中这样做,它们会.

所以看起来在 Init 或 Load 中绑定是安全的,但事实并非如此:

看起来似乎没有特殊原因将它们绑定在 InitLoad 上,因为它们将在页面生命周期的后期被触发.但是,由于 .aspx 中定义的绑定发生在 Init 期间,程序员会期望所有事件都已绑定在 Load 事件中.如果这个程序员在后面的代码中引发了子控件的事件会发生什么?Load 事件首先发生在控件树的根节点,然后递归地发生在所有子节点上.因此,当程序员试图引发子控件的事件时,它不会已经被绑定.所以这不会按预期工作.这足以考虑在 Load 事件中绑定事件是不安全的.这就是为什么您应该始终在 Init 中绑定事件.

It could look like there's no special reason to bind them on Init or Load, because they'll be triggered later in the page life cycle. But, as the binding defined in the .aspx happens during Init, a programmer will expect that all events are already bound in the Load event. What would happen if this programmer raised an event of a child control in code behind? The Load event happens first in the root of the control tree, and them on all of the children, recursively. So, by the time the programmer is trying to raise the event of the child control, it won't be already bound. So this won't work as expected. This is more than enough to consider unsafe to bind events in Load event. That's why you should always bind events in Init.

看这张图可以看到Page&的执行顺序儿童活动:ASP.NET页面生命周期图

Look at this diagram to see the order of execution of Page & children events: ASP.NET Page Life Cycle Diagram

这篇关于何时连接事件处理程序 asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆