Chapter 1: Intro.
To Internet Programming
- Differences between Static & Dynamic Web Page
Static Web Page Dynamic Web Page
Content Fixed Dynamic
* Changes according user’s
preferences
Appearance Always same to Generated dynamically at run
every users time
Coding Language Plain HTML PHP / Javascript / ASP
File Size Small Large
* Contains server-side code
Functionalities Limited More functions such as
database access
- Differences between Client-side & Server-side Dynamic Pages
Client-side Server-side
Effects created Browser Server
by
Languages(s) Javascript .NET (C#, VB.NET), PHP
used
Response speed Fast Slow
Server memory Low High
consumption
Limitations Limited functionalities More complex features
- Ex. Database access
SEO Search engine might not be Easier for search engine to
able to properly index web index web page
page
Site interaction High Relatively static
- .NET 2-step Compilation
CIL & CLR together provides the structural optimization of pre-
compiled code along with the portability of interpreted code
♪ Pre-compiled code
Code is compiled when done writing
♪ Interpreted code
Code is compiled at time of execution
♪ Hence, we can use all languages interchangeably within applications
Common Language Runtime (CLR)
♪ Responsible for execute application code
♪ Converts code to CIL
Common Intermediate Language (CIL)
♪ Low-level & platform-independent language
♪ When application executes, CIL code is compiled into machine code
by JITTER (Just-In-Time compiler)
♪ Only methods are compiled from CIL into machine code
Chapter 2: Server Controls & Site Design
- 3 Types of ASP.NET Development Models
Web Pages
♪ Simplest ASP.NET models
♪ Built around single web pages
♪ Provided with built-in templates
MVC (Model View Controller)
♪ A framework for building web applications
Web Forms
♪ Traditional ASP.NET
♪ Oldest event-driven development model
♪ Web pages with added:
Server controls
Server events
Server code
♪ Contain 2 blocks code:
HTML template
ASP.NET code
- Benefits of ASP.NET Server Controls (ISBE)
1. Intuitive object model
♪ Expose HTML elements of a page in an intuitive object model
2. Separate design from logic
♪ Separate design content of a page from application logic
3. Browser compatibility
♪ Maintain browser compatibility
While supporting advanced browser features
4. Enable view state
♪ Automatically retain value of properties by participating in view state
- Differences of HTML Control & Server Control
HTML Control Server Control
Processed By Web browser Server
Advantages ~ Provide basic control ~ Enables view state
rendering * Allows value to be
~ Provide client-side retain even after page is
scripting refreshed
Limitations Need to consider browser Must be processed by a
compatibility server
- Master Pages
Defines layout to be used by all pages
Essential elements
♪ Ex. Header & Menu
♪ Present on every pages
Advantages:
♪ Easy to create
Simply apply to new content page
♪ Easy to maintain
Can modify just a single Master Page to change appearance of all
pages
- Site Map
Overview of pages within a website
Allows visitors to quickly jump to any section of a website listed in the
site map
Chapter 3: Event-Driven Programming & Postback
- Event
An action taken on application by some force outside of code
♪ External force usually is user
Can use to hold code that will run when an action occurs
4 Sections of Event-driven Environment:
Section Examples
An event occurs User clicks a button (Force from outside)
System detects the event ASP.NET registers that an event has
occurred
System reacts to the event Some code is executed
(Ex. Calculation & display result)
System returns to original state Waits for next event
3 Major Groups of Events:
♪ HTML events
Occur on web page
Handled by browser (client)
♪ ASP.NET page-level events
Automatically run by ASP.NET when a page loads
No user involvement
Ex. Page load
♪ ASP.NET server control events
Definition:
Largest events group
Handled by server
Occur due to user interaction with the page
Ex. Click button
Advantages:
More available resources like custom-built objects
Not rely on browser’s capability to recognize & handle
HTML events
Can use any language to write code for event handler
Differences between HTML Event & Server Control Event
HTML Event Handler Server Control Event Handler
Runs on client side Runs on server side
Rely on browser support Works across all browser
* Because event is handled by server
Event handling code can only be Event handling code can be written in
written in Javascript any languages supported by .NET
Resources restricted to client side More available resources
- Stateful Postback
Retain state across postback
Postback
♪ A process which browser send info. to server
♪ Then, handle the event, sending html back to client
When Postback is used, info. about state of ASP.NET form is sent back
in an associated hidden control “_VIEWSTATE”
Value in _VIEWSTATE
♪ Generated by ASP.NET by encrypting the values of old state of form
♪ Value is used to:
Persist the state between page submissions
To store programmatic changes
Chapter 4: Database Programming
- Data-Driven Web Page
Data are stored in a database on the server-side
As data changes, they are reflected on the web site without human
intervention
Advantages:
♪ Maintainability
Easier to maintain data
Keep data up-to-date
♪ Reusability
Databases can easily backup & reuse
♪ Data context
Allows to define relationship & rules for data in database
Disadvantages:
♪ Development time
Time consuming to write code to access database
♪ Dependency on database
Website fail if database fail
♪ Cost
Full enterprise-level database solution are quite expensive
- 3 Ways to Bind Data
SQL Data Source Method
♪ Enable rich capabilities for retrieve & modify data such as:
Querying
Sorting
Paging
Filtering
Updating
Deleting
Inserting
Model Binding Method
♪ Model Binding Steps:
1. Create model
By coding / generate from existing database
2. Create a database context
Database tables are create as classes (the models)
3. Use desired data bound control on form
Models are then bound to server data controls such as
GridView
4. Write backing codes to perform transaction
Coding by Hand
♪ Programmatically create connection & SQL queries
- Data Binding
A process of dynamically assigning a value to a property of a control at a
run time
Can use to bind properties of a data-bound controls to a data source
Syntax for display data: <%# Eval(“field”) %>
Syntax for display & update data: <% #Bind(“field”) %>
- Data Bound Control
Controls Capabilities Description & Uses
GridView - Read - Display multiple records on a table
- Edit - Inflexible layout
- Delete
- Sorting
- Paging
DetailsView - Read - Displays a single record at a time
- Edit as a table
- Delete - Default structure provided
- Update - Often used in master-detail
- Paging scenarios
* Where selected record in a
master record such as GridView
control determines the record
displayed by DetailsView control
FormView - Read - Similar to DetailsView
- Edit * Displays single record
- Update - No default structure
* Must use templates
- Often used with GridView
ListView - Read - Display multiple records in a grid
- Edit - All field in 1 cell
- Delete * 1 cell = 1 record
- Update - Flexible layout with templates
- Sorting
- Paging
Repeater Read only - Similar to ListView
* 1 cell = 1 record
- No default structure
* Can create own custom layout
- Display multiple records using
template
- Loops through data with exact
supplied template
- No built in functionality
Chapter 5: Membership & Role Management
- Basic Principles
Authentication
♪ Process of verifying identity of user / computer
Authorization
♪ Process of determining what a user (or a group of users) is permitted
to do on a computer or a network, or to use a resource
- ASP.NET Security
Provides membership management service
♪ To authenticate users to access a page / entire site
♪ Provides Login control
- Types of Authentication
Forms authentication
♪ Uses traditional login/logout pages
♪ Codes associated with a Web form handles users authenticated by
username / password
♪ Users are usually stored in a database
♪ Advantage
Easy to implement
♪ Disadvantage
Need to maintain own database
Windows authentication
♪ Advantage:
Suitable for developing intranet application
♪ Disadvantage:
Not suitable for web application resides outside of organization
Internet
Microsoft Account (formerly Passport authentication)
♪ Centralized authentication service provided by Microsoft
♪ Offers a single logon & core profile services for member sites
♪ Advantages:
Single sign-on
Integrated with other services such as Windows Azure
No need to maintain database to store user info.
♪ Disadvantages:
Fees involved
Social Login
♪ A form of single sign-on using existing login info from social
networking service such as:
Facebook
Twitter
Google+
Chapter 6: State Management
- HTTP (HyperText Transfer Protocol)
Communication protocol of TCP/IP Suit with Web server
- State
A program stores data in variables (memory locations)
♪ Contents of memory locations at the given point in the program
execution is called state
Refers to current status of properties, variables, & other data maintained
by an application for a single user
- Client-side State Management Options
Cookies
♪ Files create by websites
♪ 2 Types of Cookies
Session Cookie
Kept in browser’s memory
Exist only for duration of browser session
Persistent Cookie
Kept on user’s disk as a text file
Retained until cookie’s expiration date
♪ Cookies Usage
Store user’s browsing information such as:
Client preferences
Last visited data & time
User details
Example: username & password
Previous search details
Can be used by a web server to store info. on a client computer
Can be retrieved by same web server only
♪ HttpCookie Property
Name
The cookie’s name
Value
String value assigned to cookie
Expires
DateTime value that indicates when cookie expire
♪ Advantages:
Persist on client’s computer space
No need allocate on web server to store user-specific info.
Save small amounts of info. for long periods of time
Can be used to customize a user’s visit to your web site
♪ Disadvantages:
Users can choose not to accept cookies on their Web browser
Users can block the cookies
Users can manually delete cookies
Unable to save large objects, arrays, or other complex data types
♪ Use Cookies When:
Store small piece of data
Can be configured to expire after any length of time
Query String
♪ Used in Anchor tags & hyperlinks (URLs)
♪ To pass info. from 1 page to another
View State
♪ Generated by ASP.NET by encrypting the values of old state of form
♪ Value is used to:
Persist the state between page submissions
To store programmatic changes
Control
Hidden Fields
- Server-side State Management Options
Application State
♪ Application Object
To maintain state globally
Visible throughout the whole application lifetime
Can be assessed by all pages & by all users of application
Can used to store any variable type
♪ Use Application When:
To maintain info. that is global to entire Web site
Can drain server’s resources
Session State
♪ Each visitor is assigned as individual session
Data stored in session variable are not shared among different
users
♪ Visible from page to page during a session
♪ Session variables can be used in all pages in an application for a
particular session
♪ Use Session When:
To maintain user-specific info.
To maintain state only for the user’s visit to your site
Can used to store any variable type
Cache
♪ Can use on client-side too
♪ Process of storing frequently used data on server to fulfil subsequent
requests
♪ 3 Types of Caching
Output Caching (Page Output Caching)
Allows entire contents of a page to be persisted to memory &
used to fulfil client requests
Save post-rendered content
Hence, no need to be regenerated again next time it’s
requested
Syntax:
<%@ OutputCache Duration = “60”
VaryByParam = “None”>
Duration attribute is used to define no. of seconds a page
is stored in the cache
Must include VaryByParam attribute / VaryByControl
attribute
VaryByParam attribute allows to vary cached output
depending on query string
VaryByControl attribute allows to vary cached output
depending on a control value
Use Output Caching When:
Generated page changes every few hours as info. is
loaded into database
Fragment Caching (Partial Page Caching)
Allows parts of a page to be cached & other parts to be
dynamic
By enabling the Shared = “true” attribute, UserControl’s
output can be shared among multiple pages & on sites
Use Fragment Caching When:
Generated page generally stays same
Only several tables shown within output change regularly
Data Caching [Not cover in syllabus]
Profile Properties
Database Support
Chapter 7: Validation Controls
- Definition of Validation Control
To validate data of input control
Display error message to user if data input is invalid
- 6 Types of Validation Controls
RequiredFieldValidator
♪ Ensures field is not left empty
CompareValidator
♪ Compares entered text with a value / with another control
♪ Check data type
RangeValidator
♪ Ensures entered text is within a specified range
RegularExpressionValidator
♪ Matches entered text against specified pattern
CustomValidator
♪ Runs custom code to validate entered text
ValidationSummary
♪ Provides place to summarize error messages
Chapter 8: Reusable Code for ASP.NET User Control
- User Control
An ASP.NET page that has been converted into a control
♪ File extension = .ascx
Can contain both HTML & Web Controls
Can contain same event-handling subroutines as a normal ASP.NET
page
Advantages:
♪ Reusability
Enable to reuse same content & programming logic on multiple
ASP.NET pages
Used for repetitive elements
♪ Code Reduction & Encapsulation
Reducing amount of code per page by encapsulation repetitive
elements
♪ Improve Performance
Enable fragment caching
Limitations:
♪ Not Ideal For:
Separation of presentation HTML from code blocks (server
control)
Encapsulation of data access methods in a reusable package
(Data access class / component)
Creating a control that can be reused more widely than in just the
application (which can be achieved by custom control)
Properties:
♪ Content can be static / dynamic
♪ Can expose properties in a user control
- Custom Control
3 Ways to Create Custom Control
♪ Derived Custom Control
Derived from existing Control
♪ Composite Control
Grouping existing controls together into a new compiled control
♪ Full Control
Similar to user controls
- User Control VS Custom Control
User Control Custom Control
- .aspx pages - .DLL files
- Extension is .ascx - Precompiled components
- Supports Caching - X Support Caching
- Easy to create - Hard to create
- Good for static layout - Good for dynamic layout
- Limited visual design tool support - Full visual design tool support for
for consumers consumers
- A separate copy of control is - Only a single copy of the control is
required in each application required in global assembly cache
- X added to Toolbox in Visual - Can be added to Toolbox in Visual
Studio Studio
Chapter 9: Debugging & Error Handling
- Error
To minimize mistakes
♪ Perform testing & exception handling on portions of code that are
prone to errors
Definition of Exceptions:
♪ Exception = Error
♪ Any error condition / unexpected behaviour occurs during execution
of a program, & consequently disrupts the normal flow of execution
- 4 Types of Errors
Parser Errors
Compilation Errors
Configuration Errors
♪ Caused by problem in either Web.Config or Machine.Config file
♪ Example:
Missing of </appSettings> in Web.config
Runtime / Logical Errors
- Parser VS Compilation Errors
Parser Error Compilation Error
Both involves Syntax Errors
Syntax error in HTML code Syntax error in C# code or VB.NET
Can be recognized by compiler X recognized by compiler
Example: Example:
<asp:TextBo ID = “txtID” runat = If(x < 0 {lblMsg = “invalid value” ;}
“server” />
- Spelling of TextBox is wrong - Should be write in:
If (x < 0) {
lblMsg = “invalid value”;
}
Raised when page is in process of Raised when page is in process of
being parsed being compiled
- Runtime VS Logical Errors
Runtime Error Logical Error
Both cannot detected by compiler
Occurs during runtime, after a page is Occurs due to logical error of code
successfully being compiled
Example: Example:
- Invalid query string - Divide a number by zero
- Exception Handling
Page-Level Error Handling
♪ Used to catch & handle any error occurs in a page
♪ Methods:
Try…catch…finally
To identify portions of code that prone to errors
Page_Error() method
Handle any errors within a page, regardless where they occur
within an application
Syntax:
void Page_Error() {
Application-Level Error Handling
♪ Application_Error() method
Handles errors in any page within an application
Write in Global.asax file
Syntax:
void Application_Error (object sender, EventArgs e) {