开发者

asp.Net <control> does not exist in current context

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. Bu开发者_StackOverflow社区t same error is occurring. I do not understand what is the reason behind that.


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.


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.


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.


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.


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

    }
}


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


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.


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


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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