- <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "SomePage.aspx.cs" Inherits = "SomePage" %>
- < html >
- < head >
- < title > SomePage < / title >
- < / head >
- < body >
- <asp: Label ID = "lblHello" runat = "server" EnableViewState = "false" / >
- < script runat = "server" type = "text / C #" >
- protected void Page_Load (object sender, EventArgs e)
- {
- lblHello.ForeColor = System.Drawing.Color.Red;
- }
- < / script >
- < / body >
- < / html >
- using System ;
- using System.Web ;
- using System.Web.UI ;
- using System.Web.UI.WebControls ;
- public partial class SomePage : Page
- {
- protected void Page_PreRender ( object sender, EventArgs e )
- {
- lblHello. Text = "Hello from code" ;
- }
- }
After that, the actual page class is generated, which will already process our physical request. I note that its ASP.NET places in the same temporary code file in which we also saw the previous class.
- public partial class SomePage : System. Web . SessionState . IRequiresSessionState {
- protected global :: System. Web . Ui . WebControls . Label lblhello ;
- protected void Page_Load ( object sender, EventArgs e )
- {
- lblHello. ForeColor = System. Drawing . Color . Red ;
- }
- protected system. Web . Profile . DefaultProfile Profile {
- get {
- return ( ( System. Web . Profile . DefaultProfile ) ( this . Context . Profile ) ) ;
- }
- }
- protected system. Web . HttpApplication ApplicationInstance {
- get {
- return ( ( System. Web . HttpApplication ) ( this . Context . ApplicationInstance ) ) ;
- }
- }
- }
This class is by default named somepage_aspx, but this can be changed by setting the ClassName attribute on the Page directive, like this:
- namespace ASP {
- ...
- [ System . Runtime CompilerServices . CompilerGlobalScopeAttribute ( ) ]
- public class somepage_aspx : global :: SomePage , System. Web . IHttpHandler {
- private static bool @__initialized ;
- private static object @__fileDependencies ;
- [ System. Diagnostics . DebuggerNonUserCodeAttribute ( ) ]
- public somepage_aspx ( ) {
- string [ ] dependencies ;
- ( this ) . AppRelativeVirtualPath = "~ / SomePage.aspx" ;
- if ( ( global :: ASP . somepage_aspx . @ __ initialized == false ) ) {
- dependencies = new string [ 2 ] ;
- dependencies [ 0 ] = "~ / SomePage.aspx" ;
- dependencies [ 1 ] = "~ / SomePage.aspx.cs" ;
- global :: ASP . somepage_aspx . @ __ fileDependencies = this . GetWrappedFileDependencies ( dependencies ) ;
- global :: ASP . somepage_aspx . @ __ initialized = true ;
- }
- this . Server . ScriptTimeout = 30,000,000 ;
- }
- [ System. Diagnostics . DebuggerNonUserCodeAttribute ( ) ]
- protected override void FrameworkInitialize ( ) {
- base . FrameworkInitialize ( ) ;
- this . @__BuildControlTree ( this ) ;
- this . AddWrappedFileDependencies ( global :: ASP . Somepage_aspx . @ __ fileDependencies ) ;
- }
- [ System. Diagnostics . DebuggerNonUserCodeAttribute ( ) ]
- private void @__BuildControlTree ( somepage_aspx @__ctrl ) {
- this . InitializeCulture ( ) ;
- System. Web . Ui . IParserAccessor @__parser = ( ( System. Web . UI . IParserAccessor ) ( @__ctrl ) ) ;
- @__parser. AddParsedSubObject ( new System. Web . UI . LiteralControl ( @ "<html>
- <head>
- <title> SomePage </ title>
- </ head>
- <body> " ) ) ;
- global :: System. Web . Ui . WebControls . Label @__ ctrl2 ;
- @__ ctrl2 = this . @__BuildControllblHello ( ) ;
- @__parser. AddParsedSubObject ( @__ ctrl2 ) ;
- @__parser. AddParsedSubObject ( new System. Web . UI . LiteralControl ( "</ body> </ html>" ) ) ;
- }
- [ System. Diagnostics . DebuggerNonUserCodeAttribute ( ) ]
- private global :: System. Web . Ui . WebControls . Label @__BuildControllblHello ( ) {
- global :: System. Web . Ui . WebControls . Label @__ctrl ;
- @__ctrl = new global :: System. Web . Ui . WebControls . Label ( ) ;
- this . lblError = @__ctrl ;
- @__ctrl. ApplyStyleSheetSkin ( this ) ;
- @__ctrl. Id = "lblHello" ;
- @__ctrl. EnableViewState = false ;
- return @__ctrl ;
- }
- ...
- }
- }
As you can see here, to generate the first of the classes (public partial class SomePage), the CodeFile and Inherits attributes are used, and they must be matched (that is, the file specified in the CodeFile must contain a class with the name, as in the Inherits attribute). After that, a second class is created with the name taken from the ClassName attribute (or by default - <Inits_lowercase value> _aspx)
- <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "SomePage.aspx.cs" Inherits = "SomePage" ClassName = "MyPageClass" %>
The constructor somepage_aspx initializes the current virtual path for the page, since the ASP.NET infrastructure itself is not responsible for this (you need to keep this fact in mind when developing your own custom classes that inherit the page, bypassing the standard compilation path).
- public somepage_aspx ( ) {
- string [ ] dependencies ;
- ( this ) . AppRelativeVirtualPath = "~ / SomePage.aspx" ;
- if ( ( global :: ASP . somepage_aspx . @ __ initialized == false ) ) {
- dependencies = new string [ 2 ] ;
- dependencies [ 0 ] = "~ / SomePage.aspx" ;
- dependencies [ 1 ] = "~ / SomePage.aspx.cs" ;
- global :: ASP . somepage_aspx . @ __ fileDependencies = this . GetWrappedFileDependencies ( dependencies ) ;
- global :: ASP . somepage_aspx . @ __ initialized = true ;
- }
- this . Server . ScriptTimeout = 30,000,000 ;
- }
In our case, there is only one control - this is lblHello. In addition to creating an object for it, text fragments are added to the page object tree, framing our control. As a result, each line of markup is added to the page object tree, either as LiteralControl, or as a typed server control.
- [ System. Diagnostics . DebuggerNonUserCodeAttribute ( ) ]
- private void @__BuildControlTree ( somepage_aspx @__ctrl ) {
- this . InitializeCulture ( ) ;
- System. Web . Ui . IParserAccessor @__parser = ( ( System. Web . UI . IParserAccessor ) ( @__ctrl ) ) ;
- @__parser. AddParsedSubObject ( new System. Web . UI . LiteralControl ( @ "<html>
- <head>
- <title> SomePage </ title>
- </ head>
- <body> " ) ) ;
- global :: System. Web . Ui . WebControls . Label @__ ctrl2 ;
- @__ ctrl2 = this . @__BuildControllblHello ( ) ;
- @__parser. AddParsedSubObject ( @__ ctrl2 ) ;
- @__parser. AddParsedSubObject ( new System. Web . UI . LiteralControl ( "</ body> </ html>" ) ) ;
- }
Source: https://habr.com/ru/post/61074/
All Articles