December 2005 - Posts

Code Snippets in Visual Studio 2005
27 December 05 04:05 PM | Scott Mitchell

If you are like me, you live inside of Visual Studio. I have at least one instance of VS open virtually round the clock, and, as I've discussed here before, use VS to manage not only my ASP.NET applications, but also as the editor of choice for managing sprocs, UDFs, and views in my databases. (Granted, SQL Server 2005's Enterprise Manager makes working with these database objects a much friendlier experience than SQL Server 2000's EM, but using VS makes it a cinch to have these objects' script stored in source control...)

With Visual Studio .NET, developers could extend the system through little known tips and tricks or macros, making their time with the tool more productive. Books like Visual Studio Hacks are a great place to learn secrets for better working with this IDE. (More info on this book here.)

Visual Studio 2005 offers a super cool productivity enhancement, IntelliSense Code Snippets. Code snippets in Visual Studio 2005 takes IntelliSense to the next level, allowing developers to define their own mini-IntelliSense expansions. If you use Visual Basic you're no doubt familiar with the expansions VB provides for you - enter in Public Property name As Type and hit Enter, and VB fills in the Get and Set accessors automatically. It's always so annoying going back to the C# world and having to type in the complete property syntax by hand after you've done some coding in Visual Basic for a spell. Fortunately, these annoyances end with VS 2005 and Code Snippets. With Code Snippets you can define such expansions for C# (although many of VB's common expansions already exist and are part of the default Visual Studio 2005 installation), as well as invent new ones that can be used easily in either the VB or C# worlds.

My latest 4Guys article, Creating and Using Code Snippets in Visual Studio 2005, shows how easy it is to create and use Code Snippets. Not only can you create your own Code Snippets, but Code Snippets can be imported easily and even downloaded and installed from the web with one click. Sites like make this process as easy as point and click. Just search the website for snippets that would be of use to you, then click the Download Now! button to install the snippet on your computer.

Filed under:
My New Toolbox Column on MSDN
13 December 05 08:55 AM | Scott Mitchell

Starting with the January 2006 issue, I will be authoring a new MSDN Magazine column titled Toolbox. This column replaces the New Stuff column from issues past and examines two to three new tools developers should be aware of, along with a review of a book or two. The Toolbox column in the January 2006 issue covers three products:

The article also provides a review of James Avery's book Visual Studio Hacks.

If you have suggestions for books or products to review in the column, please don't hesitate to send them to You can keep abreast of the latest Toolbox articles through the column's RSS feed or the Toolbox column category here on my blog.

ASP.NET 2.0 Article Series on 4Guys
06 December 05 02:35 PM | Scott Mitchell

Back in the version 1.x days of ASP.NET I wrote a 16-part article series on the DataGrid Web control, aptly titled, An Extensive Examination of the DataGrid Web Control. Writing such a lengthy article in piecemeal makes the article navigation a bit hard. The entire series is composed of 16 articles (referred to as Part 1, Part 2, etc.), and each article may consists of various pages, each page referred to as a “part.” Whoops, poor wood choice on my part. :-p

Another two challenges was that it was impossible for readers to keep up with the article short of subscribing to the 4Guys newsletter or the main RSS feed. There was no way for readers who just wanted to know when a new article in the series was being published to be alerted upon publication. Finally, when reaching the end of one article in the series, it was impossible to immediately jump to the next article in the series, since the index of all of the parts was only shown at the beginning of each article.

As I've started creating ASP.NET version 2.0 content on 4Guys, I've been creating more article series. Personally, I don't see any way around it - there's so much 2.0 information and each topic has an amazing breadth of features and nooks and crannies to explore, that it's seemingly impossible to just have one article on site navigation, or callback scripts, or membership, or the provider model, and so on. Last month I started my first article series on 2.0 material, beginning by Examining ASP.NET 2.0's Site Navigation, of which there is currently both a Part 1 and Part 2. Tomorrow's front-page 4Guys article will be Examining ASP.NET 2.0's Membership, Roles, and Profile.

With both of these new article series - and upcoming ones - I'm including the full article index at the start and ends of the articles. Additionally, I've added a specific RSS feed for each series. This allows those interested to be alerted when a new article in the series is published. Thanks to XSLT stylesheets, these RSS feeds also provide a one-stop place to see a complete list of all of the articles in the series along with synopses. Check out the site navigation feed and membership, roles, and profile feed.


Filed under:
Quickly Opening a Visual Studio Website Project
05 December 05 06:42 PM | Scott Mitchell

