0

有一件奇怪的事情。我在视图开始时声明了一些变量,但在标签中它无法访问。我可以在标签之间声明具有相同名称的变量。在这些标签中,原始变量已经可以再次访问。

也许,原因可能是有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();
%>

我不明白为什么,我不能谷歌一些解释。你听得懂么?一些链接?它是如何工作的,部分代码是否单独处理?

不要问为什么要使用这种奇怪的解决方案,我不是代码的作者,我只是对其进行了一些更改。

已编辑:我忘了写它是母版页。

4

1 回答 1

0

在 ASP.NET MVC 上下文中,如果您在任何元素(例如 DIV)上使用runat="server"标记,它将将该代码呈现为已编译页面中的单独方法。因此,任何现有变量都会脱离上下文,而在该标记中声明的变量会在标记之后脱离上下文runat="server"

来源: ASP.NET MVC 中 runat="server" 标记的状态是什么?

于 2012-09-07T13:14:50.090 回答