Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims...

17
Mark Dixon Page 1 20 – Modular Design in ASP
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims...

Page 1: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 1

20 – Modular Design in ASP

Page 2: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 2

Session Aims & Objectives• Aims

– Highlight modular design techniques in ASP

• Objectives,by end of this week’s sessions, you should be able to:

– Use procedures, functions, parameters, and modules (shared VB script files) in ASP

Page 3: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 3

Example: Country (database)ID Name Population Birth rate Death rate Land Mass ContinentID1 UK 60776238 10.67 10.09 241590 12 Spain 40448191 9.98 9.81 499542 13 Germany 82400996 8.2 10.71 349223 14 Egypt 80335036 22.53 5.11 995450 25 Kenya 36913721 38.94 10.95 569250 26 China 1321851888 13.45 7 9326410 3

ID Name1 Europe2 Africa3 Asia4 Australia5 North America6 South 7 Antarctica

Page 4: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 4

Example Country (user interface)

Page 5: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 5

Example: Countries (code v0)

<html> <head><title>Countries</title></head> <body> <div style="background-color: LightGreen; text-align: center;"> <b>Countries of the World</b> </div> <% Const cs = "…" Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Select * FROM [Country]", cs) Do Until rs.EOF() Response.Write(rs.Fields("Name").value & "<br>") rs.MoveNext() Loop rs.Close() rs = Nothing %> </body></html>

Countries.aspx• HTML and ASP

mixed together– messy

Page 6: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 6

Example: Countries (code v1)

<script language="vbscript" runat="server"> Const cs = "…"

Sub DisplayCountries() Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Select * FROM [Country]", cs) Do Until rs.EOF() Response.Write(rs.Fields("Name").value & "<br>") rs.MoveNext() Loop rs.Close() rs = Nothing End Sub</script>

<html> <head><title>Countries</title></head> <body> <div style="background-color: LightGreen; text-align: center;"> <b>Countries of the World</b> </div> <%DisplayCountries()%> </body></html>

Countries.aspx• HTML and ASP

separated

Page 7: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 7

Example: Countries (v2)• Add facility to order list:

Page 8: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 8

Example: Countries (code v2)<script language="vbscript" runat="server"> Const cs = "…" Sub DisplayCountries(sql As String) Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Select * FROM [Country] " & sql, cs) Do Until rs.EOF() Response.Write(rs.Fields("Name").value & "<br>") rs.MoveNext() Loop rs.Close() rs = Nothing End Sub</script>

<html> <head><title>Countries</title></head> <body> <div style="background-color: LightGreen; text-align: center;"> <b>Countries of the World</b> </div> <form action="Countries.aspx" method="post"> <input name="btnOrder" type="submit" value="Order" /> </form> <% If Request.Form("btnOrder") <> "" Then DisplayCountries(" ORDER BY [Name]") Else DisplayCountries("") End If %> </body></html>

Countries.aspx parameters allow same procedure to do different things

file getting long

Page 9: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 9

Adding VB Script file• Right click project

• click 'add new item'

Page 10: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 10

Example: Countries (code v3)

<script language="vbscript" runat="server" src="Countries.vbs" /><html> <head><title>Countries</title></head> <body> <div style="background-color: LightGreen; text-align: center;"> <b>Countries of the World</b> </div> <form action="Countries.aspx" method="post"> <input name="btnOrder" type="submit" value="Order" /> </form> <% If Request.Form("btnOrder") <> "" Then DisplayCountries(" ORDER BY [Name]") Else DisplayCountries("") End If %> </body></html>

Countries.aspx

• split code and html into 2 files

Const cs = "…"

Sub DisplayCountries(sql As String) Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Select * FROM [Country] " & sql, cs) Do Until rs.EOF() Response.Write(rs.Fields("Name").value & "<br>") rs.MoveNext() Loop rs.Close() rs = NothingEnd Sub

Countries.vbs

Page 11: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 11

Example: People DatabasePersonPersonID

Surname

Forenames

Gender

Phone eMail

1 Dixon Mark Yes 01752 232556

[email protected]

2 Smith John Yes 01752 111111

[email protected]

3 Jones Sally No 01752 888888

[email protected]

4 Bloggs Fred Yes 01752 123123

[email protected]

5 Anderson Genny No 01752 987987

[email protected]

