Week 2: Introduction To Web Forms & C#

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

WEEK 2

Introduction to Web Forms & C#

C236 WEB APPLICATION DEVELOPMENT IN .NET


AY2015-16 SEMESTER 1
MICROSOFT .NET

Week 2 2
.NET FRAMEWORK
VB.NET C# F# IronPython C++

Common Language Specification


Visual
ASP .NET Windows Form Modern Apps
Studi
o
.NET Framework Class Library .NET

Common Language Runtime

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

 Exact Numeric Type


 decimal

 Character Types
 char, string

 Boolean Type
 bool
Week 2 8
ARRAYS
 Built on .NET class System.Array

 Declared with type and dimension


 int [ ] array1D
 int [ , ] array2D

 Created using new with bounds


 array1D = new int[20]
 array2D = new int[10, 5]

 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

 Escape sequences express special characters


 char newLine = '\n';
 char backslash = '\\';

Week 2 12
STRINGS
 A string literal is specified inside double quotes
 string abc = "apple";

 The escape sequences valid for char also work


inside strings
 string msg = "Hello\t Blah Blah\n";

 Verbatim strings are strings that does not support


escape sequences.
 Prefixed by @
 string msg = @"Escape \n makes no difference";

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

 In the Solution Explorer,

 Each Web Form has three files


 aspx, aspx.cs, aspx.designer.cs

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

DateTime currentMoment = DateTime.Now;

 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

Why the GroupName of the Radio Button should be the same?

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"

string bbb = String.Format("{1}{0}{2}{1}", "E", "T", "S")


// bbb is "TEST"

string ccc = String.Format{"{0,7}", 35);


// ccc is " 35"

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);

String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year


String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second

 DateTime dt = new DateTime(2014, 4, 9, 8, 30, 17)


 Seconds ?
 Months ?
 Days?
 Hours?
 Minutes?

 Test Yourself –
 What format string for dt will output "20:08 9-Apr-14"?

String.Format("{0:HH:mm d-MMM-y}", dt);

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

You might also like