简体   繁体   English

在C#代码中无法访问designer.cs中定义的ASP控件

[英]ASP controls defined in designer.cs inaccessible in C# codebehind

I have several textboxes in my aspx file (not nested in any other controls), but when referenced in my C# file, I get the error that "The name 'controlname' does not exist in the current context." 我的aspx文件中有几个文本框(未嵌套在任何其他控件中),但是在C#文件中引用时,出现错误“当前上下文中不存在名称'controlname'”。 I have tried making a new project with the same code, converting to web application, and even deleting the designer.cs file just to convert to web app again out of frustration. 我尝试用相同的代码制作一个新项目,转换为Web应用程序,甚至删除designer.cs文件只是为了摆脱沮丧而再次转换为Web应用程序。 The page was working fine until sometime yesterday, but seemingly arbitrarily stopped recognizing the ASP controls, as I didn't change anything other than adding a SQL read, which really shouldn't affect communication between the .aspx file and the .aspx.cs file. 该页面在昨天之前一直工作良好,但是似乎任意停止了ASP控件的识别,因为除了添加SQL读取外,我没有做任何其他更改,这实际上不会影响.aspx文件与.aspx.cs之间的通信文件。 I really appreciate any advice or fixes, as I've lost a good few hours of work trying to figure out how to make these files talk to each other. 我真的很感谢任何建议或修正,因为我已经浪费了很多时间来试图弄清楚如何使这些文件相互通信。

It happens when your .ASPX.CS doesn't find the definition of the control, which is contained at the .DESIGNER file. 当您的.ASPX.CS找不到控件的定义(包含在.DESIGNER文件中)时,就会发生这种情况。 Have you modified or removed these files? 您是否修改或删除了这些文件? Could you post some of your code here? 您可以在此处发布一些代码吗?

And, a simple yet important advice: make sure to have the tag runat='server' in all of your controls. 还有一个简单但重要的建议:确保所有控件中都包含标签runat ='server'。

Regards, Calil 问候,卡里尔

Check that the properties of the .cs and .designer.cs files have their build actions set 'Compile'. 检查.cs和.designer.cs文件的属性是否将其构建操作设置为“编译”。

This can sometimes happen when the project file gets corrupted. 当项目文件损坏时,有时可能会发生这种情况。

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

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