Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get rid of __o is not declared?

Tags:

asp.net

I have some code in my master page that sets up a a hyperlink with some context sensitive information

<%If Not IsNothing(Profile.ClientID) Then%> <span class="menu-nav">  <a  target="_blank"      href= "http://b/x.aspx?ClientID=<%=Profile.ClientID.ToString()%>&Initials=<%=Session("Initials")%>"            >     Send     <br />     SMS     <br /> </a>  </span> <%End If %>  <span class="menu-nav"> <!-- Name __o is not declared Error is flagged here--> 

Now the issue seems to be in the href part. If I remove the dynamic code the error disappears. Can anyone tell me how to resolve this issue?

like image 260
Johnno Nolan Avatar asked Apr 15 '09 09:04

Johnno Nolan


1 Answers

I've found the answer on the .net forums. It contains a good explanation of why ASP.Net is acting the way it is:

We have finally obtained reliable repro and identified the underlying issue. A trivial repro looks like this:

  <% if (true) { %>   <%=1%>   <% } %>   <%=2%>    

In order to provide intellisense in <%= %> blocks at design time, ASP.NET generates assignment to a temporary __o variable and language (VB or C#) then provide the intellisense for the variable. That is done when page compiler sees the first <%= ... %> block. But here, the block is inside the if, so after the if closes, the variable goes out of scope. We end up generating something like this:

   if (true) {          object @__o;         @__o = 1;    }    @__o = 2; 

The workaround is to add a dummy expression early in the page. E.g. <%="" %>. This will not render anything, and it will make sure that __o is declared top level in the Render method, before any potential ‘if’ (or other scoping) statement.

An alternative solution is to simply use

<% response.write(var) %> 

instead of

<%= var %> 
like image 167
Peter Avatar answered Sep 22 '22 15:09

Peter