Web Application Development Using ASP.
NET
Lectures & Exercises
CHAPTER II: C# PROGRAMMING BASICS
2.1 Control Events and Handlers
Most ASP.Net pages will contain controls such as textboxes and buttons that
will allow the user to interact with them.
When some action is performed, the control will raise an event (call handler
code)
2.1.1 Control Events and Subroutines
When an event is raised, handler code is called.
For Example:
Button Control Events…
OnClick – when user clicks a button
OnCommand – When user clicks button
OnLoad – When page first loads and button loads
OnInit – When button is initialized
OnPreReader- just before button is drawn
OnUnload – When button is unloaded from memory
OnDisposed – when button is released from memory
OnDataBinding – When button is bound to a data source
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.1.2 Components of a Subroutine
The access specifier defines the scope of the subroutine.
public is a global subroutine that can be used anywhere.
Private is available in a specific class.
Most subroutines will be public.
Designates the data type of the value to be returned from the subroutine.
void says the block of code does not return a value
Other data types could be returned
Names the subroutine or event handler
Parameters allow information to be passed into the subroutine so it can
be used or modified.
Object s is the object to which this event belongs
Object is the base class for every class in C#
EventArgs e allows an array of information to be passes as one
parameter
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.2 Page Events
Every ASP.Net page has a page object with events associated with it
These events are fired in sequential order:
Page_Init – called when page is to be initialized
Page_Load- called once browser request has been processed
Page_PreRender- called once all objects on page have reacted to browser
request
Page_UnLoad- called once page is ready to be discarded
2.3 Variables and Declarations
Variables are data that allows the programmer to store, modify, and retrieve data.
Variables have a name, called an identifier.
A variable declaration contains the data type and name of the variable.
A variable can also be initialized when it is declared.
2.3.1 Common Variable Types
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.4 Arrays
Arrays allow a group of elements of a specific type to be stored in a contiguous
block of memory
Each item in the array had an offset called an index
Derived from System.Array
C# arrays are zero-based
Can be multidimensional
Arrays know their length(s) and rank
Bounds checking is automatic
Declare
Allocate
Initialize
Access and assign
2.5 Operators in C#
C# provides a fixed set of operators, whose meaning is defined for the predefined
types
Some operators can be overloaded (e.g. +)
The following table summarizes the C# operators by category
Categories are in order of decreasing precedence
Operators in each category have the same precedence
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.5.1 Operators and Precedence
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.6 Statement Syntax
Statements are terminated with a semicolon (;)
Just like C, C++ and Java
Block statements { ... } don’t need a semicolon
2.6.1 Expression Statements
Statements must do work
Assignment, method call, ++, --, new
2.7 Conditional Statement
2.7.1 If... If else… else if…. Statement
Requires a bool expression
Like C, C++ and Java, beware of dangling elses!
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.7.2 Switch Statement
Can branch on any predefined type (including string) or enum
Must explicitly state how to end case
With break, goto case, goto label, return, throw or continue
Not needed if no code supplied after the label
2.8 Looping Statement
2.8.1 while Statement
Requires bool expression
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.8.2 for Statement
Just like C++ and Java.
2.8.3 foreach Statement
Iteration through arrays or collections foreach allows “read only” access
to items in a collection
2.9 Jump Statements
break
Exit inner-most loop
continue
End iteration of inner-most loop
goto <label>
Transfer execution to label statement
return [<expression>]
Exit a method
throw
Used in exception handling
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.10 Namespaces
Namespaces provide a way to uniquely identify a type
Provides logical organization of types
Namespaces can span assemblies
Can nest namespaces
There is no relationship between namespaces and file structure (unlike Java)
The fully qualified name of a type includes all namespaces
The using statement lets you use types without typing the fully qualified name
Can always use a fully qualified name
Best practice: Put all of your types in a unique namespace
Have a namespace for your company, project, product, etc.
Look at how the .NET Framework classes are organized
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.10.1 Namespaces in .Net
To use certain features .Net we can import the namespace into our
ASP.Net page
For example, if you want to access an SQL database from a web page
you would import “System.Data.SQLClient” (more on this later)
2.11 Key Object-Oriented Concepts
Objects, instances and classes
Identity
Every instance has a unique identity, regardless of
its data
Encapsulation
Data and function are packaged together
Information hiding
An object is an abstraction
User should NOT know implementation details
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.11.1 Instantiating a Class
Use the new operator to create an object of a class
Properties and methods of the object can now be accessed
2.11.2 Scope
Encapsulation hides certain properties and methods inside the class
Some class members need to be accessed from outside the
class. These are made public
Those that are hidden from the outside are private
Those that can only be accessed through inheritance are
protected
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.12 Inheritance in C#
2.13 Code-Behind
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
2.13.1 Code-Behind Example
sample.aspx contains page layout and static content
sample.aspx inherits from the class Sample
The definition of class Sample is in the Sample.cs file
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14
Web Application Development Using ASP.NET
Lectures & Exercises
Exercise No. 2 Grade: ______________
Time: ____Minutes Time Started: ________
Directory Folder: C:\inetpub\wwwroot\LNFNMI\ Time Finished: ________
File Names: Exer2Reserv.aspx
Exer2_1.aspx (2nd link page)
Exer2_r1.aspx (1st page of the room)
Exer2_r2.aspx (2nd page of the room)
Exer2_r3.aspx (3rd page of the room)
Exer2RegiCaptcha.aspx
Files to use: 02_Exer2Materials.zip
Design and create a Web site for J.CO’s Cottages Reservation System. Your
home Web page must include an image or a background picture along with a nicely
formatted logo of the company name. Include a link to a second page that lists each of
the room names as a link. The link for each room should describe the price and amenities
for the room and/or show an image for the room (images of your choice).
Note: This is a Web page; make it attractive, Use Web Controls, CSS, and AJAX for
reservation, then after selecting reserve now, it automatically redirect to the registration
page. (Customize your registration page with captcha).
By: Jefferson A. Costales, MIT, IBM -CDA, MCTS, ZCE, OCE Chapter 2 of 14