有一件事很奇怪。我在视图开始时声明了一些变量,但在标签中它是不可访问的。我可以在标记之间声明名称相同的变量。在标记之外,可以再次访问原始变量。
也许,原因可能是有runatserver。代码如下所示:
<% var variable = something; %>
<head id="Head1" runat="server">
<%!-- <% variable = something else; %> --%> // impossible variable don't exist in this context
<% var variable = something else %> // so this row instead of last one
...
</head>
<% if (variable == something)
ThisFunctionWillBeExecute();
if (variable == somethigelse)
ThisFunctionWillNotBeExecuted();
%>我不明白为什么,我也不能在谷歌上搜索一些解释。你明白吗?一些链接?它是如何工作的,部分代码是单独处理的吗?
不要问我为什么要使用这个奇怪的解决方案,我不是代码的作者,我只是做了一些修改。
编辑:我忘记写它是母版页了。
发布于 2012-09-07 21:14:50
在DIV上下文中,如果您在任何元素上使用runat="server"标记,例如ASP.NET,它将在编译页面中将代码呈现为单独的方法。因此,任何现有的变量都会脱离上下文,而在该标记中声明的变量在runat="server"标记之后也会脱离上下文。
来源:What is the status of runat="server" tags in ASP.NET MVC?
https://stackoverflow.com/questions/12317556
复制相似问题