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

[737 byte] By [Haashole] at [2008-2-21]
# 1

In your code behind you must create the member as well. When you drag/drop a control on you Layout View VS will generate this for you; otherwise you have to do it yourself. It isn't that hard:


public class MyWebPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel pnlDescription;
// ...

Hope this helps!

PJ.vandeSande at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 2
Thanks PJ,

I did as you suggested, and now I get the following error at compile time:

The type 'MyWebPage' already contains a definition for 'pnlDescription'

John

Haashole at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 3
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" );

PJ.vandeSande at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 4

Hi!

Thanks for asking! I'm a member of the ASP.NET team, and was just popping over here to see what was going on. The best place to ask ASP.NET questions is over on the ASP.NET forums at http://www.asp.net/welcome.aspx?tabindex=1&tabid=39

Looks like PJ is answering/answered your question, so I won't chime in, but future questions should go to http://forums.asp.net.

HTH,

PEte

PeteL-MSFT at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 5

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

gc_asd at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 6

I forgot to mention that I rebuilt the project (Build>Rebuild WebSite) before adding the files again.

I am using Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Good luck.

gc_asd at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 7
that's a brilliant (although tedious) workaround, I got the same bug in VS Web Developer Express. I guess that new partial keyword needs a little work ;).

Thanks a million!

MichaelAppelmans at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 8

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.

k000der at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 9

" 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

RajagopalRamadugu at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 10

Hi,

i am using .net 2.0. In VS2005 thesr controls declaration are not shown as in VS2003 codebehind page.

Where can i add these declarations in VS2005(.NET 2.0).

Thx

Sandeep

SandeepBhutani at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 11
That workaround did not work for me, is there any other way to fix it ?
NilayPatel at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 12

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

Sandipta at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 13

Hello all,

I have the same problem now. Try allready all the things all of u said. Nothing works.

Trying 2 use the FileUpLoad Control.

NE other ideas ?

Cheers !

aaronc76 at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...
# 14

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.

HitechRider at 2007-8-30 > top of Msdn Tech,Feedback for forums and MSDN websites,Off-Topic Posts (Do Not Post Here)...