Search code examples
asp.netcode-behindpartial-classes

Why are aspx code-behind files declared as partial classes?


Why is the code behind a partial class for aspx pages?


Solution

  • I would refer you to this MSDN Page (ASP.NET Page Class Overview).

    When the page is compiled, ASP.NET generates a partial class based on the .aspx file; this class is a partial class of the code-behind class file. The generated partial class file contains declarations for the page's controls. This partial class enables your code-behind file to be used as part of a complete class without requiring you to declare the controls explicitly.

    See this chart :

    alt text http://img30.imageshack.us/img30/7692/msdnchart.gif

    This way you have one class that contains your logic and one class that contains designer stuff. At compile time it is generated as a whole.