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


Posted on Tuesday, September 30, 2008 by | Comments (13) | 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

Posted on 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 :-)

Posted on 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.

Posted on 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

Posted on 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"?

Posted on 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.

Posted on 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)

Gravatar

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

Thanks for the tip on View.ToggleDesigner. To avoid conflicts I set ctrl-shift-F7 to move between markup and code-behind.

I have View.ToggleDesigner in VS2008 with the V C# 2005 mapping scheme. YMMV

Found this page from:
http://stackoverflow.com/questions/554667/vs-addin-view-markup-does-exists-something-like-that

Posted on 4/23/2010 3:04:30 PM by TonyG #
Gravatar

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

its working, Thank you very much

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

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

I still can't manage to automatically traverse from code behind to the front-end Source instead of the Design view. Is this possible?

Posted on 10/26/2011 8:05:50 AM by Kurt Bugeja #
Gravatar

It is possible. Read comment above.

Gravatar

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

OK, macro works perfectly in VS2010.

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

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

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 7/15/2012 3:26:18 PM by pablokuko #
Gravatar

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

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

Posted on 11/26/2012 2:12:05 PM by mozzo #
Gravatar

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

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.

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

New Comment

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

Categories

Valid HTML5