Compiler says control "Does not exist in current context"
I'm working on an ASP.NET page that contains <asp:Panel ID="pnlDescription"...>
When I attempt to reference that panel in the code-behind page, intellisense recongnizes it, but it generates the following error at compile time:
The name 'pnlDescription' does not exist in the current context.
In the code, I'm setting the visible proptery to false. When I run the page - it runs just fine, it just won't compile, which gets in the way when I want to do some debugging.
I also have an <asp:Label...> on this page with the same issue.
This particular form has grown to be quite large in its number of controls, and I'm not sure if that has anything to do with why I'm seeing this error.
Any help?
John Haas
That is strange, i tested it again. I created a page, in the html code i added:
| | <asp:Panel ID="pnlDescription"> <img src="http://www.google.com/images/logo_sm.gif" /> </asp:Panel> |
And then i compiled, noting was wrong but pnlDescription was missing in my code behind page, so i added this line:
| | /// <summary> /// Summary description for WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Panel pnlDescription; // .... |
Now he was available in my IntelliSense and i can reache the panel withing my whole page code (accept for static's) and it compiled smoothly.
Can you provide us the source?
You also can use:
| | Panel pnlDescription = FindControl( "pnlDescription" ); |
I was having a very similar problem with the same error.
As a work around I excluded the aspx and aspx.cs files from the project in the solution explorer. After that, I closed visual studio, included the files again and - Magic!. It compiled...
Hope this helps
Well guys, none of those tips helped me (in VWD2005). I had to create new page+codebehind, copy the old aspx and .cs contents to new files and change the Inheritance references. After that the build worked.
The context error was fixed but also another one where I defined a label in .aspx and then tried to reference to it in codebehind. Cs-file couldn't find the label at all. Anyway, this issue was fixed too when creating new files and copying the old stuff to new ones.
" As a work around I excluded the aspx and aspx.cs files from the project in the solution explorer. After that, I closed visual studio, included the files again and - Magic!. It compiled..."
This worked like a magic Thaks
Hi,
I am also facing the same problem. As suggested, i tried to create a new solution and include the same page that was giving error. It worked fine on the new solution. However the the existing solution keeps on giving the same compiler error. I don't think this is the right way to tackle this problem, as we can't keep on creating new solution each time.
In the mean time i found a work around and it's working fine. You can also refer to the same element using following syntax.
Panel panTest;
panTest= (
Panel)Page.Master.FindControl("contentplaceholder1").FindControl("panName");Thanks,
Sandipta
Try ...
Recreate the Control in VS2005 and copy all your code over to the new one. The new one has a .designer.cs file that was missing from the one where this error occurred, and in that designer.cs file is the missing declaration. And no, adding another partial class by hand doesn't seem to work, only when VS2005 makes it all for itself does this seem to work.
This worked for me.