简体   繁体   English

Visual Studio 2008中的代码隐藏不能引用ASP.NET控件

[英]ASP.NET controls cannot be referenced in code-behind in Visual Studio 2008

Ok, so, my visual studio is broken. 好吧,我的视觉工作室坏了。 I say this NOT prematurely, as it was my first response to see where I had messed up in my code. 我说这不是过早的,因为这是我第一次看到我在代码中搞砸了。 When I add controls to the page I can't reference all of them in the code behind. 当我向页面添加控件时,我无法在后面的代码中引用它们。 Some of them I can, it seems that the first few I put on a page work, then it just stops. 其中一些我可以,似乎我在页面上工作的前几个,然后它就停止了。

I first thought it may be the type of control as initially I was trying to reference a repeater inside an update panel. 我首先想到它可能是控件的类型,因为最初我试图在更新面板中引用转发器。 I know I am correctly referencing the code behind in my aspx page. 我知道我正在引用我的aspx页面中的代码。 But just in case it was a screw up on my part I started to recreate the page from scratch and this time got a few more controls down before VS stopped recognizing my controls. 但是,以防万一我自己搞砸了,我开始从头开始重新创建页面,这次在VS停止识别我的控件之前还有一些控件。

After creating my page twice and getting stuck I thought maybe it was still the type of controls. 在创建我的页面两次并卡住后,我想也许它仍然是控件的类型。 I created a new page and just threw some labels on it. 我创建了一个新页面,只是在上面扔了一些标签。 No dice, build fails when referencing the control from the code behind. 没有骰子,从后面的代码引用控件时构建失败。

In a possibly unrelated note when I switch to the dreaded "design" mode of the aspx pages VS 2008 errors out and restarts. 在一个可能不相关的注释中,当我切换到aspx页面VS 2008的可怕“设计”模式时,错误输出并重新启动。

I have already put a trouble ticket in to Microsoft. 我已经把故障单带到了微软。 I uninstalled all add-ins, I reinstalled visual studio. 我卸载了所有加载项,我重新安装了visual studio。

Anyone that wants to see my code just ask, but I am using the straight WYSIWYG visual studio "new aspx page" nothing fancy. 任何想要看到我的代码的人都会问,但我使用的是直接的WYSIWYG视觉工作室“新的aspx页面”没什么特别的。

I doubt anyone has run into this, but have you? 我怀疑是否有人碰到这个,但是你呢?

Has anyone had success trouble shooting these things with Microsoft? 有没有人成功解决与微软拍摄这些事情的问题? Any way to expedite this ticket without paying??? 任何方式加快这张票而不付钱??? I have been talking to a rep from Microsoft for days with no luck yet and I am dead in the water. 我一直在与微软的一位代表谈论几天没有运气,我已经死在水中。


Jon Limjap: I edited the title to both make it clear and descriptive and make sure that nobody sees it as offensive. Jon Limjap:我编辑了标题,既清晰又具有描述性并确保没有人认为它具有攻击性。 "Foo-barred" doesn't exactly constitute a proper question title, although your question is clearly a valid one. “Foo-barred”并不完全构成一个正确的问题标题,尽管你的问题显然是有效的。

The above fix (deleting the temp files) did not work for me. 上面的修复(删除临时文件)对我不起作用。 I had to delete the PageName.aspx.designer.cs file, then right-click my page, and choose "Convert to Web Application" from the context menu. 我不得不删除PageName.aspx.designer.cs文件,然后右键单击我的页面,并从上下文菜单中选择“转换为Web应用程序”。

When Visual Studio attempted to rebuild the designer file, it encountered (and revealed to me) the source of the problem. 当Visual Studio尝试重建设计器文件时,它遇到(并向我透露)问题的根源。 In my case, VS had lost a reference to a DLL needed by one of the controls on my page, so I had to clean out the generated bin folders in my project. 在我的情况下,VS丢失了对我的页面上的一个控件所需的DLL的引用,所以我不得不清理项目中生成的bin文件夹。

try clearing your local VS cache. 尝试清除本地VS缓存。 find your project and delete the folder. 找到您的项目并删除该文件夹。 the folder is created by VS for what reason I honestly don't understand. 该文件夹是由VS创建的,原因是我老实说不明白。 but I've had several occasions where clearing it and doing a re-build fixes things... hope this is all that you need as well. 但是我已经有好几次清理它并重新构建东西......希望这也是你所需要的。

here 这里

%Temp%\VWDWebCache

and possibly here 可能在这里

%LocalAppData%\Microsoft\WebsiteCache

Is the control that you are trying to reference inside of the repeater? 您试图在转发器内部引用控件吗?

If so then you need to look them up using the FindControl method. 如果是这样,那么您需要使用FindControl方法查找它们。

For example for: 例如:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server">stest</asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>

You would need to do this to reference it: 你需要这样做来引用它:

LinkButton lb = Repeater1.FindControl("LinkButton1");

In my case, I was working with some old web site code, which I converted to a VS2008 solution. 在我的情况下,我正在使用一些旧的网站代码,我将其转换为VS2008解决方案。 I encountered this same problem. 我遇到了同样的问题。

