asp.net - 我如何摆脱未声明的 __o ?

标签 asp.net

我的母版页中有一些代码,用于设置包含一些上下文敏感信息的超链接

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

现在问题似乎出在 href 部分。如果我删除动态代码,错误就会消失。谁能告诉我如何解决这个问题?

最佳答案

我找到了 answer在 .net 论坛上。它很好地解释了 ASP.Net 为什么会这样:

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.

另一种解决方案是简单地使用

<% response.write(var) %>

而不是

<%= var %>

关于asp.net - 我如何摆脱未声明的 __o ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/750902/

相关文章:

c# - 通过代码隐藏将 CANONICAL 标签添加到我的页面以进行 SEO?

c# - 在应用程序中添加延迟(用于调试)

javascript - 如何通过 Jquery 或 Javascript 隐藏/显示 div?

c# - Gridview Binding DropDownList 来自后面的代码

html - 如何在不同分辨率下将叠加层保持在屏幕上的相同位置?

jquery - 如何在 ASP.NET 内容表单中使用 JQuery 验证

c# - 在 ASP.NET 网页中打开新选项卡

c# - 将存储过程添加到 DBML 不会生成 *.designer.cs 代码

c# - 我如何打开一个新窗口而不是新选项卡或弹出窗口,而是在 c# 中从代码后面单击按钮时打开一个新窗口

asp.net core - 将异常描述传递给客户端(例如 Angular)