asp.net basics

54
Introduction to ASP.NET Week 11, day1

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: ASP.NET Basics

Introduction to ASP.NETWeek 11, day1

Page 2: ASP.NET Basics

ASPActive Server Pages

‚ASP.NET is a server side scripting language‛

Page 3: ASP.NET Basics

.NET overview•.NET is a framework for developing web-based andwindows-based applications within the Microsoftenvironment.• Components of .NETMICROSOFT Intermediate language - all code iscomplied into a more abstract, trimmed version beforeexecution. All .NET languages are compiled to MSIL –the common language of .NET

The CLR- common language runtime; responsible forexecuting MSIL code; interfaces to Windows and IIS

A rich set of libraries (Framework Class Libraries) available to all .NET languages

Page 4: ASP.NET Basics

Components of .NET (Continuation)The .NET languages such as C#, VB.NET etc that conform to CLR

ASP.NET is how the Framework is exposed to the web, using IIS (internet information system) to manage simple pages of code so that they can be complied into full .NET programs. These generate HTML for the browser (when the page is requested by a client browser).

Page 5: ASP.NET Basics

Generate HTML

Get index.aspx13

4

pass index.aspx to .netframework

5

WebServer

Index.aspx in interpreted HTMl form

Browser

2Get index.aspx from hard disk

How IIS takes requests and processes it

.NET

Page 6: ASP.NET Basics

Scripting Languages

• A ‚script‛ is a collection of program or sequence of instructions that is

interpreted or carried out by another program rather than by the computer

processor. Two types of scripting languages are

– Client-side Scripting Language

– Server-side Scripting Language

• In server-side scripting, (such as PHP, ASP) the script is processed by the

server Like: Apache, ColdFusion, ISAPI and Microsoft's IIS on Windows.

• Client-side scripting such as JavaScript runs on the web browser.

Page 7: ASP.NET Basics

Generate HTML

Get index.aspx13

4

pass index.aspx to .netframework

5

WebServer

Index.aspx in interpreted HTMl form

Browser

2Get index.aspx from hard disk

How IIS takes requests and processes it

.NET

ASP.NET is a server side scripting language because the

code runs on the server and returns the result in html form

to the client browser

Page 8: ASP.NET Basics

Generate HTML

Get index.aspx13

4

pass index.aspx to .netframework

5

WebServer

Index.aspx in interpreted HTMl form

Browser

2Get index.aspx from hard disk

How IIS takes requests and processes it

.NET

Client side scripting is something where code runs on client

side(browser). For eg checking whether a HTML text field contains data or not can be done by running

a script from browser itself

Page 9: ASP.NET Basics

Microsoft Visual Studio•Microsoft Visual Studio is an IDE (Integrated DevelopmentEnvironment) on which .net applications can be createdeasily.

•It is a powerful tool that can expose the features of the.net framework to make the developers work easier

•Applications can be run from within the IDE and hasinbuilt compiler and debugger (no need to install thirdparty servers like WAMP, XAMPP etc. to run the applicationas in case of PHP)

•It exposes a rich design based interface to manage anyaspect of a .net project from design to deployment

Page 10: ASP.NET Basics

Creating an asp.net page

Page 11: ASP.NET Basics

Create a new asp.net website projectGo to start > all programs > microsoft visual studio 2008 (or whatever version

you have installed)

Page 12: ASP.NET Basics

Create a new asp.net website project

Click on file > new web site

Page 13: ASP.NET Basics

