70
Web Design & Programming <% ASP %> Mr. Baha & Dr.Husam Osta 2014

Web Design & Programming

  • Upload
    gina

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Web Design & Programming. Mr. Baha & Dr.Husam Osta 2014. Outlines. What is ASP? Internet Information Services How Does ASP Differ from HTML? What can ASP do for you ? ASP Advantages & Technology ASP Basic Syntax Rules ASP Variables ASP Time based / Date & Time Forms - PowerPoint PPT Presentation

Citation preview

Page 1: Web Design & Programming

Web Design & Programming

<% ASP %>Mr. Baha & Dr.Husam Osta

2014

Page 2: Web Design & Programming

Outlines What is ASP? Internet Information Services How Does ASP Differ from HTML? What can ASP do for you? ASP Advantages & Technology ASP Basic Syntax Rules

◦ ASP Variables◦ ASP Time based / Date & Time◦ Forms◦ Loops◦ Array◦ Radio / Check box◦ Sending emails using ASP◦ Application Object Methods◦ Examples <<<<◦ Functions◦ Passing Variables in a URL◦ Passing variables with forms◦ Sessions

Page 3: Web Design & Programming

What is ASP? ASP stands for Active Server Pages ,or classical ASP

◦ ASP is Microsoft's first server side scripting engine ◦ It enables you to make dynamic and interactive web pages.◦ ASP is a program that runs inside

Internet Information Services (IIS)

An ASP file can contain ◦ Text, HTML, XML, and scripts

Scripts in an ASP file are executed on the server The default scripting language used for ASP is VBScript, or

others like JScript◦ An ASP file has the file extension “.asp”

Page 4: Web Design & Programming

Internet Information Services (IIS)

Internet Information Services is an extensible web server created by Microsoft for use with Windows family. 

IIS supports:◦ HTTP, HTTPS, FTP, FTPS, SMTP and NNTP.

It is an integral part of Windows family since Windows NT 4.0, though it may be absent from some edition (e.g. Windows XP Home edition).

IIS is not turned on by default when Windows is installed.

Page 5: Web Design & Programming

How Does ASP Differ from HTML?

When a browser requests an HTML file1. the server returns the file

Page 6: Web Design & Programming

How Does ASP Differ from HTML? When a browser requests an ASP file

1. IIS passes the request to the ASP engine. 2. The ASP engine reads the ASP file, line by line,

and executes the scripts in the file. 3. Finally, the ASP file is returned to the browser as

plain HTML

Page 7: Web Design & Programming

What can ASP do for you?1. Dynamically edit, change, or add any content of

a Web page2. Respond to user queries or data submitted from

HTML forms3. Access any data or databases and return the

results to a browser4. Customize a Web page to make it more useful

for individual users

Page 8: Web Design & Programming

Advantages of ASP …

1. The advantages over other technologies, are 1. Simplicity2. speed

2. Provide security since the code cannot be viewed from the browser

3. Clever ASP programming can minimize the network traffic

Page 9: Web Design & Programming

ASP.Net Technology It is a unified Web development model It includes services necessary to build

enterprise-class web applications with minimum of coding.

This technology is developed under the .Net framework that is provided in the visual studio platform

Page 10: Web Design & Programming

ASP Basic Syntax Rules An ASP file normally contains HTML tags, just like an

HTML file. An ASP file can also contain server scripts,

surrounded by the delimiters <% and %>. The command response.write is used to write output

to a browser.

Example<html>

<body><% response.write(“My first ASP script!”) %></body>

</html>

Page 11: Web Design & Programming

ASP Basic Syntax Rules

ASP can format text with HTML tags

<html><body>

<% response.write("<h2>You can use HTML tags to format the text!</h2>") %>

<%response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")%>

</body></html>

Page 12: Web Design & Programming

Your first ASP page

ASP

HLTML

Page 13: Web Design & Programming

ASP Variables Variables are used to store information. The example demonstrates

◦ how to declare a variable, assign a value to it, and use the value in a text.

<!DOCTYPE html><html><body>

<%dim namename="Donald Duck"response.write("My name is: " & name)%>

</body></html>

Page 14: Web Design & Programming

