Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

ToggleAspNetCodeBehind Macro for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
25 Oct 2003CPOL4 min read 73.4K   443   17   5
A VS.NET macro to toggle between ASP.NET files and the associated Code Behind files.

Contents

Introduction

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

VB
'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
    Try
        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
            Next

            If (isCodeBehind) Then
                OpenAspNetFile(activeDoc, extensionLength)
            Else
                OpenCodeBehindFile(activeDoc)
            End If
            Exit Sub
        End If
    Catch
        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

            ActiveDocument.Close(vsSaveChanges.vsSaveChangesPrompt)
        End If
        projItem = DTE.Solution.FindProjectItem(activeDoc)
        projItem.Open(Constants.vsViewKindCode).Activate()
    Else
        fileName = Left(activeDoc, activeDoc.Length - extensionLength)
        Try
            projItem = DTE.Solution.FindProjectItem(fileName)
            projItem.Open(Constants.vsViewKindTextView).Activate()
        Catch
            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)
    projItem.Open(Constants.vsViewKindCode)
End Sub

How to use

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

Installation

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.

Image 1

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

Image 2

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."

Image 3

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.

Image 4

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.

Uninstall

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

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

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.

VB.NET
Sub CloseAllOtherTabs()
    Try
        Dim currentDocument As String = DTE.ActiveDocument.FullName
        DTE.Documents.CloseAll()
        DTE.ItemOperations.OpenFile(currentDocument)
    Catch
      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.

History

  • 10-26-2003
    • Original article.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralVisual Studio 2008 Pin
Viktar Karpach21-Aug-08 8:06
Viktar Karpach21-Aug-08 8:06 
GeneralClose Almost All Tabs Pin
jeffknight9-Sep-04 11:04
jeffknight9-Sep-04 11:04 
GeneralThere's already a way to toggle.. Pin
Eto26-Oct-03 18:53
Eto26-Oct-03 18:53 
GeneralRe: There's already a way to toggle.. Pin
Austin Bain26-Oct-03 19:28
Austin Bain26-Oct-03 19:28 
GeneralRe: There's already a way to toggle.. Pin
sfigart27-Oct-03 12:17
sfigart27-Oct-03 12: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.