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

5

7

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

Quinquagesima answered 25/8, 2009 at 18:51 Comment(3)
And you should edit your question title. It is not a stupid question.Venge
"There are no stupid questions, only stupid people" -- Mr. Garrison, South Park. OK, so that doesn't apply here, I just like that quote. :DFootprint
The only stupid question is the one that you don't ask.Drawstring
V
13

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.

Venge answered 25/8, 2009 at 18:55 Comment(2)
This is one of those "lets make a chart out of something that shouldn't be a chart" charts.Marjie
"The inheritance model for code-behind pages is slightly more complex than that for single-file pages" Section in That MSDN Article was very helpful to understand this chart betterViscacha
E
4

Because there are other parts of the class (Designer stuff) that's hidden from the developer

For example, instead of this

public MyBasePage : System.Web.UI.Page
{
    ...
    protected System.Web.UI.Label lblName;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    ...
}

ASP.NET creates those declarations in different physical files, leaving this

public partial class MyBasePage : System.Web.UI.Page
{
    ...

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    ...
}

More info:

Expiate answered 25/8, 2009 at 18:53 Comment(2)
could you elaborate? is it simmilar to the winforms designer file?Quinquagesima
@Quinquagesima - yep, it is equivilent of the winforms designer file - in ASP.NET 1.1 there would be all the control declarations at the top of your code-behind, and then a collapsed region at the bottom commented along the lines of "Designer Generated, Do Not Modify" where all the events were wired up.Shady
T
1

The Partial declaration lets you write code in other files - just put it in the same namespace and name the class the same, and they'll be treated as if they're in the same file. It's great for adding functionality to generated files. I most frequently use it to add functions / properties to my LinqToSql objects.

Throckmorton answered 25/8, 2009 at 18:56 Comment(1)
Plus it helps keep all of the ugly generated code away from you and accidently editing it. You have to be much more purposeful now to edit it.... :)Garth
D
1

The other reason for partial files is to handle the case where some of the class definition is generated by a tool (And may be regenerated at some point) and the rest of the class is implemented by you.

In such cases, not using a partial class would result in either your code being overwritten, or the generation process having difficulty doing its job (If it can do it at all).

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

Another good example of this is when using the DataContext classes for LINQ-to-SQL: The really clever stuff is generated into one set of partial class files and you can provide implementations - for validation etc. - in the other partial class, safe in the knowledge that re-generation won't destroy your work.

Drawstring answered 25/8, 2009 at 19:2 Comment(0)
N
0

Aspx.cs uses PARTIAL class because the controls (e.g. TextBox,GridView) accessed in this class are declared in .Aspx file (i.e. Physically Another file) so one class contain control declaretion (.aspx file) and another business logic based on controls declared on .aspx file. when they get compiled the considered as whole a single class.

Nantucket answered 30/7, 2013 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.