Why are aspx code files declared as partial classes?

Why code for partial class for aspx pages?

+7
code-behind partial-classes
source share
5 answers

I would like to direct you to this page alt text http://img30.imageshack.us/img30/7692/msdnchart.gif

Thus, you have one class that contains your logic and one class that contains design material. At compile time, it is generated as a whole.

+12
source share

Because there are other parts of the class (constructor material) that are hidden from the developer

For example, instead

public MyBasePage : System.Web.UI.Page { ... protected System.Web.UI.Label lblName; protected void Page_Load(object sender, EventArgs e) { } ... } 

ASP.NET creates these declarations in different physical files, leaving it

 public partial class MyBasePage : System.Web.UI.Page { ... protected void Page_Load(object sender, EventArgs e) { } ... } 

Additional Information:

+4
source share

Partial declaration allows you to write code in other files - just put it in the same namespace and name the class the same, and they will be processed as if they were in the same file. This is great for adding functionality to the generated files. I most often use it to add functions / properties to LinqToSql objects.

+1
source share

Another reason for partial files is to handle the case where part of the class definition is generated by the tool (and can be regenerated at some point), and the rest of the class is implemented by you.

In such cases, if you do not use a partial class, this will lead to overwriting your code or to the generation process, which makes it difficult to perform its work (if it can do it at all).

With partial classes in place, the generated code can be easily restored without touching your code.

Another good example of this is to use the DataContext classes for LINQ-to-SQL: really smart stuff is generated into one set of files of a partial class, and you can provide an implementation - for validation, etc. - in another partial class, safe, knowing that recreation will not ruin your work.

+1
source share

Aspx.cs uses the PARTIAL class because the controls (e.g. TextBox, GridView) available in this class are declared in the .Aspx file (i.e., physically in a different file), so one class contains the declaretion control (.aspx file ) and another based on business logic on the controls declared in the .aspx file. when they are compiled, they are considered integer as one class.

0
source share

All Articles