aspx页面中的变量范围 [英] Variable scope in aspx page

查看:40
本文介绍了aspx页面中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么代码底部的 scriptPath 变量超出范围?

Why is that the scriptPath variable is out of scope in the bottom of the code?

它不应该在整个页面范围内吗?在 MVC 中,如果我将其标记在页面顶部,例如

Shouldn't it be in scope throughout this page? In MVC, if I mark this on top of the page like

@{
    string scriptPath = "assets/scripts/",
           gkoConfig = "GkoConfig.js";
}

它在当前视图中可用.回到 WebForms 一段时间后,我错过了什么?

it is available throughout the current View. What am I missing now that I'm back to WebForms for a while?

如果我更改代码位置,它会变得更奇怪,因为在 中我不再可以访问变量,但是在 中我确实可以访问. 现在... :-/

If I change the code position, It get's weirder as inside the <head> I no longer have access to teh variable, but I do have, inside the <body> now... :-/

推荐答案

当您在 Web 窗体 .aspx 文件中声明一个变量时,您实际上是在自动生成的呈现方法中声明了一个局部变量.ASP.NET 为所有标记为 runat="server" 的标记生成单独的呈现方法,因此您实际上为 head 元素获得了单独的方法.现在,您声明的变量只能存在于这些方法之一中——因此是奇怪"的行为.

When you declare a variable in a Web Forms .aspx file, you’re actually declaring a local variable inside an auto-generated rendering method. ASP.NET generates separate rendering methods for all tags marked runat="server", so you actually get a separate method for your head element. Now, the variable you declare can only exist in one of these methods - hence the 'weird' behavior.

如果您预编译您的应用程序,您可以看到它是如何工作的使用 aspnet_compiler.exe. 您将获得为每个 Web 表单页面编译的 DLL 文件;只需在 Reflector 中打开其中一个即可查看生成的代码.我用在 head 标签外声明的变量编写了一个与您的代码等效的最小代码,这是我得到的顶级渲染方法:

You can see how this works if you pre-compile your application using aspnet_compiler.exe. You will get compiled DLL files for each of your web forms pages; just open one of those up in Reflector to see the generated code. I wrote a minimal equivalent of your code with the variable declared outside the head tag, and here’s the top-level render method that I got:

private void __Render__control1(HtmlTextWriter __w, Control parameterContainer)
{
    string str = "scripts/";
    __w.Write("\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
    parameterContainer.Controls[0].RenderControl(__w);
    __w.Write("\r\n<body>\r\n    ");
    parameterContainer.Controls[1].RenderControl(__w);
    __w.Write("\r\n    <script type=\"text/javascript\" src=\"");
    __w.Write(str);
    __w.Write("jquery-1.4.1.min.js\"></script>\r\n</body>\r\n</html>\r\n");
}

您会看到我声明的变量(此处名为 str)的范围限定于此方法,并且它正在调用其他方法来呈现头部(以及标记为 runat="服务器".)

You see that the variable that I declared (here named str) is scoped to this method, and it's calling other methods to render the head (and a form element marked runat="server".)

一个快速而肮脏的解决方案可能是简单地从 head 标签中删除 runat="server";但是,我建议您为此在代码隐藏类中声明一个受保护的变量.将这样的一行添加到您的代码隐藏文件中会起作用:

A quick and dirty solution might be to simply remove the runat="server" from your head tag; however, I’d recommend that you declare a protected variable in your code-behind class for this. Adding a line like this to your code-behind file would work:

protected string scriptPath, gkoConfig;

然后,您可以在 Web 窗体代码中的任何位置使用这些变量.

You can then use these variables anywhere in your Web Forms code.

这篇关于aspx页面中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