In my last post I have blogged how to make single page Item Template to ease your day to day work. Now I am going to extend a bit to show you how to build multi page template. You are using multi page templates in your work without even noticing it.
Drop a .aspx page into your Web Application and there you go you just used one. In general you r creating 2 files separately but the way you name the files and set certain attributes in the files makes VS to visualize properly. And let’s get hands on …
Our task is to prepare the aspx template for test bed pages creation that have all the attributes we need in the page in order not to implement it every time. This template will consist of three files aspx page itself(TemplatePage.aspx), the codebehind file (TemplateTest.cs) , the designer file (TemplatePage.aspx.designer.cs) and one file that will actually tell Visual Studio what to do (MyTemplate.vstemplate).
In the files there will be some strange looking tags like $safeitemname$.aspx.cs, which the name of the file that will be give at actual file creation time. For more information on this parameters see the links section below.
Let’s take a look at the files and start with .aspx page template file.
Actually the designer file created by VS with the regular template for aspx page contains comment on top that it is auto generated file etc. This comment will be generated for this file as well at the moment when you do not have any mistakes in aspx page source and press CTRL+K, CTRL+D which is document outline shortcut. Then this file is regenerated from VS.
And last come the template file. Marked text is text you might need to change.
You can see now there are three files for VS to create. It is important to set the attributes (marked above in all files) in the files so that VS knows this is all about one and the same aspx page.
How to: Create Multi page Item Templates for Visual Studio
In my last post I have blogged how to make single page Item Template to ease your day to day work. Now I am going to extend a bit to show you how to build multi page template. You are using multi page templates in your work without even noticing it.
Drop a .aspx page into your Web Application and there you go you just used one. In general you r creating 2 files separately but the way you name the files and set certain attributes in the files makes VS to visualize properly. And let’s get hands on …
Our task is to prepare the aspx template for test bed pages creation that have all the attributes we need in the page in order not to implement it every time. This template will consist of three files aspx page itself(TemplatePage.aspx), the codebehind file (TemplateTest.cs) , the designer file (TemplatePage.aspx.designer.cs) and one file that will actually tell Visual Studio what to do (MyTemplate.vstemplate).
In the files there will be some strange looking tags like $safeitemname$.aspx.cs, which the name of the file that will be give at actual file creation time. For more information on this parameters see the links section below.
Let’s take a look at the files and start with .aspx page template file.
<%@
Page
Language=”C#”
AutoEventWireup=”true”
CodeFile=”$safeitemname$.aspx.cs“
Inherits=”$rootnamespace$.$safeitemname$“
%>
<!DOCTYPE
html
PUBLIC
“-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html
xmlns=”http://www.w3.org/1999/xhtml”>
<head
runat=”server”>
<title>Untitled Page</title>
</head>
<body>
<a
href=”javascript:history.back();”>Back</a>
<form
id=”form1″
runat=”server”>
<div>
<!—Place your template content here –>
</div>
</form>
Created by $userdomain$\$username$ on $machinename$ at $time$.
CLR Version $clrversion$.
</body>
</html>
Note the yellow marked text. Those parameter will be replaced upon file creation with the corresponding values.
Let’s see Code Behind file
//using Section omited
namespace
$rootnamespace$
{
public
partial
class
$safeitemname$ : System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
}
}
}
… the designer file …
namespace
$rootnamespace$ {
public
partial
class
$safeitemname$ {
///
<summary>
/// form1 control.
///
</summary>
///
<remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
///
</remarks>
protected
global::System.Web.UI.HtmlControls.HtmlForm form1;
}
}
Actually the designer file created by VS with the regular template for aspx page contains comment on top that it is auto generated file etc. This comment will be generated for this file as well at the moment when you do not have any mistakes in aspx page source and press CTRL+K, CTRL+D which is document outline shortcut. Then this file is regenerated from VS.
And last come the template file. Marked text is text you might need to change.
<VSTemplate
Version=“2.0.0“
xmlns=“http://schemas.microsoft.com/developer/vstemplate/2005“
Type=“Item“>
<TemplateData>
<DefaultName>TestBedForm.aspx</DefaultName>
<Name>TestBed Template</Name>
<Description>Visual Studio Team Test C# TestBed Template.</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>10</SortOrder>
<Icon>__TemplateIcon.ico</Icon>
<TemplateGroupID>TestProject-V1</TemplateGroupID>
</TemplateData>
<TemplateContent>
<ProjectItem
TargetFileName=“$fileinputname$.aspx“
ReplaceParameters=“true“>TemplatePage.aspx</ProjectItem>
<ProjectItem
TargetFileName=“$fileinputname$.aspx.cs“
ReplaceParameters=“true“>TemplateTest.cs</ProjectItem>
<ProjectItem
TargetFileName=“$fileinputname$.aspx.designer.cs“
ReplaceParameters=“true“>TemplatePage.aspx.designer.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
You can see now there are three files for VS to create. It is important to set the attributes (marked above in all files) in the files so that VS knows this is all about one and the same aspx page.
Happy coding
Links
http://msdn2.microsoft.com/en-us/library/ms247115(VS.80).aspx – Multi page Item template creation page on microsoft.com
http://msdn2.microsoft.com/en-us/library/eehb4faa(VS.80).aspx – Template parameters you can use.