Kamlesh
Kamlesh

Reputation: 386

ASP.NET <control> does not exist in current context

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.

Upvotes: 17

Views: 32917

Answers (11)

Yam Tal
Yam Tal

Reputation: 891

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.

Upvotes: 0

Vyskol
Vyskol

Reputation: 298

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.

Upvotes: 13

Reza Mortazavi
Reza Mortazavi

Reputation: 339

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.

Upvotes: 0

Protector one
Protector one

Reputation: 7261

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.

Upvotes: 1

Bashir ahmad
Bashir ahmad

Reputation: 241

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

Upvotes: 1

Neil Busse
Neil Busse

Reputation: 149

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)
    {

    }
}

Upvotes: 1

Lzh
Lzh

Reputation: 3635

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.

Upvotes: 0

Emanuele Greco
Emanuele Greco

Reputation: 12721

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..
  }

Upvotes: 0

jim31415
jim31415

Reputation: 8808

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.

Upvotes: 9

kprobst
kprobst

Reputation: 16651

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.

Upvotes: 22

Danny Tuppeny
Danny Tuppeny

Reputation: 42333

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.

Upvotes: 1

Related Questions