I'm a really beginner so my question may be appear ridiculous.. But, i wonder how the files .aspx.designer.cs works.. It's the first time i work with a solution containing files .aspx.designer.cs for each pages. So i understand it's declaration of controls used in the .aspx for code-behind..
Here is my questions:
Why sometimes solutions doen't have .aspx.designer.cs files? (is the files hidden or doesn't exists?)
I often have problems with this files, they don't Automatically recreate declarations of controls when i add some in the .aspx code, what am i doing wrong?
Visual Studio has two approaches for creating websites: Web Site Projects and Web Application Projects. (OK, OK, three if you add MVC).
Only Web Application Projects have designer.cs files. Web Site Projects don't have them.
The Web Application Project type was added in Visual Studio 2003.
The
.aspx.designer.xx
files are the bridge for the ASP.NET webforms code-behind files and the .aspx markup files. Any server control existing on the ,aspx markup page is represented here. Most important are the name and type of the server control.This, in part, allows Visual Studio to give the user IntelliSense in the code-behind page for server controls created at design-time.
How they work: Visual Studio will generate, or keep in sync, a
protected
member in the .designer file when you add/remove a server control from the designer.Notice that .designer files create a
partial class
. This provides the linkage to the code-behind file. That's how Intellisense gets the hook between the .aspx and the code-behind.You can regenerate your designer file: http://www.undermyhat.org/blog/2009/07/tip-regenerate-aspx-designer-cs-files-when-corrupted/
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
myfile.aspx.cs
Note: the CodeBehind file class must inherit from the Page class, or some derivative thereof.
myfile.designer.aspx.cs
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"):
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.