我能否找出 ASP 回发时将触发哪些控制事件?
是否可以在发生之前获取将要触发的控制事件列表,例如在 Page_Load
处理程序中?
例如,如果单击了按钮,我可以在调用 button_click
事件处理程序之前弄清楚这一点吗?
Is it possible to get a list of control events that are going to fire before they happen, say inside the Page_Load
handler?
For example if a button was clicked can I figure this out before the button_click
event handler is called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您选择了一个非常棘手的问题...原因是事件触发的方式有多种。
1) __EVENTTARGET(如上所述)
2) 如果按钮的名称是 MyButt,那么您将在查询字符串中看到“MyButt=”。
3) 当每个控件(例如 TextBox)检查请求以查看 ViewState 中的值是否与发布的值不同时,将触发“Text_Changed”。
但是,您可以使用 #1 和 #2 检查一些地方。
You've picked a really tough question... the reason for this is that there are several ways that events will fire.
1) __EVENTTARGET (as mentioned above)
2) If the name of your button is MyButt, then you'll see "MyButt=" in the query string.
3) When each control (like, a TextBox for example) checks the request to see if it's value in the ViewState is different than posted, then a "Text_Changed" will fire.
But, you can use #1, and #2 to check a few places.
不幸的是,询问 __EVENTTARGET 值并不能解决问题。 通常,该值将为空。 回发处理不仅基于事件目标值(如果有),还根据表单(例如文本框)发布的值测试控件状态值来决定引发哪些事件,以确定是否应引发类似 TextChanged 的事件。
除了将事件处理程序实际连接到您希望捕获事件的所有控件之外,我认为没有任何方法可以确定它。 不过,基本上可以做框架所做的事情。 您需要在创建控件和恢复视图状态之间但在处理发布的值之前执行此操作。 您可以将当前控制值(来自视图状态)与发布的值进行比较,从本质上确定将触发哪些事件。
你的目标是什么? 也许有更好的解决方案。
Unfortunately, interrogating the __EVENTTARGET value won't do the trick. Often, that value will be empty. The postback processing makes some decisions about what events to raise based on more than just the event target value (if any) testing control state values against the values posted by the form (such as for a textbox) determines whether events like TextChanged should be raised.
Aside from actually hooking up an event handler to all the controls you wish to capture events for, I don't think there is any way to determine it. It might be possible to do basically what the framework does though. You would need to do it between when the controls have been created and viewstate has been restored but before the posted values are processed. You could compare the current control values (from viewstate) with the posted values, essentially determining what events would fire.
What is your goal with this? Perhaps there is a better solution.
以下内容包含单击按钮时的损坏 ID。
The following contains the mangled id for a button when clicked.
这是一个示例,我相信它可以回答您的问题。
另一种方法是在调试时设置一些断点。
Here is an example that I believe answers your question.
Another way would be just to set some breakpoints when debugging.