For me, the fix was to right-click the Web Sites project in the Solution Explorer and select Convert to Web Application. 对我来说,修复是在解决方案资源管理器中右键单击“网站”项目,然后选择“转换为Web应用程序”。 This created designer.cs files for all pages, which did not yet have these files before. 这为所有页面创建了designer.cs文件,之前还没有这些文件。

您还可以在这里找到可以安全删除的.net临时文件:C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ Temporary ASP.NET Files

I observed this happens because of missing .designer.cs file. 我发现这是因为缺少.designer.cs文件。 Following fixed this issue in my case (basically I had these files copied from VS 2005 web project to VS 2010 project): Right Click on .aspx file and select menu "Convert to Web site", this will create .designer.cs file and then it should work file. 在我的情况下修复了这个问题(基本上我将这些文件从VS 2005 web项目复制到VS 2010项目):右键单击.aspx文件并选择菜单“转换为Web站点”,这将创建.designer.cs文件和那么它应该工作文件。

In my case new asp controls I added to an existing were not being detected. 在我的情况下,我添加到现有的新asp控件没有被检测到。

What worked for me was forcing a recompile by renaming an existing control to break the build eg: changing <asp:TextBox ID="txtTitle" runat="server" /> to <asp:TextBox ID="txtTitle2" runat="server" /> 对我<asp:TextBox ID="txtTitle" runat="server" />是通过重命名现有控件来破坏构建来强制重新编译,例如:将<asp:TextBox ID="txtTitle" runat="server" />更改为<asp:TextBox ID="txtTitle2" runat="server" />

When I corrected the ID and rebuilt a new designer file was generated with the corrected ID and new controls. 当我更正ID并重建时,使用更正后的ID和新控件生成了新的设计器文件。

我们无法在应用程序运行时更改代码。为此,首先单击顶部的停止按钮,这将停止您的应用程序。现在在设计模式下单击按钮,它将在.aspx.cs文件中插入代码,然后在其中编写代码

一旦我遇到这个问题,请查看有关错误的aspx文件

I had this happen a few times and it happened again today for a new reason. 我发生了几次这种情况,今天又出现了一个新的原因。 I normally run my project through IIS but needed to run it locally to debug. 我通常通过IIS运行我的项目,但需要在本地运行它来调试。 I normally run on port 80 in IIS and 81 in debug, but I had some settings in the web.config that used 80 so I just killed the site in IIS and switched the website to port 80 in the project settings. 我通常在IIS中的端口80上运行,在调试中运行81,但我在web.config中有一些使用80的设置,所以我在IIS中杀死了网站,并在项目设置中将网站切换到端口80。 For whatever reason, this messed everything up and created the problem described in the OP. 无论出于何种原因,这都搞砸了所有事情并创造了OP中描述的问题。 I started trying things one by one, including all the advice mentioned here, but switching the port back to 81 in the project settings is what ended up working. 我开始逐个尝试,包括这里提到的所有建议,但在项目设置中将端口切换回81是最终工作。

right click on project name and select "Clean". 右键单击项目名称,然后选择“清除”。 then, check your bin folder if it has any dll remaining. 然后,检查你的bin文件夹是否还有dll。 if so, delete it. 如果是这样,删除它。 that´s it. 而已。 just rebuild and every thing will work fine. 只是重建,每件事都会很好。

Just to add my two cents with this problem. 只是为了增加我的两分钱这个问题。

The only thing from all the above that worked for me was the "Clean" and then delete anything left in the bin folder. 以上所有对我有用的唯一事情就是“清理”,然后删除bin文件夹中剩下的任何内容。 Rebuild and all controls started working then. 然后重建和所有控件开始工作。

FYI...I was having this problem too and I ended up fixing it by deleting the existing .designer.vb file, right-clicking on the project and choosing Convert to Web Application. 仅供参考我...我也遇到了这个问题,最后通过删除现有的.designer.vb文件,右键单击项目并选择转换为Web应用程序来修复它。 It then showed the real "error" that was causing the GUI to crap itself. 然后它显示了导致GUI自行崩溃的真正“错误”。 Turned out I had used the same name for 2 other labels but that wasn't being shown in the error list window. 原来我使用了其他2个标签的相同名称,但没有在错误列表窗口中显示。 Once I renamed one of the 2 other labels it built fine and stopped giving me trouble. 一旦我重新命名了其他2个标签中的一个,它就构建得很好并且不再给我带来麻烦。

This can also happen if the Inherits property on the source page doesn't match the class name in the code behind. 如果源页面上的Inherits属性与后面的代码中的类名称不匹配,也会发生这种情况。 Generally speaking, this would probably only happen if you copy/pasted a .ascx/.aspx file and forgot to update it. 一般来说,这可能只会在您复制/粘贴.ascx / .aspx文件并忘记更新时发生。

Example: 例:

<%@ Control AutoEventWireup="false" CodeBehind="myControl.ascx.vb" Inherits="myProject.myWrongControl" %>

The the code behind class: 类后面的代码:

Partial Public Class myControl

You have to add 你必须添加

runat="server"

to each element in your page. 到页面中的每个元素。

对我来说,删除/重命名以下位置的文件是有效的:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\myvirtualwebsite\e331e0a9 

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

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