Week 2: Introduction To Web Forms & C#
Week 2: Introduction To Web Forms & C#
Week 2: Introduction To Web Forms & C#
Week 2 2
.NET FRAMEWORK
VB.NET C# F# IronPython C++
Operating Systems
Week 2 3
ASP.NET FRAMEWORK
Week 2 4
C#
Week 2 5
OVERVIEW OF C#
Object-Oriented
Very similar fo Java.
Everything belongs to a class
All data types derived from System.Object
Complete C# Program
Week 2 6
PROGRAM STRUCTURE
Namespaces
Contain types and other namespaces
Type Declarations
Classes, structs, interfaces, delegates
Members
Fields, methods, properties, events, constructors
Week 2 7
DATA TYPES
Integer Types
byte, short, int, long
Floating Types
float, double
Character Types
char, string
Boolean Type
bool
Week 2 8
ARRAYS
Built on .NET class System.Array
Using initialisers
int[] myarray = { 1, 2, 3, 4 };
Week 2 9
STATEMENTS & COMMENTS
Case-sensitive
Statement delimiter is semi-colon ;
Curly Brackets to enclose multiple statements
into a block { statement; statement }
Single line comment // one line comment
Block comment /* comments */
if … else // same as Java
for ( ; ; ) // same as Java
Week 2 10
OPERATORS
Arithmetic Conditional
+ addition || or
- subtraction && and
* multiplication ! not
/ division
% modulus Comparison
++ increment != not equal
-- decrement == equal
>= greater or equal
<= lesser or equal
> greater
< lesser
Week 2 11
CHARACTERS
A char literal is specified inside single quotes
char c = 'A'; // simple character
Week 2 12
STRINGS
A string literal is specified inside double quotes
string abc = "apple";
Week 2 13
WEB FORMS
Week 2 14
CREATING A WEB FORM
Right-click on Project,
Select Add, select Web Form
Type in the name of the Web Form
Week 2 15
COMPONENTS OF A WEB FORM
ASPX
Presentation Logic
HTML, CSS and ASP.NET codes that are written to render the web
page
ASPX.CS
Business Logic
Known as "code behind"
C# codes that are written by developer for the web application to
work
ASPX.DESIGNER.CS
Codes that are generated by Visual Studio so that Intellisense can
work better
Week 2 16
SAYHELLO.ASPX
Week 2 17
SAYHELLO.ASPX.CS
Week 2 18
TOOLBOX
View menu Toobox
Shortcut – [Alt][Ctrl] + [X]
Week 2 19
SAYHELLO1
Week 2 20
SAYHELLO1 – CODE BEHIND
Week 2 21
DATETIME
A structure that represents an instant in time
To create a date with a specific year, month, day, hour,
minute, and second
DateTime date1 = new DateTime(2015, 4, 20, 9, 15, 1);
// 9:15:01am 20 April 2015
Properties
Day, Month, Year, Hour, Minute, Second
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx
Week 2 22
APPRENTICE ACTIVITY
Code SayHello1.aspx
Week 2 23
SAYHELLO2
Week 2 24
SAYHELLO2 – CODE BEHIND
Week 2 25
APPRENTICE ACTIVITY
Code SayHello2.aspx
Week 2 26
SAYHELLO3
Week 2 27
SAYHELLO3 – CODE BEHIND
Week 2 28
APPRENTICE ACTIVITY
Code SayHello3.aspx
Week 2 29
STRING.FORMAT METHOD
Syntax
Takes a format string and several arguments
The format string consists of format items delimited by { }
Returns a formatted string
Examples
string aaa = String.Format("Apple={0}, Orange={1}", 12, 34);
// aaa is "Apple=12, Orange=34"
Week 2 30
.SPLIT METHOD
Syntax
Calls from a string
Takes one char argument for separator
• Use ' ' instead of " "
Returns an array of strings
Examples
string text = "red, green, blue";
string [] colors = text.Split(',');
// colors[0] is "red"
// colors[1] is " green"
// colors[2] is " blue"
Week 2 31
.TRIM METHOD
Syntax
Calls from a string
Takes no argument
Returns a string with trailing and leading spaces removed
Examples
string x = " Nissan";
string y = " Toyota Camry ";
string z = "Hyundai ";
string a = x.Trim(); // a is "Nissan"
string b = y.Trim(); // b is "Toyota Camry"
string c = z.Trim(); // c is "Hyundai"
Week 2 32
.TOUPPER AND .TOLOWER
METHODS
Syntax
Calls from a string
Takes no argument
Returns a string with characters made uppercase or lowercase
respectively
Examples
string x = " Nissan";
string y = " Toyota Camry ";
string z = "Hyundai ";
string a = x.ToLower(); // a is " nissan"
string b = y.Trim().ToUpper(); // b is "TOYOTA CAMRY"
string c = z.ToUpper(); // c is "HYUNDAI "
Week 2 33
MORE ON DATETIMES
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
Test Yourself –
What format string for dt will output "20:08 9-Apr-14"?
http://www.csharp-examples.net/string-format-datetime/
Week 2 34
SOLVING THE PROBLEM
Week 2 35
REQUIREMENTS
Week 2 36
SOLVING THE PROBLEM
What are needed?
Aspx
• TextBox Server Control
• Literal Server Control
• RadioButton Server Control
• Button Server Control
C# (Code-behind)
• Page_Load event handler
• btnGenerate_Click event handler
Week 2 37
ASPX
Week 2 38
CODING DATE, TIME & GREETING
Week 2 39
CODING COLOR OF STANZAS
Week 2 40
GETTING THE ANIMALS AND
SOUNDS
Week 2 41
GENERATING THE STANZA
Week 2 42
NAMING SERVER CONTROLS
It is a good practice to use these standard
prefixes when naming Server Controls
TextBox – Txt
• TxtName, TxtEmail, TxtAnimals
Literal – Ltl
• LtlLyrics, LtlGreetings
RadioButton – Rb
• RbGreen, RbRed, RbMale, RbFemale
Button – Btn
• BtnOK, BtnCancel, BtnGenerate
Week 2 43
SUMMARY
What you have learned
Identify the various layers in the .NET framework
Create Web Forms with simple ASP.NET controls
Write simple code-behind logic using C#
Use C# methods for string manipulation
Week 2 44
ADDITIONAL RESOURCES
MSDN Channel 9
C# Fundamentals for Absolute Beginners
http://channel9.msdn.com/Series/C-Fundamentals-for-Absolute-Beginners
Week 2 45