Time-basedTime-based greeting using VBScriptThis it will display a different message to the user depending on the time on the server.

Page 15: Web Design & Programming

<html><body>

<%dim hh=hour(now())

response.write("<p>" & now())response.write("</p>")

If h<12 then   response.write("Good Morning!")else   response.write("Good day!")end if%>

</body></html>

Page 16: Web Design & Programming

Working with time and dates

Page 17: Web Design & Programming

FormatDateTime(Date[, NamedFormat]) The NamedFormat argument may take the

following values:◦ vbLongDate◦ vbShortDate◦ vbLongTime◦ vbShortTime

Formatting time and dates

Page 18: Web Design & Programming

ASP – Date & Time <!DOCTYPE html>

<html><body>

<%response.write(FormatDateTime(date(),vbgeneraldate))response.write("<br>")response.write(FormatDateTime(date(),vblongdate))response.write("<br>")response.write(FormatDateTime(date(),vbshortdate))response.write("<br>")response.write(FormatDateTime(now(),vblongtime))response.write("<br>")response.write(FormatDateTime(now(),vbshorttime))%>

<p>Syntax for FormatDateTime: FormatDateTime(date,namedformat).

</p>

</body></html>

Page 19: Web Design & Programming

YearReturns the current year from a date - with today's date, it returns: 2014

MonthReturns the current month from a date - with today's date, it returns: 1

DayReturns the current day of the month from a date - with today's date, it returns:29

HourReturns the current hour from a time - with the current time, it returns: 8

MinuteReturns the current minute from a time - with the current time, it returns: 10

SecondReturns the current second from a time - with the current time, it returns: 28

Functions for time and dates

Page 20: Web Design & Programming

Weekday Returns the current day of the week from a date

NOTE: this function has to be called with the argument "the first day of the week" (eg. Monday or Sunday) as well- like this: Weekday(Now,vbMonday)

Weekday function

Page 21: Web Design & Programming

ASP Forms Get

◦ A form with method="get“ is how to interact with the user, with >>>> Request.QueryString command.

◦ Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

Post◦ A form with method="post“ is how to interact with the user, with >>>>

Request.Form command.◦ Information sent from a form with the POST method is invisible to others

and has no limits on the amount of information to send. Radio buttons

◦ A form with radio buttons is how to interact with the user, through radio buttons, with >>>> Request.Form command.

Page 22: Web Design & Programming

ASP Forms – Get / Post method

<html><body>

<form action="demo_reqquery.asp" method="get">Your name: <input type="text" name="fname" size="20" /><input type="submit" value="Submit" /></form>

<%dim fnamefname=Request.QueryString("fname") (Or use this (request.form))If fname<>"" Then      Response.Write("Hello " & fname & "!<br>")      Response.Write("How are you today?")End If%></body></html>

Page 23: Web Design & Programming

For Initializion To Expressions Statement Next

Loops “For loop”

Page 24: Web Design & Programming

Loops “For loop example”For Initializion To Expressions Statement Next

Page 25: Web Design & Programming

Do {While | Until} condition Statement Loop

OR

Do Statement Loop {While | Until} condition

Do ... LoopDo Statement Loop {While | Until} condition

Page 26: Web Design & Programming

Example of Loop<!DOCTYPE html><html><body><%dim i for i=1 to 6response.write("<h" & i & ">Heading " & i & "</h" & i & ">")next%></body></html>

Page 27: Web Design & Programming

Do .. Loop example

Page 28: Web Design & Programming

Output

Page 29: Web Design & Programming

Loops within loops

Page 30: Web Design & Programming

If condition Then statement Else statement End If

Conditions : If ..Then..Else

Page 31: Web Design & Programming

= Equals< Less than> Greater than<= Less than or equal to> = Greater than or equal to<> Not equal toANDORNOT

Logical Operators

Page 32: Web Design & Programming

Example “AND”

Page 33: Web Design & Programming

If ... Then ... ElseIf ... Else

Page 34: Web Design & Programming

Select Case Expression Case 1 statement Case 2 statement Case Else statement End Select

Select ... Case

Page 35: Web Design & Programming

Comments

Page 36: Web Design & Programming

Declare an Array Arrays are used to store a series of related data items. The example demonstrates how to declare an array that stores names.

