Creating Custom Template for Web User Control in Visual Studio.NET 2003


For our project purpose, we may want some specific template for our user control like company specific namespace, functions to be included in by default when we create user control. This article shows you how to create custom template for web user control in visual studio.net 2003. Sample application attached will install the custom template for the name given for either VB or C# language and also uninstall the custom template.

Assumptions:

Assumed that following has been installed in the machine:

  1. .NET Framework 1.1 
  2. Visual Studio.NET 2003 
  3. Windows XP Professional

To Create Custom Control Template following steps needs to be done:

  • Add .vsdir file
  • Add .vsz file
  • Add Custom Controls template in Wizards Directory.

VsDir File:

A VsDir file is a text file which provides information to the Add Item and New Project Dialog boxes about how to display its items, such as their names, order in which they appear and icon displayed to them.

We should specify relpathname, {clsidpackage}, strPriority, description, DLLPath or {clsidpackage}, IconResourceID, flags, suggestedbasename. Pipe (|) characters separate the fields in each record.

VSZ File:
 
This file is used to launch wizards .Visual Studio uses to determine the wizard to call and what information to pass to it, if any.

In Wizards:

Need to create the default.js file under {Customname}/Scripts/1033. This default.js file adds the corresponding custom file to the project. Also .ascx and template.inf file under {Customname}/Templates/1033 folder. This contains the template for the custom control.

Installer Application:

Brief Introduction:

Attached Windows Installer Application does all the job for creating template for web user control. One has to input the custom ControlName and language as input and creates the necessary files for the custom control template in the corresponding directory.

Note:

Do not give name which already exists in CSharpProjectItems Folder (C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards) and VBProjectItems folder (C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBProjectItems). Sample attached is for creating template other than exisiting one which VS.NET 2003 provides. You are at own risk giving the custom template name same as the existing one.

What it Does:

On Install:

public void CustomControlInstaller(string strInstallpath)
{
try
{
CreateDirectoryStructure(strInstallpath);
CreateVSZFile();
CreateVSDIRFiles();
AddScriptsFile(strInstallpath);
CreateTemplateFile();
}
catch(Exception ex)
{
throw(ex);
}
finally
{
}
}

  1. Gets the install path where the Visual Studio.NET 2003 installed.
  2. Calls the CustomControlInstaller which takes strInstallpath as input.
  3. CreateDirectoryStructure  - Creates the directory structure for the custom template.
  4. CreateVSZFile  - Creates VSDIR file.
  5. CreateVSDIRFiles - Creates .vsz file.
  6. AddScriptsFile - Creates script file.
  7. CreateTemplateFile - Creates the necessary template file(.ascx and .cs in corresponding folders).

For CreateTemplateFile file input has been taken from the CustomControlcsascxcs.txt (C#) or CustomControlvbascxvb.txt(VB) for code-behind file. For ascx, CustomControlcsascx.txt(C#) or CustomControlvbascx.txt(VB).For scripts,defaultjs.txt(C#) or defaultjsvb.txt(VB).

These file's Build Action is set as "Embedded Resource". Below code retrieves the file from the running assembly and streams the content.

internal static string GetFromResources(string ResourceName)
{
try
{
Assembly assem = System .Reflection .Assembly .GetExecutingAssembly();
using (Stream stream = assem .GetManifestResourceStream (ResourceName))
{
try
{
using (StreamReader reader = new StreamReader (stream))
{
return reader.ReadToEnd();
}
}
catch(Exception e)
{
throw new Exception(e.ToString());
}
}
}
catch(Exception ex)
{
throw(ex);
}
finally
{
}
}

On Uninstall:

1. Deletes all the sub files created under customname folder and finally deletes the created directory for the {customname}

Home Screen:



To see your own custom template,

Right click on project and click on "Add new item", expand web project Items. You will see your custom control added in the list.

You can customize your web user control by directly editing  C:\Program Files\Microsoft Visual Studio.NET 2003 \ VC#\DesignerTemplates\1033\MyCustomControl.ascx.cs file .For VB, change in C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\DesignerTemplates\1033\.

Or 

In the solution file change the CustomControlcsascxcs.txt(C#) and CustomControlvbascxvb(VB) for your custom template file.


Similar Articles