简体   繁体   English

Web App中ASP.NET设计中没有服务器控件的代码隐藏字段

[英]No code-behind fields for server controls in ASP.NET design in Web App

I created a New Web Site in Visual Studio 2008 , added a page with a control on it: 我在Visual Studio 2008创建了一个New Web Site ,并添加了一个带有控件的页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form runat="server">
    <div>
        <asp:Label runat="server" ID="lblFoobar">Foobar</asp:Label>
    </div>
</form>
</body>
</html>

and generated code-behind class got a new field lblFoobar . 并且生成的代码隐藏类获得了一个新字段lblFoobar

I can access it into any member as a local instance variable. 我可以将其作为本地实例变量访问任何成员。 All works fine. 一切正常。

But when I created a New ASP.NET Application the same approach throws an error: 但是,当我创建一个New ASP.NET Application ,相同的方法会引发错误:

The name 'lblFoobar' does not exist in the current context

Why? 为什么?

There should be two files attached to your .aspx page. .aspx页上应该有两个文件。 One should be a .aspx.designer.cs file, and the other should be the codebehind file, the .aspx.cs file. 一个应该是.aspx.designer.cs文件,另一个应该是代码隐藏文件.aspx.cs文件。

If you right click the aspx file in the solution explorer and choose "View Codegen File", it should open the .aspx.designer.cs file for you. 如果在解决方案资源管理器中右键单击aspx文件,然后选择“查看Codegen文件”,它将为您打开.aspx.designer.cs文件。 The Label should be declared in there. 标签应在此处声明。

If not, then the designer hasn't correctly parsed your markup yet. 如果不是,那么设计器还没有正确解析您的标记。 Open the .aspx file in design view, make a small change, revert it and delete it to get the designer to run again. 在设计视图中打开.aspx文件,进行一些小的更改,将其还原并删除,以使设计器再次运行。

好的,您的示例代码缺少ASP.NET页面最重要的内容,它是@Page指令,它告诉ASP.NET引擎为该页面实例化哪个类,以及是否需要动态编译任何代码。

Make sure you didn't just copy the entire page from your web site to your web application, but you created a new page. 确保您不仅将整个页面从网站复制到Web应用程序,而且还创建了一个新页面。

Make sure your .aspx page has a correct @page directive, something close to: 确保您的.aspx页具有正确的@page指令,该指令应接近:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM