270 likes | 386 Vues
This guide introduces the fundamental concepts of passing data between web pages using ASP.NET. By the end of this week's sessions, you will be able to use self posting, query strings, and session variables to manage data flow. We will cover how to set up ASP pages in IIS, process user logins, and restrict access to pages. You’ll learn about temporary and persistent data storage methods, and best practices for maintaining session state. Real code examples will illustrate the handling of user sessions and data transfer techniques effectively.
E N D
10 – Passing Data between pages: Sessions, Query Strings, & Self Posting
Session Aims & Objectives • Aims • To introduce the fundamental ideas involved in passing data between pages • Objectives,by end of this week’s sessions, you should be able to: • pass data between pages, using: • Self Posting • Query Strings • Session Variables
IIS • Put ASP pages in: • C:\INetPub\wwwRoot(this part of hard disk exposed to outside world) • Execute pages by putting: • localhost(in web browser, e.g. IE, means local machine) • ASP pages don't work by double-clicking
IIS – Date.asp C:\INetPub\wwwRoot\Test\Date.asp localhost/test/date.asp Short for Response.Write
Example: Logon • Restrict access tohome page
Example: Logon - code (v2) LoginFail.asp Home.htm <html> <head> <title></title> <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect "home.htm" End If %> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body> </html> <html> <head> <title>My Home page</title> </head> <body> <p>Welcome to my home page.<br> <img src="YouAreHere.jpg" WIDTH="450" HEIGHT="259"> </body> </html> Logon.htm <html> <head> <title></title> </head> <body> <p>Please logon: <form action=LoginFail.asp method=post> <input name=txtUserName type=text> <input name=txtPassWord type=text> <input name=btnLogon type=submit value=Logon> </form> </body> </html> • Using Server-side VB Script:
Problem • Want restricted access: • However, can type home page directly (bypassing password page)
Solution • Need way for: • password page to tell home page • that user logged in OK
Passing Data between pages • Problem • want to protect all pages from unauthorised access • need to store record of successful login • Variables • only persist for duration of page
Passing Data (persistent) • Cookies (not covered in Soft131) • stored on users’ (client) hard drive • persists between sessions • Database/file (covered in next lecture) • stored on server hard drive • persists between sessions
Passing Data (temporary) • Session object • exists for current session • clears if user closes browser • clears after 20 mins of inactivity • Forms and Self Posting • Query Strings • Useful for passing information between pages
Example: Logon - code (v3) LoginFail.asp Home.asp <html> <head> <title></title> <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Session("LoggedIn") = "Yes" Response.Redirect "home.htm" End If %> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body> </html> <html> <head> <title>My Home page</title> <% If Session("LoggedIn") <> "Yes" Then Response.Redirect "Logon.htm" End If %> </head> <body> <p>Welcome to my home page.<br> <img src="YouAreHere.jpg" WIDTH="450" HEIGHT="259"> </body> </html> Logon.htm <html> <head> <title></title> </head> <body> <p>Please logon: <form action=LoginFail.asp method=post> <input name=txtUserName type=text> <input name=txtPassWord type=text> <input name=btnLogon type=submit value=Logon> </form> </body> </html> • Using Session variable:
Maintaining State: Session Object <html> <head> <title>Login</title> </head> <body> <% If Request.Form("txtUserName") = "George" Then Session("LoginOK") = "Yes" Response.Redirect "Home.asp" Else Session.Abandon If Request.Form("txtUserName") <> "" Then Response.Write "Invalid user name, please try again." End If End If %> <p>Please login: <form name="frmLogin" action="Login.asp" method=post> Username:<input name="txtUserName" type="text"><br> Password:<input name="txtPassWord" type="password"><br> <input name="btnLogin" type="submit" value="Login"> </form> </body> </html> Login.asp • Session variable • all strings • Abandon method • deletes all session variables • Redirect method • redirects browser to specified page
Maintaining State: Session Object Home.asp <html> <head> <title></title> <% If Session("LoginOK") <> "Yes" Then Response.Redirect "Login.asp" End If %> </head> <body> <center><b>Home Page</b></center> <p>Welcome to my home page. </body> </html> ASP code tocheck forsuccessful login
Maintaining State: Self Posting • Form points to self: • If any submitbutton pressedpage re-loads Multiply.asp <http> <head> <title>Multiply</title> … </head> <body> <form action=Multiply.asp method=post> … </form> </body> </http>
Example: Multiply Only do calc if first load Post to Self Multiply.asp <http> <head> <title>Multiply</title> <% Dim tmpRes Dim tmpNum1 Dim tmpNum2 If Request.Form("txtNum1") <> "" And Request.Form("txtNum2") <> "" Then tmpNum1 = CDbl(Request.Form("txtNum1")) tmpNum2 = CDbl(Request.Form("txtNum2")) tmpRes = tmpNum1 * tmpNum2 End If %> </head> <body> <form name="frmDefault" action=Multiply.asp method=post> <p><input name=txtNum1 type=text size=5 maxlength=5 value=<%=tmpNum1%>> <input name=txtNum2 type=text size=5 maxlength=5 value=<%=tmpNum2%>> <p><input name=btnCalc type=submit value=Calc> </form> <p><%=tmpRes%> </body> </http>
Maintaining State: Query Strings Query String • Data added to end of URL (address):http://localhost/page.asp?Surname=Bob • ASP code can use this data: • Request.QueryString("Surname") • would return the value "Bob" • Form method=get • data automatically added to query string
Example: Date-Time Menu.asp <html> <head> </head> <body> <p>What background colour do you want for you date information? <br><a href=DateTime.asp?Colour=yellow>Yellow</a> <br><a href=DateTime.asp?Colour=cyan>Light Blue</a> </body> </html> DateTime.asp <html> <head> </head> <body bgcolor=<%=request.querystring("Colour")%>> <p>The date is <%=date()%>. <p>The time is <%=time()%>. </body> </html>
Reference: Server Object Model • Request object: calling web page • Form: used to get form data from page • QueryString: used to get data from address (?) • Response object: web page sent back • Write: used to put text into web page • Redirect: used to navigate to other page • Clear: erases all HTML in web page • Session object: store data between pages • Abandon: clears session data
Numeric Expressions many people instinctivelyknow these are wrong data • 23 + 11 - a * 2 • 34 + * 12 + x d o o d o d • b + 1 – 21 45 d o d o d d operators
String Expressions data data data data operator operator operator • "What is " & num1 & " times " & num2 • "What is twice " num1 & "?" • "What is 6 minus " & & num & "?"
Example: Apples • SPECIFICATION • User Requirements • help children learn numbers 1 - 10 • Software Requirements • Functional: • display random number of apples (between 1 & 10) • ask child how many apples are there • child enters answer • computer responds appropriately • Non-functionalshould be easy to use, and interesting
Design • user interface: • design: • what buttons, text boxes, text do you need? • implement using HTML • how do you create these? • functionality: • design: • what do you want to happen, and when? • implement using VB Script code • how do you do this?
Problem Solving • Imagine working things out by hand • Consider: • what actions you take, andwhen they are done • what information you remember • Computer must mimic these
Solutions • 'which is right?' • both? • better solution • smaller (more elegant) • fewer lines of code • shorter lines of code
Tutorial Exercises • Task 1: Get Logon v3 working (from the lecture) • Task 2: Get the Multiply example (from the lecture) working • Task 3: Get the Date-Time example (from the lecture) working • Task 4: Design and code the Apples example