On my computer I have a directory called Tutorial Sites and under that directory have a subfolder for various file system-based ASP.NET 2.0 websites I create to either have as a playground of sorts, to try out some new fangled feature of 2.0, or as a demo for class or article. What's kind of annoying is the process of creating and opening these sites. Yes, it's a lot quicker than creating a vdir in IIS and doesn't clutter the website metabase, but to create a file system-based website I need to launch VS 2005, click Open Website, then tinker down to the directory and choose a name. To open one, I have to do the same.

Most of the time I find myself using Windows Explorer to navigate around the file system. What I'd really like to be able to do is just right-click on a folder and say, “Open as Website” and, poof, Visual Studio would open and load that website. Clearly this makes opening an existing website as easy as pointing and clicking; to create a new website I'd just create a new folder through Windows Explorer, then open it in VS through the context menu.

Today I finally decided to stop wishing, invest some time, and actually get something that works. Here's what I tried:

  • Visual Studio can be launched through the command-line by running devenv.exe. Therefore, my exploration started with examining devenv.exe's command-line switches. With an existing Solution or Project file, you can pass in the project or solution file and, voila, it will open. However, I didn't want a dependence on a project file - I just wanted to be able to right-click on a folder in Explorer and say, “Open as Website.“
  • I did notice that VS can accept commands through the command-line switch. Commands can be activated through the Command Window in VS and allow for interacting with the GUI. For example, you can do File.OpenWebSite. However, this just pops up the Open Website dialog box - you still have to pick a file to open. And, sadly, you can't pass in an argument to this particular command. Shucks.
  • I then though that maybe macros would solve my problem. I probably spent far too long poking through what little macro documentation I could find. Is there not a one-stop reference for the macro API?
  • After banging my head on macros for a while, I decided to try something new, and ended up with a solution that works for me, but is a bit 'messy,' as you'll see in a moment.

Since Visual Studio will let you open a solution or project file passed through the command-line, I ended up writing a managed command-line program that could be passed the folder path of the website to open and a folder path to place a project file. This program would then create an actual project file and then launch devenv.exe, passing in this just-created project file.

What I don't like about my approach is that:

  • It creates these 'garbage' project files that never get deleted unless you do so manually. Granted, these files are like 1-2KBs in size and can be tucked away in a directory out of sight, but it still feels dirty to do this.
  • Second, I don't know how to get the path to devenv.exe, so the program has the path hard-coded (C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe). The program does accept an optional third input parameter, though, that can be used to specify the path to devenv.exe.

The complete code can be downloaded here. Once you have the code downloaded and compiled, to add the “Open as Website” option to the Windows Explorer context menu you'll need to run the .reg file included in the download. This adds a registry entry in HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\ that runs the program, passing in the path folder of that was just right-clicked. You will need to customize this .reg file before you run it, putting in the path to the program. You'll also need to specify the path to dump the project files. I chose the folder C:\Documents and Settings\Scott\My Documents\Visual Studio 2005\Projects\TempWebsiteProjects, but any path should suffice.

If you have any ideas or know-how on how to improve this, or run into any problems, please don't hesitate to post a comment to this blog entry. Special thanks to Chris Frazier and Robert McLaws, who both developed a “Start the ASP.NET 2.0 Development Web Server“ concept on their own. More info at Robert's “ASP.NET 2.0 Web Server Here“ Shell Extension.


Filed under:
Displaying a Formatted Email Addresses in a GridView or DetailsView (The HyperLinkField Doesn't Cut the Mustard!)
02 December 05 05:12 PM | Scott Mitchell

If you have an Email field in a database and want to display this email as a formatted email address - mailto:emailAddress - in a GridView or DetailsView control in ASP.NET 2.0 you might initially reach for the ol' trusty HyperLinkField, setting its DataTextField property to EmailIDColumnName and its DataNavigateUrlFields and DataNavigateUrlFormatString properties to EmailIDColumnName and mailto:{0}, respectively. Doing so won't give you any warning or error message, but when you view the page in a browser you'll find that the email address is displayed, but not as a clickable email address. Boo.

According to ninatang, an ASP.NET Team Member:

This is due to a security change introduced after Beta2 for ImageField and HyperLinkField to remove the src/href if the data was potentially malicious. You can work around this change by using a TemplateField instead.
[Read nina's messageboard post...]

To remedy this, use a TemplateField, as nina suggests. The easiest way to do this is (IMO) is to add the HyperLinkField and configure its properties as I noted above through the Fields dialog box in the Design view (reached by choosing Edit Columns/Fields from the GridView/DetailsView's smart tag), and then click the “Convert this field into a TemplateField” link. Or, if you prefer to do this by hand in the Source view you can use:

<asp:HyperLink runat=”server” Text='<%# Eval(“EmailIDColumnName“) %>' NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' />

Filed under:
More Posts


My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.