Post on 09-Feb-2015
description
transcript
Copyright © 2006-2008. Iron Speed Inc. All rights reserved
Mastering the .NET Code Model
Visit us: www.ironspeed.com Download the Free Edition: www.ironspeed.com/download
2
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
ASP.NET Code Model
Single-File Page Model
Layout and Code in one ASPX file
Used only for the simplest applications
3
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
ASP.NET Code Model
Code-Behind Page Model
Most common in practice
Separate Layout and Code
Layout in ASPX
Code-behind in .VB or .CS
No performance difference
Allows re-use of common code
Designers can design layout, while developers can write code
Best practice for an N-tier application
4
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Code-Behind
Code-Behind is specified in the ASPX
5
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Code-Behind
Code-Behind is derived from the Page class (or another subclass of Page class)
6
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Best Practices - .NET Code Model
Use Code-Behind Page Model for extensibility
Copyright © 2006-2008. Iron Speed Inc. All rights reserved
Page Lifecycle
8
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Page Lifecycle
Sequence of steps a page goes through
Initialize
Load the user interface controls
Read data from the database
Display the page
And later…
Validate data
Save data into database
Redirect back to calling page
etc.
9
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Page Lifecycle
.NET handles
Loading of the page
Converting ASP controls to HTML controls
Rendering controls on the page
But at key points during the page lifecycle, .NET raises events for you to do your work
Your job: Handle various events to load data from database, initialize content in controls
10
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
.NET’s Event-based Model
Program by responding to events
Write an event handler
Initial page display has three events
User actions on page (e.g., button click) has one additional event
11
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
12
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
After Displaying a Page
Server releases the page instance
Nothing about the page or the data on the page is kept on the server
Any state information must be maintained by you in “ViewState”
13
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
What is a Postback?
User’s action causes a Postback
An event is raised
Page instance is re-created
UI Control data is repopulated from ViewState
Server-side event handler is called
Examples
Button click
Selected item in dropdown
Typed text in a textbox
Must register Event Handler to request a postback
14
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
User Actions cause “Postback”
Four events raised during Postback
Registered Event Handler called between Load and PreRender
15
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
16
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
During Postback
Check IsPostback to determine whether in Postback state
Performance Tip: Only load data during initial display (Not IsPostback)
17
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
State Management
ViewState is a standard feature of .NET
By default, state information maintained in HTML sent to browser
Encoded, but not encrypted
18
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
State Management
ViewState can be optionally saved in Session, Cache, File or Database - but:
Additional work required
Session or Cache can get timed out
File or Database requires regular cleanup
Bloats the page content, but best to leave it as Page
19
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Best Practices – Page Lifecycle
Do most of the work by handling Load event
Init should be used only for initializing event handlers
PreRender should only be used to handle dependent controls
Security for a page should be checked in Load
Use IsPostback extensively to improve performance and minimize loading data from database
Use “Page” ViewState – even though it sends lots of data to the browser
Copyright © 2006-2008. Iron Speed Inc. All rights reserved
Ajax
21
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Using Ajax
ASP.NET Ajax creates fast and flicker-free updates
Extremely simple to implement –
Just surround ASP Controls with UpdatePanel
Only sends back content that has changed
Browser only paints changed content
Can have multiple and/or nested UpdatePanels
Code-behind does not change when using Ajax
22
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Using Ajax
Can show progress using UpdateProgress
Use animated GIF image
23
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Best Practices – Ajax
Best to use a single UpdatePanel
Using multiple UpdatePanels may require special handling when updating dependent information
Copyright © 2006-2008. Iron Speed Inc. All rights reserved
Master Pages
25
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Master Pages
Creates a consistent layout
Centralizes common functionality
Easier to maintain
Typical use:
Master Page contains:
Styles
Header, Menu and Footer
Individual page contains:
Content
26
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Master Pages
27
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Master Pages
.NET merges Master pages with Page Content
Master pages can inherit from other master pages
28
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Best Practices – Master Pages
Create multiple Master Pages for different needs
Nest Master Pages if needed
Include all CSS Styles, Javascript, etc. in Master Pages
Keep Page Content focused on actual data
Copyright © 2006-2008. Iron Speed Inc. All rights reserved
N-Tier Applications
30
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
N-Tier Application
Divide application into N-Tiers
Layout in ASPX pages
UI Code in Code-Behind code files
Business Layer
Data Access Layer
Stored Procedures
31
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
N-Tier Application
32
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Best Practices – N-Tier Application
Changes to layout should not impact Code-Behind
Changes to Database Schema should have minimal impact on UI Layer
Data Access Layer must use Strongly Typed objects
Paging must be done in the database, not in memory
ASP GridView, ListView etc. do paging in memory
Concurrency handling is important in highly asynchronous applications
Copyright © 2006-2008. Iron Speed Inc. All rights reserved
Iron Speed Designer
34
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Iron Speed Designer
Implements Best Practices for
.NET Code Model
Page Lifecycle
Ajax
Master Pages
N-Tier Applications
Lets you focus on what is most important
Your business logic
Use the .NET Code Model knowledge to extend
35
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Three Recent Game Changing Enhancements
Layout Customizations
Use Spreadsheet Grid
Toolbox – Drag and drop
Easily add pre-configured panels and controls
Code Customizations
Use Excel-like Formulas
36
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Page Layout Customization
Spreadsheet shows a ‘logical representation’ of page section
Drag and drop to arrange controls
Designer converts page layout to HTML (ASPX)
(You don’t have to know any HTML or ASPX!)
Cell Editor allows fine-grained tuning and formatting
- Add any HTML or ASPX
- Change font, bold, italics, alignment, cell borders, cell width
37
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Using Toolbox Controls
Drag and drop controls onto your Web page
Toolbox controls are databound – there’s nothing to hook up!
- Fields, labels and entire panels!
38
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Formula Language
Game Changing Enhancement to Version7.0
Excel-like
Most common tasks can be done – 75 to 80%
No need to know VB.NET or C#
Easy to extend – local and global extensions
39
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Key Questions
What is your time worth?
What is the ROI?
40
Copyright © 2006-2008. Iron Speed® Inc. All rights reserved
Questions?