Visual Studio macro to switch between ASP.NET markup and code-behind

I recommend

I’ve been using a Visual Studio macro to switch between markup and code-behind for years now. I’ve forgotten where I originally obtained the code for this Visual Studio macro.  If you search around you can find incarnations of it floating around the web. I created a really short tutorial explaining how to setup the macro at the end of this post.

Public Sub Switch()
  Dim FileName As String

  If (DTE.ActiveWindow.Document.FullName.ToLower().EndsWith(".cs")) Then
      ' swith from .aspx.cs to .aspx
      FileName = DTE.ActiveWindow.Document.FullName.ToLower().Replace(".cs", "")
      If System.IO.File.Exists(FileName) Then
          DTE.ItemOperations.OpenFile(FileName)
      End If
  ElseIf (DTE.ActiveWindow.Document.FullName.ToLower().EndsWith(".aspx")) Then
      ' swith from .aspx to .aspx.cs 
      FileName = DTE.ActiveWindow.Document.FullName.ToLower().Replace(".aspx", ".aspx.cs")
      If System.IO.File.Exists(FileName) Then
          DTE.ItemOperations.OpenFile(FileName)
      End If
  ElseIf (DTE.ActiveWindow.Document.FullName.ToLower().EndsWith(".ascx")) Then
      FileName = DTE.ActiveWindow.Document.FullName.ToLower().Replace(".ascx", ".ascx.cs")
      If System.IO.File.Exists(FileName) Then
          DTE.ItemOperations.OpenFile(FileName)
      End If
  ElseIf (DTE.ActiveWindow.Document.FullName.ToLower().EndsWith(".master")) Then
      FileName = DTE.ActiveWindow.Document.FullName.ToLower().Replace(".master", ".master.cs")
      If System.IO.File.Exists(FileName) Then
          DTE.ItemOperations.OpenFile(FileName)
      End If
  End If
End Sub

I created a short tutorial to show how to setup the macro in Visual Studio.

I hope this helps! Enjoy :-D

Technical Visual Studio ASP.NET January 26, 2010


Comments

F7 or Alt+F7, man :)

Belorus - September 3, 2010

Correct me if I'm wrong, but I'm pretty sure the built in F7 only switches from markup to code behind. I just tried it, and it doesn't go from code behind to markup. This macro makes it so you can toggle back and forth very quickly. Of course I might be missing the boat :-)

Nathan Fox - September 3, 2010