Visual Studio 2010 - Code Snippets do not have ID property

Visual Studio code snippets are pretty handy. VS 2010 introduces few more that might improve your coding performance.

For basics of what I am talking about check these two great references.

So I went ahead to try couple of those including label, button, and textbox - most basic of all.

I went inside VS 2010 website and opened an .aspx Souce page. I typed <label + TAB

I get following markup code generated for me:  <asp:Label Text="text" runat="server" />

So it generates markup for an asp.net Label with  runat="server" along with Text property set to default value of "text". I was just amazed to see that ID property was not added by defaut. If you have noticed that when you add a control from Toolbox it will add an ID property as well for you in the generated markup. That makes sense as ID property is most frequently required to reference the control.

So to overcome that I went ahead and modified the snippet myself.

1: Opened the label.snippet file in Notepad:

C:\Program Files\Microsoft Visual Studio 10.0\Web\Snippets\HTML\1033\ASP.NET

Note: Path may vary for you depending on your installation directory but hope you get where to find it.

2: Modified the <Snippet> section in the file like below (the bold part) to include the ID property:

<Snippet>
    <Declarations>
      <Literal>
        <ID>text</ID>
        <ToolTip>text</ToolTip>
        <Default>text</Default>
      </Literal>
       <Literal>
        <ID>ID</ID>
        <ToolTip>id</ToolTip>
        <Default>id</Default>
      </Literal>
    </Declarations>
    <Code Language="html"><![CDATA[<asp:$shortcut$ text="$text$" ID="$ID$" runat="server" />$end$]]></Code>
  </Snippet>

Now when I went back to VS and typed <label + TAB

I got : <asp:Label Text="text" ID="id" runat="server" />

Cool Hmmm...So go ahead and play / modify other snippets if they don't fit your need or you can write your own snippets as well.

Check these as well:

Thanks for reading. Comments welcome.

No Comments