C# ASP.NET 控件无法在 Visual Studio 2008 中的代码隐藏中引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET controls cannot be referenced in code-behind in Visual Studio 2008
提问by Sara Chipps
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. 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.
我首先认为它可能是控件类型,因为最初我试图在更新面板中引用中继器。我知道我正确地引用了我的 aspx 页面中的代码。但是为了以防万一我搞砸了,我开始从头开始重新创建页面,这次在 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.
我已经向 Microsoft 提交了故障单。我卸载了所有加载项,然后重新安装了 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.
任何想要查看我的代码的人都可以询问,但我使用的是直接所见即所得的视觉工作室“新的 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.
有没有人在使用 Microsoft 解决这些问题时遇到过成功?有什么办法不用付钱就能加快这张票吗???几天来我一直在与微软的代表交谈,但没有运气,我已经死在水中了。
Jon Limjap:I edited the title to both make it clear and descriptive andmake sure that nobody sees it as offensive. "Foo-barred" doesn't exactly constitute a proper question title, although your question is clearly a valid one.
Jon Limjap:我编辑了标题以使其清晰和描述性,并确保没有人认为它具有冒犯性。“Foo-barred”并不完全构成一个正确的问题标题,尽管您的问题显然是一个有效的问题。
采纳答案by Brian Boatright
try clearing your local VS cache. find your project and delete the folder. the folder is created by VS for what reason I honestly don't understand. 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.
尝试清除本地 VS 缓存。找到您的项目并删除该文件夹。该文件夹是由 VS 创建的,我真的不明白什么原因。但我有好几次清除它并重新构建修复问题......希望这也是你所需要的。
here
这里
%Temp%\VWDWebCache
and possibly here
可能在这里
%LocalAppData%\Microsoft\WebsiteCache
回答by Sean Lynch
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");
回答by Brian Boatright
you will also find .net temp files which are safe to delete here: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
您还会在此处找到可以安全删除的 .net 临时文件:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
回答by LorettoDave
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. 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.
当 Visual Studio 尝试重建设计器文件时,它遇到了(并向我揭示了)问题的根源。就我而言,VS 丢失了对我页面上的一个控件所需的 DLL 的引用,因此我必须清除项目中生成的 bin 文件夹。
回答by Daan
In my case, I was working with some old web site code, which I converted to a VS2008 solution. I encountered this same problem.
就我而言,我正在使用一些旧的网站代码,我将其转换为 VS2008 解决方案。我遇到了同样的问题。
For me, the fix was to right-click the Web Sites project in the Solution Explorer and select Convert to Web Application. This created designer.cs
files for all pages, which did not yet have these files before.
对我来说,修复方法是在解决方案资源管理器中右键单击网站项目,然后选择转换为 Web 应用程序。这designer.cs
为所有页面创建了文件,之前还没有这些文件。
回答by Leah
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="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 和新控件的新设计器文件。
回答by Dennis
For me, deleting/renaming the files in the following location worked:
对我来说,删除/重命名以下位置的文件有效:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\myvirtualwebsite\e331e0a9
回答by Yogesh P
I observed this happens because of missing .designer.cs file. 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.
我观察到这是因为缺少 .designer.cs 文件。以下在我的情况下解决了这个问题(基本上我将这些文件从 VS 2005 web 项目复制到 VS 2010 项目):右键单击 .aspx 文件并选择菜单“转换为网站”,这将创建 .designer.cs 文件和那么它应该工作文件。
回答by Sardor
Check out your aspx file on errors, once I've faced with that problem
一旦我遇到了那个问题,请检查您的 aspx 文件中的错误
回答by StronglyTyped
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. 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. For whatever reason, this messed everything up and created the problem described in the 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.
我发生过几次这种情况,今天又因为新的原因再次发生。我通常通过 IIS 运行我的项目,但需要在本地运行它以进行调试。我通常在 IIS 中的端口 80 和调试中的 81 端口上运行,但我在 web.config 中有一些使用 80 的设置,所以我只是在 IIS 中终止了该站点,并在项目设置中将网站切换到端口 80。无论出于何种原因,这都搞砸了一切并造成了 OP 中描述的问题。我开始一一尝试,包括这里提到的所有建议,但最终在项目设置中将端口切换回 81。