<!DOCTYPE html><html><body>

<%Dim famname(5),ifamname(0) = "Jan Egil"famname(1) = "Tove"famname(2) = "Hege"famname(3) = "Stale"famname(4) = "Kai Jim"famname(5) = "Borge"

For i = 0 to 5      response.write(famname(i) & "<br>")Next%>

</body></html>

Page 37: Web Design & Programming

An array is a set of indexed elements where each has its own, unique identification number.

Ex:

<% Dim fruitlist, arrFruits fruitlist = "apples, pears, bananas, oranges, lemons" arrFruits = Split(fruitlist,",") %>

Arrays

Page 38: Web Design & Programming

<%Dim fruitlist, arrFruitsfruitlist = "apples, pears, bananas, oranges, lemons" arrFruits = Split(fruitlist,",") Response.Write"<p>The list of fruits:</p>Response.Write"<ul>“Response.Write"<li>" & arrFruits(0) & "</li>“Response.Write"<li>" & arrFruits(1) & "</li>" Response.Write"<li>" & arrFruits(2) & "</li>“Response.Write"<li>" & arrFruits(3) & "</li>“Response.Write"<li>" & arrFruits(4) & "</li>“Response.Write "</ul>" %>

Array Ex:

Page 39: Web Design & Programming

http://html.net/tutorials/asp/lesson8.asp

Arrays

Page 40: Web Design & Programming

Radio buttons<html><% dim carscars=Request.Form("cars")%>

<body><form action="demo_radiob.asp" method="post"><p>Please select your favorite car:</p>

<input type="radio" name="cars"<%if cars="Volvo" then Response.Write("checked")%> value="Volvo">Volvo</input><br><input type="radio" name="cars"<%if cars=“Benz" then Response.Write("checked")%>

value=“Benz">Benz</input><br><input type="radio" name="cars"<%if cars="BMW" then Response.Write("checked")%> value="BMW">BMW</input><br><br>

<input type="submit" value="Submit" /></form><% if cars<>"" then

Response.Write("<p>Your favorite car is: " & cars & "</p>")end if%></body></html>

Page 41: Web Design & Programming

Check box<html><body><% fruits=Request.Form("fruits")%><form action="demo_checkboxes.asp" method="post"><p>Which of these fruits do you prefer:</p><input type="checkbox" name="fruits" value="Apples“

<% if instr(fruits,"Apple") then Response.Write("checked")%> Apple> <br><input type="checkbox" name="fruits" value="Oranges“

<%if instr(fruits,"Oranges") then Response.Write("checked")%> Orange> <br><input type="checkbox" name="fruits" value="Bananas“

<%if instr(fruits,"Banana") then Response.Write("checked")%> Banana> <br><input type="submit" value="Submit"></form>

<% if fruits<>"" then %><p>You like: <%Response.Write(fruits)%></p><% end if %>

</body></html>

Page 42: Web Design & Programming

ASP Sending e-mail with CDOSYS CDOSYS is a built-in component in ASP. This

component is used to send e-mails with ASP. CDO (Collaboration Data Objects) is a

Microsoft technology that is designed to simplify the creation of messaging applications.

CDOSYS is a built-in component in ASP.

Page 43: Web Design & Programming

Examples – ASP Sending e-mail

Page 44: Web Design & Programming

Application objects methods

Property Description

CodePage Specifies the character set that will be used when displaying dynamic content

LCID Sets or returns an integer that specifies a location or region. Contents like date, time, and currency will be displayed according to that location or region

SessionID Returns a unique id for each user. The unique id is generated by the server

Timeout Sets or returns the timeout period (in minutes) for the Session object in this application

Page 45: Web Design & Programming

Application objects methods

<html><body>

<%response.write("The default LCID for this page is: " &Session.LCID&

"<br>")response.Write(" Session ID is " &Session.SessionID)response.write("</p>")response.write("Default Timeout is: " &Session.Timeout& " minutes.")%>

</body></html>

Page 46: Web Design & Programming

In Vbscript – find average

<html><body>

<script type="text/vbscript">dim number1 dim number2 dim answer number1 = cInt (InputBox("Type the first number") ) number2 = cInt (InputBox("Type the second number")) answer = (number1 + number2)/2 msgBox " Average of " number1 & number2 & " = " & answer