6 Smith Bob Yes 01752 898898

[email protected]

Page 12: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 12

Example: People (design)

Page 13: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 13

Example: People (code v0)

• 2 pages• duplicate same code

<script language="vbscript" runat="server"> Const cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\databases\People.mdb;Persist Security Info=False" Const adOpenDynamic = 3

Sub DisplayMenu() Response.Write("<center>") Response.Write("<a href='People.aspx'>People</a> | ") Response.Write("<a href='Person.aspx'>Person</a>") Response.Write("</center><br><br>") End Sub

Function PersonName(ByVal r As Object) As String PersonName = r.Fields("Forenames").Value & " " & r.Fields("Surname").Value End Function</script>

<html> <head><title>People</title></head> <body> <% DisplayMenu Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) Do Until rs.EOF() Response.Write(PersonName(rs) & "<br>") rs.MoveNext() Loop rs.Close() rs = Nothing %> </body></html>

<script language="vbscript" runat="server"> Const cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\databases\People.mdb;Persist Security Info=False" Const adOpenDynamic = 3

Sub DisplayMenu() Response.Write("<center>") Response.Write("<a href='People.aspx'>People</a> | ") Response.Write("<a href='Person.aspx'>Person</a>") Response.Write("</center><br><br>") End Sub

Function PersonName(ByVal r As Object) As String PersonName = r.Fields("Forenames").Value & " " & r.Fields("Surname").Value End Function</script> <html> <head><title>Person Page</title></head> <body> <% DisplayMenu Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs, adOpenDynamic) If Session("curID") <> "" Then rs.Find("[ID] = " & Session("curID")) If Request.Form("btnPrev") <> "" Then rs.MovePrevious() ElseIf Request.Form("btnNext") <> "" Then rs.MoveNext() End If End If Session("curID") = CStr(rs.Fields("ID").Value) Response.Write(PersonName(rs) & "<br>") rs.Close() rs = Nothing %> <form action="Person.aspx" method="post"> <input name="btnPrev" type="submit" value="Previous" /> <input name="btnNext" type="submit" value="Next" /> </form> </body></html>

Person.aspxPeople.aspx

Page 14: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 14

Example: People (code v1)

<script runat="server" src="_People.vbs"></script>

<html> <head><title>People</title></head> <body> <% DisplayMenu Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) Do Until rs.EOF() Response.Write(PersonName(rs) & "<br>") rs.MoveNext() Loop rs.Close() rs = Nothing %> </body></html>

Const cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb;Persist Security Info=False"Const adOpenDynamic = 3

Sub DisplayMenu() Response.Write("<center>") Response.Write("<a href='People.aspx'>People</a> ") Response.Write("<a href='Person.aspx'>Person</a>") Response.Write("</center><br><br>")End Sub

Function PersonName(r As Object) As String PersonName = r.Fields("Forenames").Value & " " & r.Fields("Surname").ValueEnd Function

<script runat="server" src="_People.vbs"></script>

<html> <head><title>Person Page</title></head> <body> <% DisplayMenu Dim rs As Object rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs, adOpenDynamic) If Session("curID") <> "" Then rs.Find("[ID] = " & Session("curID")) If Request.Form("btnPrev") <> "" Then rs.MovePrevious() ElseIf Request.Form("btnNext") <> "" Then rs.MoveNext() End If End If Session("curID") = CStr(rs.Fields("ID").Value) Response.Write(PersonName(rs) & "<br>") rs.Close() rs = Nothing %> <form action="Person.aspx" method="post"> <input name="btnPrev" type="submit" value="Previous" /> <input name="btnNext" type="submit" value="Next" /> </form> </body></html>

Person.aspx

People.aspx

_People.vbs

• 2 pages– share same

code

• change one place

Page 15: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 15

Tutorial Exercise: Countries• Task 1: Get the countries example (from the lecture) working.• Task 2: Modify your page to display more information about each

country.• Task 3: Add an unordered button to your page.• Task 4: Add an order by population button to your page

Page 16: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 16

Tutorial Exercise: People• Task 1: Get the people example from the lecture working.• Task 2: Modify your page to display more information about each

person.• Task 3: Modify your page so that the user can order the list of people.

Page 17: Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,

Mark Dixon Page 17

Tutorial Exercise: Assignment• Task 1: Use module (files) and procedures in your assignment.