Visual Studio tips for web developers
I’ve used Visual Studio in its various incarnations since 1998 and despite all short-comings I still think it’s one of the most powerful IDEs out there. There are many people who are introduced to Visual Studio today, so here are some small beginner’s tips I want to share to new ASP.NET developers.
Use server-side comments for aspx pages
I often see HTML comments used on aspx pages, like this:
One of the downsides of this method is that the comments will be visible to anyone viewing the markup of the rendered page, and unnecessary bytes will have to be transferred to the browser. Use server-side comments instead:
This will prevent the content from being rendered at all. Unfortunately server-side comments seems to be almost unknown to the developer community but they have actually been there since Visual Studio 2005.
Note that the HTML comment style is still used for certain file types, such as web.config. If you are unsure of what comment style to use, Visual Studio can do the thinking for you. Just use the keyboard shortcut Ctrl+E,C and uncomment with Ctrl+E,U. (Keyboard mappings can be different on your machine depending on environment setup.)
Set source mode to default
I really hope this one is default for everyone by now, but just to be sure. Your aspx page can be viewed in two modes: Design and Source. Design mode will hide your markup code while doing a poor job as a drag-n-drop layout tool. This is the default mode for many installations, but try to stay away from it or your markup will most likely be cluttered with non-compliant code.
Learn the shortcuts
Keyboard shortcuts are a real time-saver in complex IDEs. You probably know about the usual ones but there are some hidden gems.
For instance, Ctrl+- returns you to previous cursor location. This even works across different open files and can be nice to have for returning after Go To Definition (F12) or similar. Visual Studio saves your locations on a stack so it can be used multiple times.
Another time-saver is the fact that usual copy-paste (Ctrl+X/C/V) works even without anything being marked. Just position your cursor on a line and hit Ctrl+X to erase the entire line, no marking needed.
If you frequently type certain chunks of code, snippets may be useful. There are several pre-made ones available out of the box. For instance, if you need a property, just type prop (a unique identifiable part of the snippet shortcut name) and hit Tab twice for completion to get a code skeleton with fields for changing type and names. It’s a templated snippet so you can advance to next field by pressing Tab and move to previous by Shift+Tab.
You can view all available snippets in the Snippet Manager (Ctrl+K, Ctrl+B). Snippets are simple XML files and located in the Visual Studio program folder, so you can easily modify them or create your own.
Use correct placeholders
If you need a container or wrapper for a piece of code, I recommend using an <asp:PlaceHolder> instead of <asp:Panel>. The latter will insert an extra <div> in your markup which will likely lead to irritation and debugging if you’re not aware of it.
Once you hit a breakpoint, it’s common to step forward using F10/F11. But don’t forget the “run to cursor” feature, which may save some time. You may also set conditions on your breakpoints by right-clicking the red dot and select “Condition”. The “Hit Count” feature may also be useful while debugging loops. If you want to evaluate a statement, type it in the “Immediate window”.