How to switch between view source and markup view in Visual Studio?

This is strange, but F7 key in Visual Studio switches to code view, but doesn't switch back to markup view. There is no way to switch to markup view unless you open aspx or ascx file directly. This little macros was found by me on web. I had to fix it a little bit, so it works in Visual Studio 2008.

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.Document.DTE.ExecuteCommand("View.ViewCode")

 

End Sub


Tuesday, September 30, 2008 | Comments (6) | Add Comment

Comments

Gravatar

Re:How to switch between view source and markup view in Visual Studio?

Hello,

This code works wonderful on Visual Studio 2005 SP1 as well.

- I've created a macro called: Public Module ToggleASPXSourceCode

that contains the three subroutines

- Then by using the Tools 'menu option'-> 'Customize' menu option -> 'Keyboard' button, I've reassigned the F7 key to point to the macro: MyMacros.ToggleASPXSourceCode.ToggleAspNetCodeBehindFile

1/30/2009 12:13:10 AM | by Charles Rex
Gravatar

Re:How to switch between view source and markup view in Visual Studio?

Shift + F7 :-)

3/4/2009 6:13:19 AM | by Petr
Gravatar

Petr,
Shift + F7 switches markup view <-> html code view. In my post I am talking about code behind view and html code view.

Gravatar

Re:How to switch between view source and markup view in Visual Studio?

Shift+F7, Shift+F7
Pressing twice will toggle to markup view.

3/11/2009 11:37:51 AM | by James
Gravatar

Even if you press Shift+F7 once then it will toggle to markup view. However there are no way to go to code behind view.

Gravatar

Re:How to switch between view source and markup view in Visual Studio?

Tools> Options> Keyboard

Search for "View.Toggle" in "Show Commands Containing".

Add new shortcut to "Global" with F7.

This works without needing a macro for VS2008

5/10/2009 11:35:38 PM | by Kate
Gravatar

Hmmm, this is working. Thank you for a tip.

Gravatar

Re:How to switch between view source and markup view in Visual Studio?

I can find "View.ToggleDesigner", but I can't find "View.Toggle". Can you tell me how to find "View.Toggle"?

9/14/2009 6:31:02 PM | by Jack
Gravatar

Hi,

This is really strange. It was there and now I can't find it any more. My F7 shortcut is set to View.ToggleDesigner and Visual Studio does switch between different views. Try to set it to View.ToggleDesigner. See if it works.

Gravatar

Re:How to switch between view source and markup view in Visual Studio?

Thanks for the macro. I also do not see "View.Toggle" as an option (maybe it was removed with SP1?), but the macro does the job nicely. Crazy that this capability is not built in.

10/13/2009 9:04:19 AM | by David Hammond
Gravatar

I saw it after SP1. It is strange, but it is still working for me. When I press F7 it switches between code and codebehind. Visual Studio shows that keyboard shortcut for F7 is tied to:
View.ToggleDesigner(Global)
View.ViewCode(Settings Designer)

New Comment

Your Name:
Email (for internal use only):
Subject:
Comment:
 
Code above: