代码隐藏文件无法识别* .ascx中的控件

时间:2021-05-24 15:52:49

I have a QuestionControl.ascx and a QuestionControl.ascx.cs code behind file I copied to a new project. When I build the project any references in the code behind file to controls declared in the ascx gives me this error:

我有一个QuestionControl.ascx和一个QuestionControl.ascx.cs代码隐藏在我复制到新项目的文件后面。当我构建项目时,代码隐藏文件中的任何引用到ascx中声明的控件都会给我这个错误:

'QuestionControl' does not contain a definition for 'rdbtnlstQuestion1' and no extension method 'rdbtnlstQuestion1' accepting a first argument of type 'QuestionControl' could be found (are you missing a using directive or an assembly reference?)

'QuestionControl'不包含'rdbtnlstQuestion1'的定义,并且没有可以找到接受类型'QuestionControl'的第一个参数的扩展方法'rdbtnlstQuestion1'(你是否缺少using指令或汇编引用?)

This is at the top of my *.ascx:

这是我* .ascx的顶部:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="QuestionControl.ascx.cs" Inherits="QuestionControl" %>

I've also tried CodeBehind:

我也尝试过CodeBehind:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="QuestionControl.ascx.cs" Inherits="QuestionControl" %>

This is the top of my class in the codebehind file, it is not contained in a namespace:

这是我在代码隐藏文件中的*,它不包含在命名空间中:

public partial class QuestionControl : System.Web.UI.UserControl
{

5 个解决方案

#1


13  

Try deleting your designer file. VS.NET should recreate it for you when you open the ascx file. I've had problems like this in the past where the designer gets out-of-sync for some reason, and deleting it usually fixes the problem.

尝试删除您的设计器文件。当您打开ascx文件时,VS.NET应该为您重新创建它。我曾经遇到过这样的问题,设计师出于某种原因失去了同步,删除它通常会解决问题。

#2


5  

Another solution is to:

另一个解决方案是:

  • open your .ascx page in design view
  • 在设计视图中打开.ascx页面

  • right click anywhere on the page and select Refresh
  • 右键单击页面上的任意位置,然后选择刷新

(.ascx.designer.cs file may need to to be closed while doing refresh for this to work)

(.ascx.designer.cs文件可能需要在刷新时关闭才能工作)

#3


1  

What worked form me was listed on another SO answer and I can't find it so I'm repeating it here.

在我的另一个SO答案中列出了我的作品,我找不到它,所以我在这里重复一遍。

Try deleting your "ReflectedSchemas" folder in

尝试删除您的“ReflectedSchemas”文件夹

C:\Users\YOURUSENAME\AppData\Roaming\Microsoft\VisualStudio\15.0_079f391b\ReflectedSchemas

This worked for me. Sometimes I get the "unrecognized" errors, and I delete this folder again. Many thanks to the OP. Been driving me crazy for years. Now I have no "squiggles" in the HTML and no "red bars" in the code behind (aspx.cs)

这对我有用。有时我会收到“无法识别”的错误,我会再次删除此文件夹。非常感谢OP。一直让我疯狂多年。现在我在HTML中没有“曲线”,后面的代码中没有“红色条”(aspx.cs)

#4


0  

I had problems for example creating a dropdownlist inside a gridview. What I did is creating the ddl outside of the gv until the desinger.cs recognized it and afterwards moved the control inside the gv... hope this helps

我有问题,例如在gridview中创建一个下拉列表。我做的是在gv之外创建ddl,直到desinger.cs识别出来然后将控件移到gv中...希望这有助于

#5


0  

I was having the same issue, the code-behind didn't recognize the controls on the .aspx page. I'm using VS 2012. I right-clicked the project; clicked on Convert to Web Application; and it added all the designer files that weren't there at all before. I rebuilt everything and it's good now.

我遇到了同样的问题,代码隐藏无法识别.aspx页面上的控件。我正在使用VS 2012.我右键单击了该项目;点击转换为Web应用程序;并且它添加了之前根本不存在的所有设计器文件。我重建了一切,现在很好。

#1


13  

Try deleting your designer file. VS.NET should recreate it for you when you open the ascx file. I've had problems like this in the past where the designer gets out-of-sync for some reason, and deleting it usually fixes the problem.

尝试删除您的设计器文件。当您打开ascx文件时,VS.NET应该为您重新创建它。我曾经遇到过这样的问题,设计师出于某种原因失去了同步,删除它通常会解决问题。

#2


5  

Another solution is to:

另一个解决方案是:

  • open your .ascx page in design view
  • 在设计视图中打开.ascx页面

  • right click anywhere on the page and select Refresh
  • 右键单击页面上的任意位置,然后选择刷新

(.ascx.designer.cs file may need to to be closed while doing refresh for this to work)

(.ascx.designer.cs文件可能需要在刷新时关闭才能工作)

#3


1  

What worked form me was listed on another SO answer and I can't find it so I'm repeating it here.

在我的另一个SO答案中列出了我的作品,我找不到它,所以我在这里重复一遍。

Try deleting your "ReflectedSchemas" folder in

尝试删除您的“ReflectedSchemas”文件夹

C:\Users\YOURUSENAME\AppData\Roaming\Microsoft\VisualStudio\15.0_079f391b\ReflectedSchemas

This worked for me. Sometimes I get the "unrecognized" errors, and I delete this folder again. Many thanks to the OP. Been driving me crazy for years. Now I have no "squiggles" in the HTML and no "red bars" in the code behind (aspx.cs)

这对我有用。有时我会收到“无法识别”的错误,我会再次删除此文件夹。非常感谢OP。一直让我疯狂多年。现在我在HTML中没有“曲线”,后面的代码中没有“红色条”(aspx.cs)

#4


0  

I had problems for example creating a dropdownlist inside a gridview. What I did is creating the ddl outside of the gv until the desinger.cs recognized it and afterwards moved the control inside the gv... hope this helps

我有问题,例如在gridview中创建一个下拉列表。我做的是在gv之外创建ddl,直到desinger.cs识别出来然后将控件移到gv中...希望这有助于

#5


0  

I was having the same issue, the code-behind didn't recognize the controls on the .aspx page. I'm using VS 2012. I right-clicked the project; clicked on Convert to Web Application; and it added all the designer files that weren't there at all before. I rebuilt everything and it's good now.

我遇到了同样的问题,代码隐藏无法识别.aspx页面上的控件。我正在使用VS 2012.我右键单击了该项目;点击转换为Web应用程序;并且它添加了之前根本不存在的所有设计器文件。我重建了一切,现在很好。