Macro to add a Codebehind file to an ASPX page in VS2005

The best way to add a code file (a.k.a. codebehind) to an ASP.NET ASPX page is check the "Place code in separate file" checkbox when you create it:

However, as Fritz Onion wrote, it's nice to avoid creating unnecessary codebehind files "just in case", because the with advanced web controls in ASP.NET 2.0, it's possible that many of your pages won't need any additional code.

The problem: If you're working with a page wasn't created with a codebehind file, there's no "right-click / add code file" option. Mikhail Arkhipov wrote some basic directions on how to do this manually, and Fritz's post goes into a little more detail. Fritz asked for a plugin to add code files to an ASPX page, but I'll settle for a Visual Studio macro. This thing's pretty simple - just select the ASPX file in the solution explorer, then run the macro. There's some error handling, but I'd make sure you save your work before running this.

I've started playing with upgrading it to a Visual Studio Addin (using the MakeAddin macro), but it's not working yet.

Sub AddCodeBehind() Dim docName Dim docFullName Dim nameOfType Try DTE.ExecuteCommand("View.ViewCode") docName = DTE.ActiveDocument.Name If (docName.ToString().EndsWith(".aspx") = False) Then Throw New System.Exception() End If Catch ex As Exception Throw New System.Exception("You must select an ASPX file in the Solution Explorer before running this macro.") End Try nameOfType = Replace(docName, ".aspx", "") docFullName = DTE.ActiveDocument.FullName DTE.ExecuteCommand("Edit.Replace") DTE.Find.ReplaceWith = "<%@ Page Language=""C#"" CodeFile=""" + nameOfType + ".aspx.cs"" Inherits=""" + nameOfType + """" DTE.Windows.Item("" + nameOfType + ".aspx").Activate() DTE.Find.FindWhat = "<%@ Page Language=""VB""" DTE.Find.ReplaceWith = "<%@ Page Language=""C#"" CodeFile=""" + nameOfType + ".aspx.cs"" Inherits=""" + nameOfType + """" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = False DTE.Find.MatchWholeWord = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone DTE.Find.Action = vsFindAction.vsFindActionReplaceAll If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then DTE.Find.FindWhat = "<%@ Page Language=""C#""" DTE.Find.Action = vsFindAction.vsFindActionReplaceAll End If '{CF2DDC32-8CAD-11D2-9302-005345000000} is the GUID for the Find / Replace Dialog DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close() DTE.ActiveDocument.Save() DTE.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes) DTE.ItemOperations.AddNewItem("Web Developer Project Files\Visual C#\Class", docName + ".cs") DTE.ExecuteCommand("Edit.Replace") DTE.Windows.Item(nameOfType + ".aspx.cs").Activate() DTE.Find.FindWhat = "public class " + nameOfType DTE.Find.ReplaceWith = "public partial class " + nameOfType + " : Page" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = False DTE.Find.MatchWholeWord = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone DTE.Find.Action = vsFindAction.vsFindActionReplaceAll If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then Throw New System.Exception("vsFindResultNotFound") End If '{CF2DDC32-8CAD-11D2-9302-005345000000} is the GUID for the Find / Replace Dialog in case you missed that earlier comment DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close() DTE.ActiveDocument.Save() DTE.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes) DTE.ItemOperations.OpenFile(docName) DTE.ItemOperations.OpenFile(docFullName + ".cs") End Sub
Published Thursday, November 30, 2006 4:47 PM by Jon Galloway
Filed under:

Comments

# re: Macro to add a Codebehind file to an ASPX page in VS2005

I have always just excluded the file, then included it back in. If there's no code behind file, it asks if you want to create one.

Friday, December 01, 2006 1:10 PM by Ted Jardine

# re: Macro to add a Codebehind file to an ASPX page in VS2005

Nice, Ted. I'd forgotten about that trick. I'll keep quiet about that partly finished VS.NET Addin to do that, then...

Sunday, December 03, 2006 2:19 AM by Jon Galloway

# re: Macro to add a Codebehind file to an ASPX page in VS2005

By the way, that exclude / include trick only seems to work with Web Application Projects, so I'm keeping this macro handy.

Wednesday, December 06, 2006 6:07 PM by Jon Galloway

# re: Macro to add a Codebehind file to an ASPX page in VS2005

Sunday, August 05, 2007 1:25 AM by Walker

# re: Macro to add a Codebehind file to an ASPX page in VS2005

Nice

Tuesday, November 27, 2007 6:48 PM by Ivan

# re: Macro to add a Codebehind file to an ASPX page in VS2005

Nice

Wednesday, November 28, 2007 3:09 AM by Ivan

# re: Macro to add a Codebehind file to an ASPX page in VS2005

Nice

Thursday, November 29, 2007 8:40 AM by Ivan

# re: Macro to add a Codebehind file to an ASPX page in VS2005

I stumbled upon your macro which was just what I was looking for. I did find a problem though. I am using Visual Studio 2008 so it may be limited to that but if you try to run the macro on a file in a folder it doesn't work as the DTE.ActiveDocument.FullName doesn't contain the path info. For instance if i have a folder called public the fullname must be prefixed by path/. Hope I explained that ok, I looked for a way to fix it and post it but couldn't find a method to get the server type relative path.

Thursday, May 15, 2008 10:36 PM by petebob796

Leave a Comment

(required) 
(required) 
(optional)
(required)