20 Modular Design in ASP Mark Dixon Page

  • Slides: 17
Download presentation
20 – Modular Design in ASP Mark Dixon Page 1

20 – Modular Design in ASP Mark Dixon Page 1

Session Aims & Objectives • Aims – Highlight modular design techniques in ASP •

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 Mark Dixon Page 2

Example: Country (database) Mark Dixon Page 3

Example: Country (database) Mark Dixon Page 3

Example Country (user interface) Mark Dixon Page 4

Example Country (user interface) Mark Dixon Page 4

Example: Countries (code v 0) Countries. aspx <html> <head><title>Countries</title></head> <body> <div style="background-color: Light. Green;

Example: Countries (code v 0) Countries. aspx <html> <head><title>Countries</title></head> <body> <div style="background-color: Light. Green; text-align: center; "> <b>Countries of the World</b> </div> <% Const cs = "…" Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Select * FROM [Country]", cs) Do Until rs. EOF() Response. Write(rs. Fields("Name"). value & " ") rs. Move. Next() Loop rs. Close() rs = Nothing %> </body> </html> Mark Dixon • HTML and ASP mixed together – messy Page 5

Example: Countries (code v 1) Countries. aspx <script language="vbscript" runat="server"> Const cs = "…"

Example: Countries (code v 1) Countries. aspx <script language="vbscript" runat="server"> Const cs = "…" Sub Display. Countries() Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Select * FROM [Country]", cs) Do Until rs. EOF() Response. Write(rs. Fields("Name"). value & " ") rs. Move. Next() Loop rs. Close() rs = Nothing End Sub </script> • HTML and ASP separated <html> <head><title>Countries</title></head> <body> <div style="background-color: Light. Green; text-align: center; "> <b>Countries of the World</b> </div> <%Display. Countries()%> </body> </html> Mark Dixon Page 6

Example: Countries (v 2) • Add facility to order list: Mark Dixon Page 7

Example: Countries (v 2) • Add facility to order list: Mark Dixon Page 7

Example: Countries (code v 2) Countries. aspx <script language="vbscript" runat="server"> Const cs = "…"

Example: Countries (code v 2) Countries. aspx <script language="vbscript" runat="server"> Const cs = "…" Sub Display. Countries(sql As String) Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Select * FROM [Country] " & sql, cs) Do Until rs. EOF() Response. Write(rs. Fields("Name"). value & " ") rs. Move. Next() Loop rs. Close() rs = Nothing End Sub </script> J parameters allow same procedure to do different things L file getting long <html> <head><title>Countries</title></head> <body> <div style="background-color: Light. Green; text-align: center; "> <b>Countries of the World</b> </div> <form action="Countries. aspx" method="post"> <input name="btn. Order" type="submit" value="Order" /> </form> <% If Request. Form("btn. Order") <> "" Then Display. Countries(" ORDER BY [Name]") Else Display. Countries("") End If %> </body> </html> Mark Dixon Page 8

Adding VB Script file • Right click project • click 'add new item' Mark

Adding VB Script file • Right click project • click 'add new item' Mark Dixon Page 9

Example: Countries (code v 3) Countries. aspx Countries. vbs <script language="vbscript" runat="server" src='data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20415%20289%22%3E%3C/svg%3E' data-src="Countries. vbs"

