Useful Code Snippet: Web Property

January 15, 2008

Write WebControls much? Well, I do and I have a code snippet that I pretty much can’t live without. It is used for creating ViewState backed properties. Here is the basic pattern I use when creating properties for my web controls:

public string Name
        object o = ViewState["Name"];
        return o == null ? String.Empty : (string)o;
    set { ViewState["Name"] = value; }

Basically, you just want a property (in this case a string named “Name”) and you want to store it in ViewState. And when the value is being retrieved, if it is not in ViewState, you want to retrieve some default value. This is a pattern that I repeat over and over in my controls. So I made a code snippet to make it easier.

I use the shortcut ‘webprop’


and it expands like this:


To install, you just need to place the .snippet file (link to download below) in your ‘My Snippets’ folder which in windows vista is located at:

C:\Users\<UserName>\Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets

or if you use Visual Studio 2008:

C:\Users\<UserName>\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets

I hope this is helpful. It saves me a bunch of time.

You can download the .snipped file here

kick it on

Productivity Tip: Copy As Path

December 10, 2007

Here is a quick little tip I just learned that I use all of the time now. In Windows Vista, if you hold down the Shift key and right click on a file, a new option appears in the context menu: "Copy As Path"


This will copy the full path of the file you select to the clipboard. This is very handy for pasting file paths into a command prompt window. Why this option is so hidden, I don’t know.

I hope this saves you some time.

kick it on

Productivity tip: Visual Studio Smart Tags

November 10, 2007

In Visual Studio 2005 they added a feature that is very useful – when you type the name of a class, if you have the assembly for that class referenced but you don’t yet have a using tag in your file, it can add it for you. This is accomplished using a Smart Tag (much like the smart tags in Microsoft Office). The one problem is that if you are typing and the smart tag comes up, you have to use the mouse to click on it and choosing an option.

When you first type the word (which is case sensitive)


Then you put your mouse over the little blip and you get:


Then you click and you get the menu:


My point is that this is all a very disruptive if you just want to quickly add the using tag and keep moving. I should mention, that there is a default keyboard binding to show the smart tag menu which is Alt+Shift+F10. Personally I can never remember this short cut.

So what is the tip already?

What I like to do is to bind this shortcut to a more intuitive Alt+Down Arrow. This way I just hold down the alt button and pressing down makes sense that the menu would show.

To set this binding you must open up the Tools->Options menu in Visual Studio and go to the ‘Keyboard’ tab. In the commands box, type in ‘View.ShowSmartTag’ and you will see the command show up in the list:

Then, put your cursor in the Shortcut key box and press Alt+The Down Arrow. Click assign and OK and you are all good to go.

Now to add a using statement, you just have to type the class name, type Alt+Down Arrow, and press enter. Not very disruptive at all. This may seem small but I find this to be extremely useful in my day to day development.