As p.campbell pointed-out, the .designer.cs file links the .aspx file to its .aspx.cs CodeBehind file. Without the .designer.cs file, every .aspx page control in the .aspx.cs CodeBehind file will return the error, "does not exist in the current context". The linkage in .designer.cs is done based on the "Inherits" property of the @ Page directive in the aspx file together with the namespace and class of the .aspx.cs CodeBehind file. The final segment of the "Inherits" property must match the class defined in both the CodeBehind file and the .designer.cs file, and the segments prior to it must match the namespace of the .designer.cs and CodeBehind files.
Example:
myfile.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="myfile.aspx.cs"
Inherits="my.namespace.dot.classname" %>
myfile.aspx.cs
namespace my.namespace.dot {
public partial class classname : Page { ... }
}
Note: the CodeBehind file class must inherit from the Page class, or some derivative thereof.
myfile.designer.aspx.cs
namespace my.namespace.dot {
public partial class classname { ... }
}
Note: the .designer.cs class doesn't care about inheritance, just that the class name matches the CodeBehind and .aspx files.
You can regenerate a lost .designer file like this (w3cgeek.com "Regenerate designer.cs"):
- Create a new, blank file in same dir as your .aspx and .aspx.cs files named "myfile.aspx.designer.cs" where "myfile" is the name of the .aspx and .aspx.cs files that you want to link.
- Add a namespace with an empty class to the new file, and ensure their names match the namespace and class specified in the .aspx and .aspx.cs files which you are linking.
- Save the .designer.cs file, make any change to the .aspx file (e.g., adding a space), and save the .aspx file.
Visual Studio should auto-populate the .designer.cs file with all the necessary code to link your .aspx and CodeBehind files. The "does not exist in the current context" errors should now be gone!
EDIT: I added the .designer.cs instructions because the link is dead which was originally posted by p.campbell.