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

This is strange, but F7 key in Visual Studio switches to a code view, but doesn’t switch back to a markup view. There is no way to switch to markup view unless you open aspx or ascx file directly. Here is a little macro that I found 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
        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
Posted on May 30, 2008 by


Posted on 1/30/2009 12:13:10 AM by Charles Rex


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

Posted on 3/4/2009 06:13:19 AM by Petr

Shift + F7 :-)


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

Posted on 3/11/2009 11:37:51 AM by James

Shift+F7, Shift+F7

Pressing twice will toggle to markup view.

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.

Posted on 5/10/2009 11:35:38 PM by Kate

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

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

Posted on 9/14/2009 06:31:02 PM by Jack

I can find “View.ToggleDesigner”, but I can’t find “View.Toggle”. Can you tell me how to find “View.Toggle”?


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.

Posted on 10/13/2009 09:04:19 AM by David Hammond

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.

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.ViewCode(Settings Designer)

Posted on 10/19/2011 11:26:36 PM by santhosh

its working, Thank you very much

Posted on 10/26/2011 08:05:50 AM by Kurt Bugeja

I still can’t manage to automatically traverse from code behind to the front-end Source instead of the Design view. Is this possible?
It is possible. Read comment above.

Posted on 10/27/2011 12:09:48 AM by Kurt Bugeja

OK, macro works perfectly in VS2010.

Posted on 7/15/2012 03:26:18 PM by pablokuko

Thanks a lot! this was the only solution I found for vs2010…

I made only one addition to also include “.master” extension files too :)

Posted on 11/26/2012 02:12:05 PM by mozzo

Please release this as a VS 2012 addin!! (add master pages and webparts as well)

Posted on 2/6/2014 09:30:40 AM by Edgar Chavez

For those still loking… In VS2012 Just add a shortcut for View.ViewMarkup in Tools>Options then under Environment>Keyboard and for Visual C# 2005 mapping look for the View.ViewMarkup command and assing whatever shortcut you like for Global.