Click here to Skip to main content
13,590,480 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 25 Apr 2011
Licenced CPOL

Customization of Project and Item Templates - Part III

, 25 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Creation of Project and Item Templates in Visual Studio - Part III


At the time of adding new project item file, the item template will generate coding for the text file, whether it is a source code or XML generation. In Visual Studio, the code generation is done using T4 text templates which is a mixture of text blocks and control logic to generate a text file.


Part I and part II of this article can be viewed using the links below:

T4 Text Template Generators

Text file generation can be done using T4 text templates in two ways. We can run the T4 text templates during runtime or design-time.

Runtime T4 text templates do not require Visual Studio. It is mostly used to generate HTML or XML report files. Design-time T4 text templates can be used to generate source code files in Visual Studio. It is mostly used to read an input data file or database and generate a source code of either .cs or .vb.

We can customize the text template generation to implement our own control logic.

Here, we will see how to customize design-time T4 text template generators by generating .aspx page file.

Creation of Text Template Generators

  • Create a new Web Application
  • Add a new Item dialog box and select Text Template from the Templates pane. It will create an extension of .tt.

The ViewPage text template displays data from the custom data class in a table element. Whenever you make changes in the Text Template and click save, it will ask for running the Text Template generator. A new file ViewPage.aspx will be generated.

Add the following code in the Text Template. CustomTool property of the Text Template file will be set to TextTemplatingFileGenerator.

The '<#@ template' directive in the first line of file specifies that it is a template file. The output directive specifies the extension name of the generated code file. Here, it will be ViewPage.aspx. The assembly directive is used for referencing other assemblies. The import directive is the same as using in C#.

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".aspx" #>
<#@ assembly name="$(SolutionDir)bin\WebApplication1.dll" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Core" #>

<#@ import namespace="WebApplication1" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.ObjectModel" #>

<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 


    <h2>Customer Lists<h2>

  data cls = new data();

  ObservableCollection<Customer> customers = cls.GetCustomers();
    <table  border="1">
          <td>Id</td> <td>Name</td>
<# foreach (Customer c in customers)


          <td> <#= c.CustId #> </td>
          <td> <#= c.CustName #> </td>



Here is the code for creating a sample data class:

public class data
    public data()
    {  }

    public System.Collections.ObjectModel.ObservableCollection<customer> GetCustomers()
        System.Collections.ObjectModel.ObservableCollection<customer> customers =
                new System.Collections.ObjectModel.ObservableCollection<customer>();
        customers.Add(new Customer(1, "John"));
        customers.Add(new Customer(2, "Steve"));
        customers.Add(new Customer(3, "Raj"));
        customers.Add(new Customer(4, "Jagan"));
        customers.Add(new Customer(5, "George"));
        return customers;

public partial class Customer
    public Customer()
    {  }

    public Customer(int custid, string custname)
        this.CustId = custid;
        this.CustName = custname;

    private int custId;
    private string custName;

    public int CustId
        get { return custId; }
        set { custId = value; }

    public string CustName
        get { return custName;  }
        set { custName = value; }

public partial class Customers : 

Running Text Templates

When you run save text template, it will ask whether to run custom tool.

To run Text Template files, any of the following can be done:

  • Save the text template file.
  • Right-Click the text template, and Click Run Custom Tool.
  • Build the Solution. It will run all the text templates defined in the solution.


Likewise, we can generate text files by implementing business logic using T4 Text Templates. It is more powerful when it comes to generating database layer business logic. We can extract information from data model as XML, and implement complex business logic for data layers.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
I have experience in more than 10 years in microsoft technologies. Visit my blog at

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180618.1 | Last Updated 25 Apr 2011
Article Copyright 2011 by arunsuji
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid