Click here to Skip to main content
13,408,284 members (40,028 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked
Posted 5 Jan 2004

How to build your own ASP.NET templates for VS.NET

, 6 Jan 2004
Rate this:
Please Sign up or sign in to vote.
This article explains how to add custom templates for both aspx and code behind files in VS.NET's "add new item" menu


Over the last 2 years and 5 small and midsize business related web projects I got tired of copying, pasting and deleting code between projects and pages, I've figured out that either web developers are extremely diligent or I'm missing a big thing here, as a lazy being I've decided that I'm missing something and went looking for it, it wasn't there.


You should probably read these articles first :

The missing link

Having read all articles suggested above you may have noticed that a crucial part is missing for web developers : manipulating templates of the code behind. If you haven't read the articles above I strongly recommend it, at least the first one as it supply a good tutorial for web pages templates but here is a short brief : (skip to the interesting part)

When adding a new item to a web project VS.NET goes to a certain file that holds registration of file types that can be added to your project, this file is located in

C:\Program Files\Microsoft Visual Studio .NET
on a default installation and is called WebProjectItems.vsdir, it looks like this :

We need to copy and mess a little bit with this file so here goes :

  • step 1 : create a folder in WebProjectItems directory called MyCustomTemplates
  • step 2 : copy WebProjectItems.vsdir to the newly created directory and rename it MyCustomTemplates.vsdir
  • step 3 : open the file using VS.NET, notepad or any other text editor
  • step 4 : leave only the row that has WebForm.aspx at the end of it (usually the first long one) and delete all the other rows in this file

As for vsdir files here is a small legend :(taken from vsdir files in the MSDN)

Field Meaning
RelPathNameRequired. The name of the Wizard's .vsz file, such as MyWizard.vsz.
{clsidPackage}Optional. A GUID representing a product (such as Visual C++) that has a DLL containing localized resources. Normally, this field is blank for VSDir files that correspond with third-party Wizards.
LocalizedNameOptional. This is the localizable name of the Wizard or template and the name that appears in the Add Item dialog box. This can be a string or a resource identifier of the form #ResID.
SortPriorityRequired. An integer representing the sort order and relative priority of the Wizard, with 1 being highest. For instance, if this item is "1," then this will appear next to other 1s and ahead of all 2s or lower.
DescriptionRequired. A localizable description of the template or Wizard as it will appear in the Add Item dialog box when the item is selected. This can be a string or a resource identifier of the form #ResID. Applies only to template files, not folders.
DLLPath or {clsidPackage}Required. Specifies a full path to a DLL or EXE file, or a GUID of a product that has a .dll file that contains an icon to load for the Wizard. The icon is loaded as a resource out of a DLL/EXE file using the given IconResourceId. This setting overrides {clsidPackage}, if specified, for icon location. Applies only to template files, not folders.
IconResourceIdOptional. A resource identifier within the DLL file that determines the icon to display. If no icon is defined, the environment substitutes the default icon for a file with the same extension as the item. Applies only to template files, not folders.
FlagsRequired. See Flags description and table below. Applies only to template files, not folders.
SuggestedBaseNameRequired. The default name for the Wizard, displayed in the Name field in the dialog box. This is either a string or a resource identifier of the form #ResID. If the name is not unique, the environment appends the name with an integer. For example, MyFile.asp might be changed to MyFile1.asp. If no name is provided, then "Project" is used. Applies only to template files, not folders.

Now that we understand all this mambo jambo in the vsdir file we can carry on :

  • step 5 : rename the ..\CSharpAddWebFormWiz.vsz at the begining of your file to : ..\..\MyCustomTemplatesAddWebFormWiz.vsz
  • step 6 : change #2236 to my page this supplys a description to the icon in the "add new item" right window
  • step 7 : change WebForm.aspx to myPage.aspx this supplys the name for the new file

That's it for our .vsdir file. This is how the file should look like now :

    {FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|my page|

We now move on to the .vsz file :

  • Go to
    C:\Program Files\Microsoft Visual Studio .NET 
  • Copy CSharpAddWebFormWiz.vsz and rename it to MyCustomTemplatesAddWebFormWiz.vsz

Now if we look at it in notepad it looks like this :

Change the "WIZARD_NAME=CSharpAddWebFormWiz" param to "WIZARD_NAME=MyCustomTemplatesAddWebFormWiz". That's it.

Let's move on :

  • 1. Go to C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards
  • 2. Scroll down and find CSharpAddWebFormWiz directory
  • 3. Copy it and rename the copy to MyCustomTemplatesAddWebFormWiz
  • 4. Inside you'll notice two directories : scripts and templates
  • 5. If you want to change the WebForm.aspx code this is the time.
  • 6. Save all changes, close all instances of VS.NET and open a web project (existing or a new one) choose the "add new item" on the project right click menu, on the left window you should see Mycustomtemplates directory under add Project Item and on the right window you should see your custom page "my Page".
  • 7. On the file name you should see myPagen.aspx (where n is a number added by VS.NET) .
If something didn't work, follow the instructions and correct any mistakes.

The interesting part

If you have read the articles above and apply the methodology suggested you have noticed that there is no reference to the code behind WebForm.aspx.cs, it is not under the templates directory and apparently it is created by the wizard package using a template taken from somewhere else, this is actually what happens, the template for the code behind file resides on the

            Files\Microsoft Visual Studio .NET 
file. You can check it by changing this file and add a web page by the regular template or your newly created template. This suggests that you can't modify this file because the code you will put there will apply to all your templates and you probably do not want that. So I looked around for a solution and found it in the CSharpAddMobileWebFormWiz under
C:\Program Files\Microsoft 
            Visual Studio .NET 2003\VC#\VC#Wizards\
. For some reason they (MS) wrote another wizard for mobile forms and included the MobileWebForm1.aspx.cs under the templates directory.

So all we need to do now is this :

  1. Add the MobileWebForm1.aspx.cs to our MyCustomTemplates\templates\1033 directory, rename it to WebForm1.aspx.cs .
  2. Rename the default.js in the MyCustomTemplates\scripts\1033 directory to old default.js
  3. Copy default.js script from CSharpAddMobileWebFormWiz\scripts\1033 to MyCustomTemplates\scripts\1033 and open it in VS.NET or notepad.
  4. Go to line 278 and change "MobileWebForm1.aspx.cs" to "WebForm1.aspx.cs"
  5. Go to line 278 and change "MobileWebForm1.aspx" to "WebForm1.aspx" you will now need the old default.js,
  6. Open it in a text editor and go to line 2 copy all 3 first functions shown here :
function AddDefaultServerScriptToWizard(selProj)
    wizard.AddSymbol("DEFAULT_SERVER_SCRIPT", "JavaScript");

function AddDefaultClientScriptToWizard(selProj)
    var prjScriptLang = selProj.Properties("DefaultClientScript").Value;
    // 0 = JScript
    // 1 = VBScript
    if(prjScriptLang == 0)
        wizard.AddSymbol("DEFAULT_CLIENT_SCRIPT", "JavaScript");
        wizard.AddSymbol("DEFAULT_CLIENT_SCRIPT", "VBScript");

function AddDefaultDefaultHTMLPageLayoutToWizard(selProj)
    var prjPageLayout = selProj.Properties("DefaultHTMLPageLayout").Value;
    // 0 = FlowLayout
    // 1 = GridLayout
    if(prjPageLayout == 0)
        wizard.AddSymbol("DEFAULT_HTML_LAYOUT", "FlowLayout");
        wizard.AddSymbol("DEFAULT_HTML_LAYOUT", "GridLayout");

  1. Copy them to your new default.js
  2. go to line 59 (function AddFileToVSProject) and add calls to these functions:
Notice that there is an extra call here to AddDefaultTargetSchemaToWizard(selProj) this function adds the target schema to tha page and is not existed in the original default.js file (dont know where it comes from...) but it works so I don't really care right now.
  1. open the WebForm1.aspx.cs and remove the references
    and System.Web.UI.MobileControls
  2. change the inheritance of the web page to from this :
public class myPage1 : System.Web.UI.MobileControls.MobilePage 
to this :
public class myPage1 : System.Web.UI.Page
  1. add some of your code to the code behind.
private void initializePageParams()

private void BindGrid()

//whatever you want...

That's about it, save all your work and close all instances of VS.NET open a web project and add an item using your template .

Known issues

When adding a page using your new template VS.NET will show you this alert :

You should choose no.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Israel Israel
c# and developer

You may also be interested in...


Comments and Discussions

GeneralRE: ProjectItem object doesn't Return from AddFormTemplate Method in Visual C++ Pin
Karthikeyan .g26-Mar-07 22:49
memberKarthikeyan .g26-Mar-07 22:49 
QuestionHow to deploy this template to other machines? Pin
wissamt15-Jun-06 3:21
memberwissamt15-Jun-06 3:21 
GeneralAvoiding the pop up screen about replacing the file Pin
Quip19-Sep-05 5:39
memberQuip19-Sep-05 5:39 
GeneralRe: Avoiding the pop up screen about replacing the file Pin
moiztankiwala29-Apr-09 7:41
membermoiztankiwala29-Apr-09 7:41 
GeneralMobile reference Pin
awiesendanger29-Mar-05 4:51
memberawiesendanger29-Mar-05 4:51 
GeneralAdd Web UserControl template Pin
Yitzhak Gootvilig28-Feb-05 21:10
memberYitzhak Gootvilig28-Feb-05 21:10 
GeneralSubfolders Pin
Levi.Rosol26-Dec-04 21:00
memberLevi.Rosol26-Dec-04 21:00 
GeneralRe: Subfolders Pin
MajinLoki6-Jul-05 9:53
memberMajinLoki6-Jul-05 9:53 
QuestionHow to add Imports Statements. Pin
NehalSS19-Oct-04 5:13
memberNehalSS19-Oct-04 5:13 
GeneralObject Expected Pin
deb200315-Oct-04 13:03
memberdeb200315-Oct-04 13:03 
GeneralRe: Object Expected Pin
deb200316-Oct-04 8:19
memberdeb200316-Oct-04 8:19 
GeneralRe: Object Expected Pin
RLyda10-Aug-05 8:59
memberRLyda10-Aug-05 8:59 
GeneralGetting rid of the 'file already exists' message Pin
Torra2-Feb-04 7:00
memberTorra2-Feb-04 7:00 
GeneralRe: Getting rid of the 'file already exists' message Pin
Antonio Cisternino22-May-04 11:59
memberAntonio Cisternino22-May-04 11:59 
GeneralRe: Getting rid of the 'file already exists' message Pin
moiztankiwala29-Apr-09 7:43
membermoiztankiwala29-Apr-09 7:43 
GeneralRe: Getting rid of the 'file already exists' message Pin
Thenmozhi17-Sep-04 5:33
sussThenmozhi17-Sep-04 5:33 
GeneralRe: How to attcah the code behind template in a custom web project at the time of creating the custom web project? Pin
Anonymous27-Jan-04 6:44
sussAnonymous27-Jan-04 6:44 
GeneralRe: How to attcah the code behind template in a custom web project at the time of creating the custom web project? Pin
rugha27-Jan-04 22:04
memberrugha27-Jan-04 22:04 
QuestionHow to attcah the code behind template in a custom web project at the time of creating the custom web project? Pin
mukeshdas18-Jan-04 4:59
membermukeshdas18-Jan-04 4:59 
AnswerRe: How to attcah the code behind template in a custom web project at the time of creating the custom web project? Pin
rugha18-Jan-04 13:24
memberrugha18-Jan-04 13:24 
GeneralRe: How to attcah the code behind template in a custom web project at the time of creating the custom web project? Pin
mukeshdas18-Jan-04 19:51
membermukeshdas18-Jan-04 19:51 
GeneralRe: How to attcah the code behind template in a custom web project at the time of creating the custom web project? Pin
rugha18-Jan-04 20:57
memberrugha18-Jan-04 20:57 
GeneralVS can't find default.js Pin
jenia115-Jan-04 9:12
memberjenia115-Jan-04 9:12 
GeneralRe: VS can't find default.js Pin
rugha17-Jan-04 20:47
memberrugha17-Jan-04 20:47 
GeneralRe: VS can't find default.js Pin
RLyda10-Aug-05 9:00
memberRLyda10-Aug-05 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180212.1 | Last Updated 7 Jan 2004
Article Copyright 2004 by rugha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid