如果要连接的事件处理程序asp.net [英] When to wire up event handlers asp.net

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

问题描述

让我们说我们有一个pretty标准形式一个文本框和一个按钮(为简单起见)。你要根据用户的输入来处理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事件的事件处理程序在code-背后?如果是这样,这里是把它最好的地方?页面加载?页面初始化?我已经试过这两个地方,但没有发现任何区别。或者这只是一个个人的$ P $程序员pference?我已经在网上搜索几次,但没有发现任何满意的答复。

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 事件处理程序,如初始化加载 prerender ...此事件中存在的控制和Pages以及用户控制(事实上它们派生形式控制,其中包含所有这些事件)。

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()功能,如在preINIT()。 (这通常只对自定义服务器控件完成)。

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_Init 的Page_Load ,因为控制事件在加载后triggerd所有的控件已完成即可。在加载一步发生在从上到下的方式,首先在页,然后在递归所有的孩子控制。

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.

加载饰面,这是引发了第一次活动是更改事件,如框TextChanged 的SelectionChanged 。然后触发所有其他活动,如点击

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或卸载的事件,他们就不会被触发。如果您在初始化或负载所做的,他们会

所以它可能看起来像它的安全,在初始化或负载进行绑定,但事实并非如此

它可能看起来像有绑定他们在初始化无特殊原因加载,因为他们会在稍后在页面生命周期触发。但是,作为绑定在的.aspx 初始化发生,程序员会想到,所有的事件都已经绑定的定义在加载事件。如果程序员提出了code后面的子控件的事件会发生什么?在加载事件首先发生在控制树的根,和他们所有的孩子,递归。所以,由程序员试图提高子控制的情况下的时间,它不会被已经绑定。因此,作为预计今年将无法正常工作。这是绰绰有余考虑不安全绑定在加载事件的事件。这就是为什么您应该总是绑定事件的初始化

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.

看看下面这张图,看看网页和放大器的执行顺序;孩子的事件:
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天全站免登陆