Click here to Skip to main content
13,868,937 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 25 Oct 2003
Licenced CPOL

ToggleAspNetCodeBehind Macro for Visual Studio .NET

, 25 Oct 2003
Rate this:
Please Sign up or sign in to vote.
A VS.NET macro to toggle between ASP.NET files and the associated Code Behind files.



Visual Studio .NET (VS.NET) is a great development environment. However, no development environment is perfect. I always find things that are missing or could be improved. For instance, when programming in ASP.NET, I am always switching between the .aspx or .ascx and the associated Code Behind file. I found that there is no feature to toggle between these two files, however, all was not lost because the VS.NET team knew that they could not provide features for all users, so they opened up the VS.NET Automation Object Model for us to extend the development environment. First, I searched the Internet to find out if another user had come across problems and had found a solution. I did not find a solution to my problem, but I did find a macro [^] to switch between .cpp (C++) files and .h (C++ header) files. I ended up writing my own macro to switch between ASP.NET files and code behind files.

Macro code

'Description:  Opens the associated file, either aspx, ascx, 
'   asax, or code(behind). If the file does not have an associated
'   file no action is taken.  If the global.asax file is opened in 
'   the designer mode, the designer is closed leaving only the code
'   behind file.
Sub ToggleAspNetCodeBehindFile()
    'Add file extensions here
    Dim langExtension() As String = {".cs", ".vb", ".jsl"}

    Dim activeDoc As String
        activeDoc = LCase(ActiveDocument().FullName)

        If (InStr(activeDoc, ".aspx") Or _
            InStr(activeDoc, ".ascx") Or _
            InStr(activeDoc, ".asmx") Or _
            InStr(activeDoc, ".asax")) Then

            ' Add additional file extensions here
            Dim i As Integer
            Dim isCodeBehind As Boolean = False
            Dim extensionLength As Integer = 0

            For i = 0 To langExtension.Length - 1
                If activeDoc.EndsWith(langExtension(i)) Then
                    isCodeBehind = True
                    extensionLength = langExtension(i).Length
                    Exit For
                End If

            If (isCodeBehind) Then
                OpenAspNetFile(activeDoc, extensionLength)
            End If
            Exit Sub
        End If
        MsgBox("Please select an ASP.NET document to toggle.", _
            MsgBoxStyle.OKOnly, "No ASP.NET Document Selected")
    End Try
End Sub

'Description: Opens an aspx, ascx, asax file
Private Sub OpenAspNetFile(ByVal activeDoc As String, _
    ByVal extensionLength As Integer)

    Dim fileName As String

    Dim projItem As ProjectItem

    ' .asax or .asmx file
    ' Close document if in design mode and open code view 
    If (InStr(activeDoc, ".asax") Or InStr(activeDoc, ".asmx")) Then
        If (InStr(DTE.ActiveWindow.Caption, "design", _
            CompareMethod.Text)) Then

        End If
        projItem = DTE.Solution.FindProjectItem(activeDoc)
        fileName = Left(activeDoc, activeDoc.Length - extensionLength)
            projItem = DTE.Solution.FindProjectItem(fileName)
            MsgBox(ActiveDocument().Name & " is not a valid " _
                & "Code-Behind file.", MsgBoxStyle.OKOnly, _
                "ASP.NET file not found")
        End Try
    End If
End Sub

'Description: Opens the code behind file
Private Sub OpenCodeBehindFile(ByVal activeDoc As String)
    Dim projItem As ProjectItem

    projItem = DTE.Solution.FindProjectItem(activeDoc)
End Sub

How to use

Before you can use the above macro, it must be installed.


Installation is simple. Copy the code above into a module in the Macro IDE and then save the file. You can also use the pre-compiled version available in the download file. Choose "Load Macro Project" from the "Tools/Macros" menu. Open the file AspNetMacros.vsmacros extracted from the downloaded archive file.

Now you can toggle between ASP.NET and code behind files using the Macro Explorer.

This method to use the macro is not very efficient. It is a good thing that Microsoft allows us to assign "Shortcut Keys" to macros.

Assign macro to a shortcut key

To assign a shortcut key, click on the Tools/Options menu choice. In the dialog, open the Environment folder, click on Keyboard. Next, search for the macro in the "Show commands containing:" text box by typing toggleasp. You should then see the macro listed in the big text box below the search box. Click on the ToggleAspNetCodeBehindFile macro. If more than one is listed, make sure "Use new shortcut in:" is set to "Global." Add your shortcut key to the "Press shortcut key(s):" text box. I currently have mine assigned to "Alt-Space."

Make sure the shortcut you enter is not currently assigned, by checking the "Shortcut currently used by:" drop down box. If it is, I would choose another shortcut key. Click "OK" to assign the shortcut key. VS.NET will warn you about not being able to add the shortcut key to the default keyboard mapping scheme if you have not changed the keyboard mappings. If this happens, VS.NET will offer to make a copy. When it does, click "OK" and your shortcut key will be assigned.

To test the shortcut key, open an ASP.NET file and press your shortcut key. The code behind file should open. Press it again and the focus should be set back to the ASP.NET file. It does not matter which file you have opened, the macro will open the associated file.


I hope that you find this macro useful and would not need to uninstall it. However, uninstallation of the macro is easy. If you copied and pasted the code into a macro module, delete the pasted code. If you loaded the macro project, open the macro explorer, click on the macro project to remove. Then, in the Tools/Macros menu, click "Unload Macro Project". If you assigned a shortcut key, it will be removed automatically.

Macro resources

Other useful Visual Studio .NET macros


TypeFinder is a macro that will search for the namespace the type belongs to and insert it before the type or optionally add the Import (VB.NET) or using (C#) statements at the top of your code. The link to TypeFinder [^] did not work the last time I checked, but hopefully it will be working soon.


CollapseAll is a simple macro to collapse all the project nodes in the Solution Explorer. Click here for CollapseAll[^]

Close All Other Tabs

This is a small macro I wrote to add a feature to VS.NET that Andres Aguiar [^] thought should have been included.

Sub CloseAllOtherTabs()
        Dim currentDocument As String = DTE.ActiveDocument.FullName
      MsgBox("Please make sure you have selected a Tab before running the " _
            & "CloseAllOtherTabs macro.", _ 
            MsgBoxStyle.OKOnly, "CloseAllOtherTabs")
    End Try
End Sub

Final thoughts

This macro has filled a gap for me in VS.NET and made me more productive. I hope this macro helps you to be more productive too.


  • 10-26-2003
    • Original article.


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


About the Author

Austin Bain
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralVisual Studio 2008 Pin
Viktar Karpach21-Aug-08 9:06
memberViktar Karpach21-Aug-08 9:06 
GeneralClose Almost All Tabs Pin
jeffknight9-Sep-04 12:04
memberjeffknight9-Sep-04 12:04 
GeneralThere's already a way to toggle.. Pin
Eto26-Oct-03 19:53
memberEto26-Oct-03 19:53 
GeneralRe: There's already a way to toggle.. Pin
Austin Bain26-Oct-03 20:28
memberAustin Bain26-Oct-03 20:28 
GeneralRe: There's already a way to toggle.. Pin
sfigart27-Oct-03 13:17
membersfigart27-Oct-03 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 26 Oct 2003
Article Copyright 2003 by Austin Bain
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid