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

61 Comments

Comments have been disabled for this content.