</script></body></html>

Page 47: Web Design & Programming

In Vbscript – find smaller #

<html><body>

<script type="text/vbscript">dim num1 dim num2 num1 = cInt (InputBox("Type the first number") ) num2 = cInt (InputBox("Type the second number")) If num1 < num2 then msgBox " Minimum is " & num1If num2 < num1 then msgBox " Minimum is " & num2</script>

</body></html>

Page 48: Web Design & Programming

ASP text box types !

The TextMode property accepts the following three values: SingleLine— displays a single-line input field. MultiLine— displays a multi-line input field. Password— displays a single-line input field in which the text is

hidden.

A basic TextBox:<asp:TextBox id="tb1" runat="server" />A password TextBox:<asp:TextBox id="tb2" TextMode="password" runat="server" />A TextBox with text:<asp:TextBox id="tb4" Text="Hello World!" runat="server" />A multiline TextBox:<asp:TextBox id="tb3" TextMode="multiline" runat="server" />

Page 49: Web Design & Programming

Find the output example !<form runat="server">

<asp:DropDownList id="drop1" runat="server"><asp:ListItem>fall</asp:ListItem><asp:ListItem>spring</asp:ListItem><asp:ListItem>summer</asp:ListItem></asp:DropDownList>

<asp:Button Text="Submit" OnClick="submit" runat="server"/>

<p><asp:label id="mess" runat="server"/></p>

</form>

Page 50: Web Design & Programming

Find the output example !

<form runat="server">

Enter your name:

<asp:TextBox id="txt1" runat="server" /><asp:password id="pass1" runat="server" />

<p><asp:Label id="lbl1" runat="server" /></p>

<asp:Calendar runat="server" />

<asp:Button OnClick="submit" Text="Submit" runat="server" />

</form>

Page 51: Web Design & Programming

• Use subroutines and functions in ASP pages in order to economise on code.. i.e. to enable ‘modules’ of code to be used and reused

• Subroutines define a series of steps that accomplishes some task, but does not usually return a value

• Functions accomplish a series of steps And usually return a single value

ASP: Subroutines and functions

Page 52: Web Design & Programming

ASP : Subroutines - example<html><head><script language="VBScript" runat="Server">sub calculate(num1,num2) response.write(num1*num2)end sub</script></head><body>The product of the two numbers entered is:<p><% dim x dim y x = 5 y = 7%> Result: <%call calculate(x,y)%></p></body>

Subroutine always starts with Sub, ends with End Sub

use “Call”

Arguments passed to subroutine (num1, num2)

Page 53: Web Design & Programming

