How do i get rid of __o is not declared?

asp.net

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

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 %>

Solution 2 - asp.net

Yes, I have experienced the same bug occasionally in pages that use server side constructs on ASPX pages.

Overtime, I found a fix for it (I'm sorry, I just haven't been able to find out where I found this bit of info again.) and that fix is to put the following code above the errant <%...%> block:

<%-- For other devs: Do not remove below line. --%>
<%="" %>
<%-- For other devs: Do not remove above line. --%>

Apparently, where you put the above code makes all the difference to VS.NET, so it may take a few tries to get it right.

Solution 3 - asp.net

This is an odd solution, but for me I managed to fix this problem by simply closing the offending open files in Visual Studio.

With them open, i was erratically getting the __o problem.

As soon as I closed them, the __o problem disappeared.

Solution 4 - asp.net

After some hours of googling and analyzing bunch of aspx'ses in my current project seems I've found the solution, that is working for me. Would to advise strongly avoid html-style comments:

<!-- ... -->

inside aspx page. Instead of it use aspx-style comments

<%-- ... --%>

Additionally it helped me obtain that vs intellisense and code highlighting became working again and the mainly thing - this case had begun from it - vs can now hit the breakpoints inside embedded pieces of vb/cs code! And no any damn "This is not a valid location for a breakpoint" message.

Solution 5 - asp.net

When I've cleaned the solution, restarted IIS and it is still mysteriously playing up, I find this can sometimes be caused by pasting an ASPX source file's contents from another system into Visual Studio which "helpfully" updates the code, possibly changing some IDs and breaking the page.

Pasting it into another editor (Notepad++?) then saving it stops Visual Studio from "being helpful" and the page works again.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnno NolanView Question on Stackoverflow
Solution 1 - asp.netPeterView Answer on Stackoverflow
Solution 2 - asp.netCerebrusView Answer on Stackoverflow
Solution 3 - asp.netkolinView Answer on Stackoverflow
Solution 4 - asp.netCodetyperView Answer on Stackoverflow
Solution 5 - asp.netGlennGView Answer on Stackoverflow