One of my favoritest features in Visual Studio 2005 is Code Snippets. Code Snippets are XML-based definitions of commonly-used code that you can insert into your classes (VB, C#, or J#) or XML files via right-click or menu selection. In addition to a huge number of snippets that ship out of the box, you can also define your own Code Snippets and save them to your Code Snippets folder(s) (look under My Documents > Visual Studio 2005 > Code Snippets), and the IDE will pick them up automatically and make them available to you via the Insert Snippet context-menu item. There’s even a built-in XML snippet that contains a skeleton definition for a snippet, to help you get started creating your own (just create a new XML file, right-click in the XML editor, select Insert Snippet, and then double-click the Expansion item and look for the Snippet entry).
One of the things that kind of bummed me out about snippets, though, was that there isn’t any official support for inserting snippets into the markup of an ASP.NET web form (you can use snippets in code-behind, of course, just as with any class). When you right-click in the markup view of a .aspx page, no “Insert Snippet” item…boo-hoo.
Well, it’s completely unsupported, and not guaranteed to always work, but I’ve found a hack workaround that allows you to insert snippets into an ASP.NET web form…simply open the .aspx file by right-clicking it in Solution Explorer, and selecting Open With…, then choosing the XML Editor. Now, when you right-click in the editor, you’ll get the context menu item for Insert Snippet, and can insert any snippets saved into the XML snippets folder. So you can create a snippet containing ASP.NET markup, drop it into that folder, and take advantage of it via this hack.
Keep in mind that when you have a .aspx file open in the XML editor, you won’t get proper Intellisense for ASP.NET tags, so it’s best to open the file in the XML editor, insert the desired snippet, save, then close the file and reopen it normally to take advantage of Intellisense, etc.
A little birdie tells me that full snippet support is on the list for the next version of Visual Studio, but until then, this little trick should allow you to take advantage of snippets, even without official support…just don’t email me any nasty-grams if it doesn’t always work perfectly! As I said, it’s a hack. 🙂