There are three ways to write code in an ASP.NET web form: the mixed code method, inline code method, and code-behind method. The code-behind method, which is the default in Visual Studio .NET, separates the code into a separate file from the HTML content for better readability and maintainability. The code-behind file is compiled into the web page and associated using attributes in the @Page directive when the application starts up.
Report
Share
Report
Share
1 of 10
More Related Content
Code model
2. Writing Code
Writing code in an ASP.NET Web form is done in three
ways:
Mixed code method
The code is in the same file as the web content, mixed with the
HTML code
This method is not recommended as the source code is hard
to read and maintain
Inline code method
Code-behind method
2
3. Writing Code (2)
Writing code in an ASP.NET web form is done in three
ways:
Mixed code method
Inline code method
The code is separated in a SCRIPT section in the same file
Code-behind method
3
4. Writing Code (3)
Writing code in an ASP.NET web form is done in three
ways:
Mixed code method
Inline code method
Code-behind method
The code is in the code-behind page – a separate file from the
HTML content
When using Visual Studio .NET this is the default method
4
7. Code-behind Model
A separate compiled file containing the program logic of
the page
Each web page has its own code-behind page
Has the same name as the web page to which it is attached
The file extension is .aspx.cs
The two files are built into one when the application is
started
7
8. How Does Code-behind Work?
To associate an .aspx page to its code-behind class the
@Page directive is used
VS.NET adds three attributes to the @Page directive:
Inherits – allows the .aspx page to derive from the
code-behind class
Codebehind – used internally by Visual Studio .NET to
associate the files
Src – contains the name of the code-behind page
Used if the application is not precompiled
8
9. JIT Compilation
The Code-behind page can be either precompiled or just-
in-time (JIT) Compiled
JIT compilation
A compilation at first request
Set by the Src attribute of the @Page directive
VS.NET doesn’t add it by default
9
10. Precompilation
Precompilation
Avoids the delay at first request
Simplifies the deployment of the web application
The source code of the code-behind class is not
necessary
10
Editor's Notes
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*