Example: Countries (code v 3) Countries. aspx Countries. vbs <script language="vbscript" runat="server" src="Countries. vbs" Const/>cs = "…" <html> <head><title>Countries</title></head> Sub Display. Countries(sql As String) <body> Dim rs As Object <div style="background-color: Light. Green; text-align: center; "> rs = Create. Object("ADODB. Recordset") <b>Countries of the World</b> rs. Open("Select * FROM [Country] " & sql, cs) </div> Do Until rs. EOF() <form action="Countries. aspx" method="post"> Response. Write(rs. Fields("Name"). value & "<br <input name="btn. Order" type="submit" value="Order" /> rs. Move. Next() </form> Loop <% rs. Close() If Request. Form("btn. Order") <> "" Then rs = Nothing Display. Countries(" ORDER BY [Name]") End Sub Else Display. Countries("") End If %> </body> </html> • split code and html into 2 files Mark Dixon Page 10

Example: People Database Person. ID Surname Forenames Gender Phone Mark Dixon e. Mail 1

Example: People Database Person. ID Surname Forenames Gender Phone Mark Dixon e. Mail 1 Dixon Mark Yes 01752 232556 mark. dixon@plymouth. ac. u k 2 Smith John Yes 01752 111111 john. smith@john. smith. ac. u k 3 Jones Sally No 01752 888888 sally. jones@sally. jones. com 4 Bloggs Fred Yes 01752 123123 fred. bloggs@aaaaaa. com 5 Anderson Genny No 01752 987987 genny@bbbb. cccc. com 6 Smith Yes 01752 898898 bob. smith@bob-smith. com Bob Page 11

Example: People (design) Mark Dixon Page 12

Example: People (design) Mark Dixon Page 12

Example: People (code v 0) People. aspx <script language="vbscript" runat="server"> Person. aspx <script language="vbscript"

Example: People (code v 0) People. aspx <script language="vbscript" runat="server"> Person. aspx <script language="vbscript" runat="server"> Const cs = "Provider=Microsoft. Jet. OLEDB. 4. 0; Data Source=D: databasesPeople. mdb; Persist Security Const. Info=False" cs = "Provider=Microsoft. Jet. OLEDB. 4. 0; Data Source=D: databasesPeople. mdb; Persist Se Const ad. Open. Dynamic = 3 Sub Display. Menu() Response. Write("<center>") Response. Write("<a href='People. aspx'>People</a> | ") Response. Write("<a href='Person. aspx'>Person</a>") Response. Write("</center> ") End Sub Function Person. Name(By. Val r As Object) As String Person. Name = r. Fields("Forenames"). Value & " " & r. Fields("Surname"). Value End Function </script> <html> <head><title>People</title></head> <body> <% Display. Menu Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Person", cs) Do Until rs. EOF() Response. Write(Person. Name(rs) & " ") rs. Move. Next() Loop rs. Close() rs = Nothing %> </body> </html> <head><title>Person Page</title></head> <body> <% Display. Menu Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Person", cs, ad. Open. Dynamic) • 2 pages • duplicate same code Mark Dixon If Session("cur. ID") <> "" Then rs. Find("[ID] = " & Session("cur. ID")) If Request. Form("btn. Prev") <> "" Then rs. Move. Previous() Else. If Request. Form("btn. Next") <> "" Then rs. Move. Next() End If Session("cur. ID") = CStr(rs. Fields("ID"). Value) Response. Write(Person. Name(rs) & " ") rs. Close() rs = Nothing %> <form action="Person. aspx" method="post"> <input name="btn. Prev" type="submit" value="Previous" /> <input name="btn. Next" type="submit" value="Next" /> </form> </body> </html> Page 13

Example: People (code v 1) _People. vbs Const cs = "Provider=Microsoft. Jet. OLEDB. 4.

Example: People (code v 1) _People. vbs Const cs = "Provider=Microsoft. Jet. OLEDB. 4. 0; Data Source=D: People. mdb; Persist Security Info=False" Const ad. Open. Dynamic = 3 Sub Display. Menu() Response. Write("<center>") Response. Write("<a href='People. aspx'>People</a> ") Response. Write("<a href='Person. aspx'>Person</a>") Response. Write("</center> ") End Sub Person. aspx <script runat="server" src="_People. vbs"></script> Function Person. Name(r As Object) As String Person. Name = r. Fields("Forenames"). Value & " " & r. Fields("Surname"). Value End Function People. aspx • 2 pages – share same code • change one place Mark Dixon <script runat="server" src="_People. vbs"></script> <html> <head><title>People</title></head> <body> <% Display. Menu Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Person", cs) Do Until rs. EOF() Response. Write(Person. Name(rs) & " ") rs. Move. Next() Loop rs. Close() rs = Nothing %> </body> </html> <head><title>Person Page</title></head> <body> <% Display. Menu Dim rs As Object rs = Create. Object("ADODB. Recordset") rs. Open("Person", cs, ad. Open. Dynamic) If Session("cur. ID") <> "" Then rs. Find("[ID] = " & Session("cur. ID")) If Request. Form("btn. Prev") <> "" Then rs. Move. Previous() Else. If Request. Form("btn. Next") <> "" Then rs. Move. Next() End If Session("cur. ID") = CStr(rs. Fields("ID"). Value) Response. Write(Person. Name(rs) & " ") rs. Close() rs = Nothing %> <form action="Person. aspx" method="post"> <input name="btn. Prev" type="submit" value="Previous" /> <input name="btn. Next" type="submit" value="Next" /> </form> </body> </html> Page 14

Tutorial Exercise: Countries • Task 1: Get the countries example (from the lecture) working.

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 Mark Dixon Page 15

Tutorial Exercise: People • Task 1: Get the people example from the lecture working.

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. Mark Dixon Page 16

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

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