The name does not exists in the current context asp.net

Many a times while writing a code we get this error which says, “The name <control> does not exists in the current context”. This is because the code behind file is not able to find the control being added to your aspx file.

Control does not exists in current context

Control does not exists in current context

There are three possible solutions for this error,

1 – There are two classes with same name. Possibly you have renamed a page and created a new one, but you did not changed the name in the cs file. So make sure you have only one file with this name in your project. There may be two files with different name but same class name this may create problems.

2 – Multiple web.config files in your project. Make sure you have only one web.config file in your project. However you can have mulitple web.config files in your project but that has to be in particular format check here.

3 – The designer file is not refreshed.

control does not exists in current context designer file

control does not exists in current context designer file

In this case you have to delete the existing designer file

delete web page designer file

delete web page designer file

After deleting the file right click on the page, in this case default.aspx and select “Convert to web application”

regenerate designer file - convert page to web application

regenerate designer file - convert page to web application

Once you convert it will prompt for confirmation just select yes

web Page to web application conversion prompt

web Page to web application conversion prompt

Once converted you can see the added control declaration in the designer file.

New designer file after converting page to web application

New designer file after converting page to web application

Now you can use the control in your code behind.

Enjoy coding 🙂