Create a new asp.net website project(for C# select the language as Visual C#)

Page 14: ASP.NET Basics

Create a new asp.net website project

Page 15: ASP.NET Basics

Create a new asp.net website project

Page 16: ASP.NET Basics

Create a new asp.net website project

Page 17: ASP.NET Basics

Create a new asp.net website project

Page 18: ASP.NET Basics

Create a new asp.net website project• Double click on the button control you just created

Page 19: ASP.NET Basics

Create a new asp.net website project• You can also add handlers for other events of the button

Page 20: ASP.NET Basics

Adding connection string to the web.config file

Page 21: ASP.NET Basics

Adding a new item to the project

Page 22: ASP.NET Basics

Adding a new item to the project

Page 23: ASP.NET Basics

Running the project

Page 24: ASP.NET Basics

Getting started with ASP.NET programming

Page 25: ASP.NET Basics

.Net Controls, Events and Event handlers

• .net Controls – There are no. of controls those are available in the toolbox inside visual

studio. Eg. Button, textbox, dropdownlist etc.

Each control represents a class extended fromthe System.Web.UI.WebControls class

Each control has events, properties and methods associated with it

Each control in an aspx page is identified by a unique id, no two controlson the same page can share an id

• Events Events happen when the user does some sort of action on a control eg.

Click for a button, changing the text for a textbox etc.

• Event Handlers Event handlers are code blocks that will be executed when events are

raised

Page 26: ASP.NET Basics

.Net Controls, Events and Event handlers (continuation)

In plain words an event handler for the click of a button that has an id“btn_a” will tell the server like

“Do this on the click of btn_a”

There are no. of event handlers associated with each type of .net control

Custom event handlers can also be registered for certain controls.

Page 27: ASP.NET Basics

Commenting

// comment single line

/* comment

multiple lines */

C#

//comment single line

/* Comment

multiple Lines*/

VB.NET

‘comment single line

/* Comment

multiple Lines*/

C ASP.NET

Page 28: ASP.NET Basics

Data Types

Page 29: ASP.NET Basics

Data types

• ASP.NET supports many different data types such as

o Integer

o String

o Double

o Boolean

o Datetime

o Arrays

Page 30: ASP.NET Basics

Type Casting

Response.write ((0.1 + 0.7) * 10); //Outputs 8

Response.write ([cint] ((0.1 + 0.7) * 10)); //Outputs 7

• This happens because the result of this simple arithmetic

expression is stored internally as 7.999999 instead of 8; when the

value is converted to integer, ASP.NET simply truncates away the

fractional part, resulting in a rather significant error (12.5%, to be

exact).

Page 31: ASP.NET Basics

Variables

Page 32: ASP.NET Basics

Declaring a variable

//Declaring a variable

Int a=10;

Char c=‘a’;

Float f=1.12;

C#

String a; //variable without initialisation

String a = ‚Hello‛; //variable with

initialisation

VB.NET

Dim a as string //variable without

initialisation

dim a as string = ‚Hello‛ //variable with

initialisation

C ASP.NET

Page 33: ASP.NET Basics

Variables

• Variables are temporary storage containers.

• In ASP.NET, a variable can contain any type of data, such as, for example,

strings, integers, floating numbers, objects and arrays provided the variable

must be declared in that datatype.

• As in case of PHP which is loosely typed, ASP.NET will not implicitly

change the type of a variable as needed. ASP.NET languages are strongly

typed, like C and Java, where variables can only contain one type of data

throughout their existence.

Page 34: ASP.NET Basics

Constants

Page 35: ASP.NET Basics

Declaring a constant

//Declaring a constant using ‘const’

const int a=10;

int const a=10;

//Declaring a constant using ‘#define’

#define TRUE 1

#define FALSE 0

#define NAME_SIZE 20

C#

Const String a = ‚Hello‛;

VB.NET

const a as string = ‚Hello‛

C ASP.NET

Page 36: ASP.NET Basics

Constants

• Conversely to variables, constants are meant for defining immutable values.

• Compile-time and Run-time ConstantsA compile-time constant is computed at the time the code is compiled, while a run-time constant can only be computed while the application is running. A compile-time constant will have the same value each time an application runs, while a run-time constant may change each time.

Page 37: ASP.NET Basics

Control structures

Page 38: ASP.NET Basics

Control Structures

Conditional Control Structures

• If

• If else

• Switch

Loops

For

While

Do while

Other

• Break

• Continue

‚Exactly the same as on left side‛

+

• return

• go to

C ASP.NET

Page 39: ASP.NET Basics

functions

Page 40: ASP.NET Basics

Functions

Int findSum(int a,int b)

{

Int c;

c=a+b;

Return c

}

findSum(10,15);

function findSum(byVal a as

integer,byVal b as integer)

Dim c as integer

c=a+b

Return c

End function

Dim d as integer = findSum(10,15)

Response.write(d) ‘output : 25

C ASP.NET (vb.net example)

Page 41: ASP.NET Basics

Functions

public int findSum(int a, int b) {

int c;

c = (a + b);

return c;

}

private int d = findSum(10, 15);

Response.write(d); //output : 25

ASP.NET (C# example)

Page 42: ASP.NET Basics

Function arguments

• You can define any number of arguments to an ASP.NET function.

• Arguments can be passed by reference(byRef) or by value (byValue)

o byRef - When an argument is passed by reference, the called procedure can change the value of the variable. The change persists after the procedure is called

o byValue - When an argument is passed by value, any changes that the called procedure makes to the value of the variable do not persist after the procedure is called.

Page 43: ASP.NET Basics

Function return type• The function return type can also be defined in the function

declaration – VB.net

Function findSum(byval a as integer, byval b as integer) as integer

Dim c as integer

c=a+b

Return c

End function

– C#

public int findSum(int a, int b) {int c;c = (a + b);return c;

}

Page 44: ASP.NET Basics

Function return type• A function without a return value is called as a sub routine

and is defined as follows in ASP.NET– Vb.net

private sub findSum(byval a as integer, byval b as integer)

Dim c as integer

c=a+b

End sub

– C#

Protected void findSum(int a, int b)

{

int c;

c=a+b;

}

Page 45: ASP.NET Basics

Strings

Char a*+=‚Baabtra‛;

Printf(‚Hello %s‛,a);

Dim a as string =‚Baabtra‛

Response.write( ‚Hello ‛ + a) //Output:

Hello baabtra

Response.write( ‚Hello + a‛) //Output:

Hello + a

‚Strings will be continued in coming

chapters‛

C ASP.NET

Page 46: ASP.NET Basics

Arrays

Indexed Array

int a[]={1,2,3,4,5,6,7,8,9,10};

for(i=0;i<10;i++)

Printf(‚%d‛,a[i]);

‚Array will be continued in coming

chapters‛

Declaring an array of strings

Vb.net

Dim arrStr as string() //without

initialisation

Dim arrStr as string() = (‚a‛, ‚b‛)

//with initialisation

C#

string[] arrStr ;//without

initialisation

string[] arrStr = new string[] {

"one", "two", "three" } //with

initialisation

C ASP.NET

Page 47: ASP.NET Basics

Operators

Page 48: ASP.NET Basics

Operators

C

Arithmetic Operators

+, ++, -, --, *, /, %

Comparison Operators

==, !=, ===, !==, >, >=, < , <=

Logical Operators

&&, ||, !

Assignment Operators

=, +=, -=, *=, /=, %=

ASP.NET

Arithmetic Operators

Operator

description Example Result

+ Addition myNum = 3 + 4 7

- Subtraction myNum = 4 - 3 1

* Multiplication myNum = 3 * 4 12

/ Division myNum = 12 /4 3

^ Exponential myNum = 2 ^ 4 16

Mod Modulus myNum = 23 Mod 10

3

- Negation myNum = -10 -10

\ Integer Division

myNum = 9 \ 3 3

Page 49: ASP.NET Basics

Comparison operators

Operator description Example Result

= Equal to 3 = 4 false

< Less than 4 < 3 false

> Greater than 4 > 3 true

<= Less than or equal to 4 <= 4 true

>= Greater than or equal to 4 >= 5 false

<> / != Not equal to 4 <> 3 / 4 != 3 true

Page 50: ASP.NET Basics

Logical operators

Operator description Example Result

And / && Both Must be TRUE True And False false

Or / || One Must be TRUE True Or False true

Not / ! Flips Truth Value Not true false

String operators

Operator description Example Result

& / +String Concatenation string4 = “Hello" & " world"

string4 = “Hello world"

Page 51: ASP.NET Basics

• Bitwise operators allow you to manipulate bits of data. All these operators are designed to work only on integer numbers—therefore, the interpreter will attempt to convert their operands to integers before executing them.

Bitwise Operators

AND Bitwise AND. The result of the operation will be a value whose bits are set if they are set in both operands, and unset otherwise.

OR Bitwise OR. The result of the operation will be a value whose bits are set if they are set in either operand (or both), and unset otherwise.

XOR Bitwise XOR (exclusive OR). The result of the operation will be a value whose bits are set if they are set in either operand, and unset otherwise.

NOT Bitwise NOT. inverts each bit in a sequence of bits. A 0 becomes a 1, and a 1 becomes a 0.

<< Bitwise left shift. This operation shifts the left-hand operand’s bits to the left by a number of positions equal to the right operand, inserting unset bits in the shifted positions

>> Bitwise right shift. This operation shifts the left-hand operand’s bits to the right by a number of positions equal to the right operand, inserting unset bits in the shifted positions.

Page 52: ASP.NET Basics

End of day 1

Page 53: ASP.NET Basics

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 54: ASP.NET Basics

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]