aspx.designer.cs how does it work?

2019-01-17 20:29发布

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?

3条回答
看我几分像从前
2楼-- · 2019-01-17 21:29

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.

查看更多
太酷不给撩
3楼-- · 2019-01-17 21:32

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.

  protected global::System.Web.UI.WebControls.DropDownList DropDownList1;

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/

查看更多
来,给爷笑一个
4楼-- · 2019-01-17 21:32

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"):

  1. 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.
  2. 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.
  3. 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.

查看更多
登录 后发表回答