0

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.

chrismay
  • 1,384
  • 2
  • 15
  • 25
  • On a tangent here, why are you switching to C#? If your current team is big and has more VB.Net experience, it doesn't make sense to toss all that knowledge aside. – Only Bolivian Here Oct 06 '11 at 04:08
  • Or team isn't that big, only 8 at the moment. Back when it was just 2-3 of us, I was the only one who was ok with using whatever language, the others were coming from asp classic, and so using vb.net as our standard was the obvious choice. But it seems using VB is an uphill battle, not because it can't do the job, because I actually think it works better and is easier to read, but in terms of tool support, online examples, and hiring new developers. There is a stigma associated with it. Some devs don't want to move to a company that uses it. – chrismay Oct 06 '11 at 13:02

1 Answers1

4

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?

Community
  • 1
  • 1
Doozer Blake
  • 7,677
  • 2
  • 29
  • 40
  • Thanks this is exactly what I was hoping to find. I edited my question to explain the compile time thing more clearly. What I mean is, in VB.Net the wiring up of controls to event handlers is checked at compile time. If I have Sub X(sender as object, e as eventargs) Handles MyButton.Click, it will only compile if 1) there is a control named MyButton with a Click event, and that X is defined correctly to support the Click event. – chrismay Oct 06 '11 at 12:56
  • @chrismay Ahh, that makes more sense. You're right, C# does not check that events exist and/or are correct. As far as I know, there's no work around for that. – Doozer Blake Oct 06 '11 at 12:58
  • If I refactor the code later to change "X" to a better name like "SaveButtonClick", I don't have to remember to change anything in my aspx. In C#, if I don't remember to correctly rename the aspx "onClick" handler, it will compile, but not function at runtime. It's more reliant on the non-checked string I type in the markup for the onClick handler. – chrismay Oct 06 '11 at 12:58