Ron
Ron

Reputation: 33

ASP.NET code control is confusing

I found this on a project I'm working on:

 <% If Session("VALUE1") <> "" Then %>
    document.forms[0].action= "<%=Session("VALUE1")%>";
 <% Else %>
    document.forms[0].action="NewPage.aspx"
 <% End If %>

When I single step through this starting at the top line, the code skips over the If Session("VALUE1") but also skips over the Else as well. How is this possible?

Upvotes: 1

Views: 52

Answers (2)

Guffa
Guffa

Reputation: 700342

The code isn't skipped, it's just that you don't see the actual code that is executed.

The code that is generated for that markup when the page is compiled looks something like this:

If Session("VALUE1") <> "" Then
  Response.Write("   document.forms[0].action= """)
  Response.Write(Session("VALUE1"))
  Response.Write(""";")
Else
  Response.Write("   document.forms[0].action=""NewPage.aspx""")
End If

As the Response.Write statements are generated and has no corresponding statements in the source code, it will look like they are skipped when you single step through the code.

Upvotes: 1

Joe Enos
Joe Enos

Reputation: 40393

Inside both the If and Else blocks, there's no actual server code, only markup (which happens to be javascript). Since there's nothing to execute, your debugger doesn't have anything to stop at. So it's not actually skipping both of them.

If you look at the rendered output, one of the two will end up on the page.

Upvotes: 5

Related Questions