Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

The following contains the mangled id for a button when clicked.

Page.Request.Form["__EVENTTARGET"]

Here is an example that I believe answers your question.

Another way would be just to set some breakpoints when debugging.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top