17

I am facing a problem: I have taken a dropdownList control and ID is drpDownCountries in an ASP.NET project. 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.

The code-behind code looks like this:

drpDownCountries.Attributes.Add("onBlur", "ErrorHighlight('" + drpDownCountries.ClientID + "','" + lblCountry.ClientID + "');"); 

But when I compile the project I am getting the following error:

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

I have checked this thing on different machines too, and the same error is occurring. I do not understand what the reason is or how to fix it.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Kamlesh
  • 386
  • 1
  • 4
  • 19
  • 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
  • A missing .DLL file could be the cause; have you checked that they are all present? – TylerH Feb 20 '23 at 21:54

11 Answers11

22

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
  • 16,165
  • 5
  • 32
  • 53
13

I had this same problem and what 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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Vyskol
  • 298
  • 3
  • 11
  • Clicking back and forth to the design view finally got the runtime to see the control. – DOK Dec 20 '12 at 19:26
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,588
  • 6
  • 43
  • 64
  • 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
  • 139
  • 8
1

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Bashir ahmad
  • 241
  • 4
  • 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,926
  • 5
  • 62
  • 86
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
  • 40,147
  • 24
  • 151
  • 275
  • 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!

You can then remove the temp controller and save; it won't ruin anything.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Yam Tal
  • 871
  • 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.

Lzh
  • 3,585
  • 1
  • 22
  • 36
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
  • 329
  • 3
  • 14
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,551
  • 7
  • 51
  • 70