I have been wondering for a while; but especially since over the past few weeks I have focused more on developing interfaces. This may sound like a broad question, but hopefully there is an answer or reason:
Why aren't .NET web control event handlers running?
Reasoning
The reason I ask is due to the subtlety and elegance of strictly typed event handlers. In my entire project, where necessary, I usually use the .NET EventHandler<T>
delegate that exists with .NET 2.0; like here .
public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs
It would be relatively straightforward to expand on this and define the type for sender
, something like this.
public delegate void EventHandler<TSender, TArgs>(TSender sender, TArgs args) where TArgs : EventArgs
Whenever I work with .NET controls, sometimes I bind the event handler in the code, not in the ASPX file, and then, if necessary, throw the object
on the desired type if I need to perform additional checks or changes.
Existing
Definition
public class Button : WebControl, IButtonControl, IPostBackEventHandler { public event EventHandler Click; }
Implementation
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.MyButton.Click += new EventHandler(MyButton_Click); } protected void MyButton_Click(object sender, EventArgs e) {
Generic
Definition
public class Button : WebControl, IButtonControl, IPostBackEventHandler { public event EventHandler<Button, EventArgs> Click; }
Implementation
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.MyButton.Click += new EventHandler(MyButton_Click); } protected void MyButton_Click(Button sender, EventArgs e) {
I know this is a relatively small change, but of course it is more elegant? :)