16

I am facing a small big problem. I have taken a dropdownList control and ID is "drpDownCountries" in ASP.Net project. The problem is that "The dropdownlist control is placed on page, in the code behind file of c#, while typing the control name "drpDownCountries", this control ID is listed in object member list. But, in compiling the project I am getting following error.

Error : - The name 'drpDownCountries' does not exist in the current context.

I have checked this thing on different machines too. But same error is occurring. I do not understand what is the reason behind that.

Bala R
  • 104,615
  • 23
  • 192
  • 207
Kamlesh
  • 376
  • 1
  • 4
  • 18
  • 1
    Can you provide the code of where the error is occuring? – Jon Egerton Mar 05 '11 at 14:47
  • **drpDownCountries.Attributes.Add("onBlur", "ErrorHighlight('" + drpDownCountries.ClientID + "','" + lblCountry.ClientID + "');");** Here is the code, on which I am getting error. – Kamlesh Mar 05 '11 at 14:59
  • 1
    check this: http://stackoverflow.com/questions/706603/the-name-controlname-does-not-exist-in-the-current-context this answer: "Check your code behind file name and Inherits property on the @Page directive, make sure they both match." – Alexan Mar 05 '11 at 15:04
  • Actually, if I removed the code behind statements(code) then its working perfectly with another controls. – Kamlesh Mar 05 '11 at 15:15
  • 1
    Where exactly you insert this code? Page_Load? Page_Init? In Page_init control could not exist yet. – Alexan Mar 05 '11 at 15:52
  • drpDownCountries is contained within a Panel control? look at my solution – Emanuele Greco Dec 28 '11 at 08:43
  • 1
    I need to stop using Microsoft's dev tools... this is not the only 'completely silly' issue I face. I'm wasting my LIFE. I want to build something, and all I get silly problems like these and THEY ARE TOO MANY! One time it's this, next time can't connect because of another silly reason, and that because of that and so on and on to Endless Silliness. I am sure open source developers don't experience this silliness. I'm trying to be a developer but all I'm doing is housekeeping. I guess that's the difference between Microsoft developers and real open source ones. – Mzn Jun 18 '14 at 10:18

11 Answers11

21

Right-click on the ASPX (or ascx) file, and select Convert to web application (or something like that). That will force a refresh on the designer file.

kprobst
  • 15,555
  • 5
  • 30
  • 53
13

I had this same problem and tried all the answers listed here, to no avail.

What finally worked for me was to make a change to the ascx file in Design view and then save it. This finally forced Visual Studio to regenerate the designer.cs file and include my new control.

Vyskol
  • 298
  • 3
  • 11
9

I have seen this error occur when there is a copy of the .aspx page in the project folder.

Example:

Error occurs in Test.aspx.

There is a Test-copy.aspx file in the project folder.

Delete, rename with a different extension, or move Test-copy.aspx to a different folder.

Error is resolved.

jim31415
  • 8,280
  • 6
  • 41
  • 62
  • I use Google Drive to synch files between computers. I think it put another file in there called FileName[conflict].aspx by accident. I backed up that file and deleted it for good measure and everything is looking good now. Such a simple thing that I should have caught but didn't. – Jason Geiger Mar 12 '14 at 14:12
1

So first check that your ascx document is defined like so

ExampleClass.ascx

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

ExampleClass.ascx.cs

public partial class ExampleClass : System.Web.UI.UserControl
{
     protected void Page_Load(object sender, System.EventArgs e)
    {

    }
}
Neil Busse
  • 119
  • 8
1

In aspx this type of error often occurs when you miss runat="server"

Eric Aya
  • 69,000
  • 34
  • 174
  • 243
Bashir ahmad
  • 225
  • 3
  • 12
1

Do not let Intellisense fool you. Sometimes (usually after fixing problems with duplicate class names), you simply need to rebuild the project and the reported errors go away. Reopening the file after the build might be necessary.

Protector one
  • 6,343
  • 4
  • 56
  • 80
1

It's possible there is an error in your aspx/aspx file that is causing the designer file not to be updated correctly. You could confirm this by adding something new (eg. "") and see if you can access that. If not, something is probably broken in the markup that you'll need to fix.

Danny Tuppeny
  • 36,705
  • 20
  • 137
  • 258
  • Thanks, but you know I have checked the same things on different - different machines. But I am getting same experiences. – Kamlesh Mar 07 '11 at 15:36
  • 1
    If there is something wrong in your aspx/ascx, surely it would be broken on the other machines too? – Danny Tuppeny Mar 07 '11 at 17:36
  • This applies to me, I had 2 divs marked runat="server" with the same name, but even though one was commented out in the ascx file, it was silently causing an error and stopping the ascx.designer.cs file from being automatically updated. I renamed the divs in question, then I right-clicked the control and selected "Convert to Web Application" and that fixed it. – John Ferguson Oct 08 '13 at 11:20
0

The only thing that worked for me was to add a temp controller in the aspx file and saving it. That generated the designer again, and my controllers are now recognized! I'm so proud. You can then remove the temp controller and save, it won't ruin anything, you scaredy cat!

Yam Tal
  • 841
  • 6
  • 3
0

Recreate the project. Just create a new project and add the elements one by one and hope it won't happen again. If it does, well that's part of the Microsoft experience: recreate another project and so on, until you decide to quit your job and join open-source.

CORRECTION

I'm going to redo the project that I have been working on since the last 3 days using ASP .NET MVC. I should be using an open-source tech for sure, but too bad it's not my decision for this project to not use .NET.

Mzn
  • 2,547
  • 21
  • 33
0

If this is happening after copy/move pages to new location, or project, you may simply check if PageName.ascx.designer.cs is included in project.

There is a bug in visual studio (or maybe reshrper): It includes PageName.ascx and PageName.ascx.cs, but not PageName.ascx.designer.cs, which must be included manually.

Reza Mortazavi
  • 169
  • 1
  • 13
0

You should put some code to get help..

Anyway, the problem could be that drpDownCountries is contained within a Panel control.

The Panel control is a Container control, in that it can hold lots of controls.
In order to access the controls within that Panel control, you first need to "help" ASP.Net to find it.

The typical way of doing this is to use the FindControl method look here.

Code sample:

DropDownList myDrop = (DropDownList)this.Panel1.FindControl("drpDownCountries");
 if(myDrop != null)
  {
     ..somecode..
  }
Emanuele Greco
  • 12,183
  • 7
  • 48
  • 69