ASP.Net -> object does not exist in the current context – really??

Another one of those problems that can stump you for a while. I often make backups of files as I progress and sometimes leave them in the website folder, this was fine with classic ASP but as I have found with ASP.Net can cause all sorts of problems.One issue I was having was trying to reference a component in my aspx page from the codebehind page and seeing the following error when trying to reference a label control:

“Label2 does not exist in the current context”

So I found that the problem was even though I renamed one of my old files it still contained the class that my codebehind was referencing (i.e. the class names were still the same) – and therefore was not seeing the newly added Label control to my new code.Shame the compiler doesn’t realise the duplication and give some indication.

Lesson: Remove any backed up files from the web folder and/or build directory to avoid pain 🙂

Note:
From a comment made by Chris (below) this can also be caused by a missing files if it was built as a ‘Web Application’ project and you are now trying to build it as a ‘Web Site’ project (e.g. could be missing a designer.cs file).

Either when downloading source from another developer or migrating a web app from one server to another you may find it was actually created as a ‘Web Application’ project in Visual Studio, and if you have created it as a ‘Web Site’ project you may hit problems. The solution to this is to convert the project to a ‘Web Application’ (right click on your project in solution explorer) and then rebuild it.

ASP.Net beware the rogue ‘form’ tag!

I have just spent 3 evenings trying to resolve an issue with an ASP.Net datagrid and getting the updated values from the cells in the OnRowUpdating event.After trying every conceivable possible method of getting the values under the sun I find that the cause of these missing values was one ‘form’ tag with ‘method=”post”‘ sitting in the HTML of my master.master page and screwing up the postback

Damn that was a hard lesson to learn!