Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Code model
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
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
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
Example: Inline Code Method
5
<html>
<asp:Button id="btn" runat="server"/>
...
</html>
<script Language="c#" runat="server">
private void btn_Click(object sender,
System.EventArgs e)
{
...
}
</script>
Code model
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
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
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
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

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
  • 5. Example: Inline Code Method 5 <html> <asp:Button id="btn" runat="server"/> ... </html> <script Language="c#" runat="server"> private void btn_Click(object sender, System.EventArgs e) { ... } </script>
  • 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

  1. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  3. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  4. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  5. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  6. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  7. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  8. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  9. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  10. (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*