简体   繁体   中英

Moving asp.net team from VB.Net to C#, seems are we are missing something with wiring up events?

An asp.net (mostly webforms) team I work on has recently decided to make the move from vb.net to c#. The people who used to be on the team who couldn't read/write C# code well had since moved on, and each time we hired a new person it became more of a problem for us to be a vb.net shop.

I'll save the standard vb/c# language war stuff and just stick with the one thing that I ran into today.

With vb.net, when we want to handle some event from some object, we just pick the object out of the list in the upper left, pick the event out of the list on the upper right, and it creates the fully wired up event handler. It's also very clear that the method is an event handler due to the "Handles ... " at the end.

In the C# world, it seems we are left with a few poor options.

1) Manually wireup the event by typing the name of the event and event handler in the aspx markup. Like actually writing "OnLoad=SomeFunction" in the aspx. (EDIT: I didn't realize that his only applied to the form and not to any contained controls, so I guess this isn't really an option)

2) Using auto wireup events, and naming methods and signatures correctly so that they match up.

I'm hoping I'm missing some other much better option.

With these 2 options that I've seen so far:

1) we have to know the event names and event arguments. We can't just look down the list and be like "Oh good this has OnItemClick", I have to go research that in some control documentation. Likewise I'd have to go research the event args to find out that ItemClick needs a RadPanelBarEventArgs as the 2nd parameter. Wasted time.

2) I get no compile check on anything I'm doing with regard to connecting events. If I name the method signature wrong, or if I type it in wrong in the aspx page, everything will compile just fine until runtime when the mismatch will blow things up. (EDIT: what I mean is if I type onClick="ABC" and the event handler is really named "ABCD" this will not be caught at compile time. With VB, issues with renaming/mistyping event handler names are caught at compile time, because of the "Handles". Same goes for if I have onClick="ABC" and this controls onClick really needs a SpecialEventArgs but I accidentally use SomeRegularEventArgs)

I'm hoping we missed something and there is a better way to do this.

This is a known 'bug' or missing feature. See Visual studio 2010 showing available events from code behind

If you want to auto-generate events in C#:

  • Go to your aspx page in Design View.
  • Make sure the properties panel is visible
  • Either select a control with your mouse, or use the dropdown under properties to select the control.
  • Move to the Events tab (the lightning bolt)
  • Find the event you want to wire up and either write in a name and hit Enter, leave it empty and hit Enter, or Double-click it.
  • It will create the event handler with all the properties.

As for Number 2. Are you putting your code in a code-behind file? .aspx.cs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM