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