Click here to Skip to main content
15,886,362 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.5K   443   17  
A VS.NET macro to toggle between ASP.NET files and the associated Code Behind files.
'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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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