ASP : Function example<html><head><script language="VBScript" runat="Server">Function retailPrice(price) dim tax tax=price*.09 retailPrice =price+taxend Function</script><body><% dim y y = retailPrice(100) Response.write(“The retail price includig tax " &y)%>

</body></html>

Function always startswith Function and ends with End function

Make sure that output is explicitly named in the function

Retrieves a netprice and calculates the retail price, using the function

Page 54: Web Design & Programming

A function process is to use inputs and return an output or value. 

It can be useful if, for example, you have a wide range of data you have processed or if you have calculations or routines in other ways that must be performed many times.

Syntax:◦ Function Name(list of parameters)

Statement◦ End Function

Functions

Page 55: Web Design & Programming

<% Function AddAll(number1, number2, number3) AddAll = number1 + number2 + number3End Function Response.Write"123 + 654 + 9 equals" & AddAll(123,654,9) %>

Functions Ex1:

Page 56: Web Design & Programming

Functions Ex2:

Page 57: Web Design & Programming

Maybe you have wondered why some URLs looks something like this:

http://html.net/page.asp?id=1254

Why is there a question mark after the page name?◦ The answer is that the characters after the question mark

are an HTTP query string. ◦ An HTTP query string can contain both variables and their

values. ◦ In the example above, the HTTP query string contains a

variable named id, with the value 1254.

Passing Variables in a URL

Page 58: Web Design & Programming

<html> <head><title>QueryString</title> </head>

<body> <% ' Variable name and age is found in the URL Response.Write "<h1>Hello " & Request.QueryString("name") & "</h1>"Response.Write "<h1>You are " & Request.QueryString ("age") & " years old</h1>“%></body></html>

http://html.net/tutorials/asp/lesson10_ex2.asp?name=Joe&age=24

Page 59: Web Design & Programming

When you code a form, there are two particular important attributes: ◦Action ◦Method

Passing variables with forms

Page 60: Web Design & Programming

When you code a form, there are two particular important attributes: ◦Action

Is used to enter the URL where the form is submitted. In this case it would be the ASP file that you want to

handle the input.◦Method

Passing variables with forms

Page 61: Web Design & Programming

When you code a form, there are two particular important attributes: ◦Action◦Method

Can either have the value "post" or "get" which are two different methods to pass data.

Using "get", the data is sent through the URL, Using "post", the data is sent as a block of data through

standard input service (STDIN)

Passing variables with forms

Page 62: Web Design & Programming

The page that contains the form doesn't need to be an ASP file (but it can be)

<html><head><title>Form</title></head>

<body><h1>Enter your name</h1> <form method="post" action="handler.asp"> <input type="text" name="username"><input type="submit"> </form></body></html>

An HTML page with a form

Page 63: Web Design & Programming

<html> <head><title>Form</title></head><body> <form method="post" action="handler.asp"> <p>What is your name:</p><input type="text" name="username"><p>What is your favorite color: <input type="radio" name="favoritecolor" value="r" /> Red <input type="radio" name="favoritecolor" value="g" /> Green <input type="radio" name="favoritecolor" value="b" /> Blue </p> <input type="submit" value="Submit" /> </form> </body> </html>

User input and conditions

example :http://html.net/tutorials/asp/lesson11_ex2.asp

Page 64: Web Design & Programming

The Result of the previous Form

Page 65: Web Design & Programming

<% strHeading = "<h1>Hello " & Request.Form("username") & "</h1>" Select Case Request.Form ("favoritecolor")Case "r" strBackgroundColor = "rgb(255,0,0)" Case "g" strBackgroundColor = "rgb(0255.0)" Case "b" strBackgroundColor = "rgb(0,0,255)" Case Else strBackgroundColor = "rgb(255,255,255)" End Select %> <html><head><title>Form</title> </head> <body style="background: <% = strBackgroundColor %>;"><% Response.Write strHeading %></body></html>

Example

Page 66: Web Design & Programming

<% Session ("StartTime") = Now %> Thereby, a session was started. As described above, each session is given an ID by the server.

<% Response.Write Session("StartTime") %>

Session have a default duration of 20 minutes Can be changed using the following code:

<% Session.Timeout = 60 %>

If you want to stop a session, it can always be killed in this way:<% Session.Abandon %>

Sessions

Page 67: Web Design & Programming

<html><head> <title>Login</title></head><body> <form method="post" action="login.asp"><p>Username: <input type="text" name="username" /></p><p>Password: <input type="text" name="password" /></p><p><input type="submit" value="Let me in" /></p> </form></body></html>

Login system with sessions

Example:http://html.net/tutorials/asp/lesson12_ex1.asp

Page 68: Web Design & Programming

<html><head><title>Login</title> </head>

<body> <% ' Check if username and password are correct If Request.Form("username") = "asp" AND Request.Form("password") = "asp" Then ' If correct, we set the session to YESSession("login") = "YES" Session.Timeout = 30 Response.Write "<h1> You are now logged in</h1>" Response.Write "<p><a href='document.asp'>Link to protected file</a></p>“Else 'If not correct, we set the session to NOSession("login") = "NO" Session.Timeout = 30 Response.Write "<h1>You are NOT logged in</h1>" Response.Write "<p><a href='document.asp'>Link to protected file</a></p>" End If %> </body> </html>

Page 69: Web Design & Programming

<% ' If the user is not logged in ' send him/her to the login form If Session ("login") <>"YES" Then Response.Redirect “form.asp”End If %>

<html> <head> <title>Login</title> </head>

<body> <h1>This document is protected</h1> <p>You can only see it if you are logged in.</p>

</body> </html>

Page 70: Web Design & Programming

<% Thank You %>