44
Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. [email protected] http://www.draperconsulting.com

Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. [email protected]

Embed Size (px)

Citation preview

Page 1: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

Tips, Trick and Techniquesfor ASP Developers

Atlanta ASP User Group

Don DraperDraper Consulting, Inc.

[email protected]

http://www.draperconsulting.com

Page 2: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

2

Topics

• Transact-SQL Techniques

• Handling Expired Session Techniques

• Background Images in Tables

• JavaScript Tips

• The Response Object

• Understanding HTTP Headers

Page 3: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

3

Transact-SQL Techniques

Page 4: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

4

Transact-SQL, Technique #1Ad-Hoc WHERE Clause

• Problem: User can enter any selection criteria in multiple fields for ad-hoc query, but data source is a stored procedure.

• Solution: WHERE (col1 = @arg1 OR @arg1 is null) AND (col2 like @arg2 OR @arg2 is null) AND (col3 like @arg3 OR @arg3 is null)

Page 5: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

5

Transact-SQL, Technique #1Ad-Hoc WHERE Clause

• In ASP Code…– Make sure empty parameters are passed as

nulls if parm = empty then parm = null

– Can use Parameter ObjectSet param1 = cmd.CreateParameter ("city", adVarChar,adParamInput,15,city)

Set param2 = cmd.CreateParameter ("company", adVarChar, adParamInput, 40, company)

cmd.Parameters.Append param1

cmd.Parameters.Append param1

Page 6: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

6

Transact-SQL, Technique #1Ad-Hoc WHERE Clause

• In ASP Code…– Can use Array function to pass params

city = request("city")

if city = empty then city = Null ‘ pass null to sp if not needed

Set rs = cmd.Execute (recs, Array( company, city ) )

Page 7: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

7

Transact-SQL, Technique #1Ad-Hoc WHERE Clause

• In Stored Procedure…– default arguments to null

@companyname varchar(40) = null, …

– use “Is null”..NOT “= null”Good: WHERE (company like @company OR @company is

null)

Bad: WHERE (company like @company OR @company = null)

Page 8: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

8

Transact-SQL, Technique #2Ad-Hoc ORDER BY Clause

• Problem: User can click on any header in table and the result set is reordered for that column. However, the data source is a stored procedure. How can I alter ORDER BY clause?

Solution #1: Use Exec() – lose precompiled advantage of stored proce

• Solution #2: Use ANSI CASE (better)– Retain precompiled advantage of stored proc

Page 9: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

9

Transact-SQL, Technique #2Ad-Hoc ORDER BY Clause

• Sample Code for Stored Procedure

ORDER BY

CASE @sort passed in a argument to procedure

WHEN 'id' THEN CAST (CustomerID as varchar(20))

WHEN 'companyname' THEN companyname

WHEN 'city' THEN city

ELSE CAST (CustomerID as varchar(20))

END

Page 10: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

10

Handling Expired Sessions

Page 11: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

11

Handling Expired Sessions

• We use sessions to …– Maintain state information– Maintain user specific data across pages– Provide additional security…and more

• Sessions end by – Session.Abandon– Session expires due to period of inactivity

Page 12: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

12

Handling Expired Sessions

• When session expires– Need user to start over

• login again• start new session & re-establish session data

• Techniques to check for current session– Check for session value at top of page– Let session events handle it for you!

Page 13: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

13

Handling Expired Sessions

• Typical Technique:– Check for session variable at the top of

each page

If session(“userid”) = “” then

redirect(“login.asp”)

End if

Page 14: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

14

Handling Expired Sessions

• Suggested Technique:– Let a session event handle it for you– In Global.ASA file Session_OnStart

• Check referring URL• Redirect/Transfer if not coming from your start (login?)

page

fromURL = Request.ServerVariables("URL")

if InStr(lcase(fromURL), "login.asp") = 0 then

Response.Redirect("expired_page.asp")

end if

Page 15: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

15

Handling Expired Sessions

• Watch out for…– login page is ASP page

• Session already started before login – Use <@ EnableSessionState = FALSE>– Use Session.Abandon to kill session

– other pages that start session before login• Same as above

– use NT Performance Monitor to watch session count when testing

Page 16: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

16

Background Images in Tables

Page 17: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

17

Background Images in Tables

Table background images offer an easy way to place other elements over an image. Cell can be used for positioning.

Page 18: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

18

Background Images in Tables

• Problem: Background images can be defined as

an attribute of a TABLE tag. This permits elements such as text, fields and more to be place over an image.

However, Netscape renders the image starting in each cell, even if defined at the TABLE level!

Page 19: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

19

Background Images in Tables

<TABLE background=“images/myimg.gif”>

..

..

</TABLE>

Page 20: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

20

Background Images in Tables

Netscape renders the image inside each cell, even if defined at the TABLE level. As you can see, this is not acceptable!

Page 21: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

21

Background Images in Tables

• Solution: Use two nested tables– Outer table

• gets one empty cell to hold inner table• background attribute defined in Table tag

– Inner table• background attribute in Table tag but with

empty value ( background=“” )

• Use cells to position elements over image

Page 22: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

22

Client-Side JavaScript

Page 23: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

23

Client-Side JavaScript

• Two common uses…– Place cursor in appropriate form field– Client-side form validation

• Place cursor in appropriate form fielddocument.formname.fieldname.focus();

<BODY onLoad="document.form1.name.focus();“>

Page 24: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

24

Client-Side JavaScriptPlace cursor in UID field if empty…otherwise place in

password field

<script language="JavaScript"><!-- hide from old browsersfunction set_focus( ) {if (document.forms[0].as_userid.value.length > 0)

document.forms[0].as_password.focus();else

document.forms[0].as_userid.focus();}// --></script>

Page 25: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

25

Client-Side JavaScript

Use // to comment out closing HTML comments <script language="JavaScript">

<!--

function myfunction( ) {

}

// --> the js comment (//) prevents errors in some browsers

</script>

Page 26: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

26

Client-Side JavaScript

FormChek.js - Form Validation Library • Free, Public Domain Library• Over 60K of validation routines by Eric Krock, NS

– Basic data validation routines– Reformat data– Prompt user– Interactively check field contents– Credit card validation

Page 27: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

27

Understanding HTTP Headers

Page 28: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

28

Understanding HTTP Headers?

• Request Headers (from browser)– GET - hyperlink request– POST - FORM request (default)

• Response Headers (from server or ASP code)– Content-Type: - defines type of content– Location: - redirect browsers to URL– Set-Cookie: - stores data on browser

Page 29: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

29

Pages / ASP

FunctionA( )FunctionB( )FunctionC(a)..

Pages / ASP

FunctionA( )FunctionB( )FunctionC(a)..

Request & Response

IIS / ASP(server)Web browser

HTTP Request

HTTP Response

GET URL HTTP/1.0

HTML page or binary content (PDF) prefixed with HTTP Response Headers

Page 30: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

30

HTTP Response Headers

<HTML><BODY>

My Web Page!

</BODY></HTML>

<HTML><BODY>

My Web Page!

</BODY></HTML>

HTTP/1.0 200 OKServer: Apache/1.1.1Content-Type: text/htmlContent-Length: 31078

HTTP/1.0 200 OKServer: Apache/1.1.1Content-Type: text/htmlContent-Length: 31078

HTML Page

HTTP Headers

Page 31: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

31

HTTP Header Format

• Each header is on its own line with no blank lines between them

• The last header is followed by a blank line and then the content

Page 32: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

32

HTTP Headers

<HTML><BODY>

My Web Page!

</BODY></HTML></BODY>

<HTML><BODY>

My Web Page!

</BODY></HTML></BODY>

HTTP/1.0 200 OKServer: Apache/1.1.1Content-Type: text/htmlContent-Length: text/html

HTTP/1.0 200 OKServer: Apache/1.1.1Content-Type: text/htmlContent-Length: text/html

HTML Page

Headers added by Web Server

Headers you can control

One blank line separates

headers from

content

Page 33: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

33

Common HTTP Response Headers

• Location: (redirection)

• Content-Type: (MIME type of content)

• Content-Length: (length of content)

• Set-Cookie: (save data on browser)

Page 34: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

34

The Response Object

Page 35: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

35

The Response Object

• Collections– Cookies

• Properties– Buffer– CacheControl– CharSet– ContentType– Expires– ExpiresAbsolute– IsClientConnected– Status

• Methods– AddHeader– AppendToLog– BinaryWrite– Clear– End– Flush– PICS– Redirect– Write

Page 36: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

36

The Response Object

• Collections–

• Properties– Buffer– – – – – – –

• Methods– AddHeader– – – Clear– End– Flush– – Redirect–

Page 37: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

37

Tips for Using Flush and End

• Response.Flush– Flush after page titles but before long database

queries to increase perception of speed– Output continue to fill buffer after Flush is used– Response.Buffer must be TRUE

• Response.End– Stops further output to the browser– Use Clear to remove output prior to End

Page 38: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

38

Tips for Using Clear and Redirect

• Response.Clear– Clear page content since the last Flush or Clear;

headers are still sent– Useful to display only error message instead of

standard page content

• Response.Redirect– Sends 302 Object Moved response error– Buffer must be true if content already sent– Don’t need Clear prior to Redirect

Page 39: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

39

Redirect(“file2.asp”)Redirect(“file2.asp”)

Request & ResponseRedirect Requires 2 Round Trips to Server

IIS / ASP(server)Web browser

Request: GET /file1.asp HTTP/1.0

Response: 302 Object MovedLocation: file2.asp

Request: GET /file2.asp HTTP/1.0

File1.asp

<HTML><HTML>

File2.asp

Response: 200 OkHTTP/1.1 200 OK

Server: Microsoft-IIS/4.0Date: Wed, 16 Feb 2000 10:26:53 GMT

Content-Length: 218Content-Type: text/html

Page 40: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

40

Server.Transfer(“File2.ASP”)

Server.Transfer(“File2.ASP”)

Request & ResponseServer.Transfer Requires Only 1 Trip to Server

(ASP 3.0)

IIS / ASP 3.0(server)Web browser

Request: GET /file1.asp HTTP/1.0

File1.asp

<HTML><HTML>

File2.asp

Response: 200 OkHTTP/1.1 200 OK

Server: Microsoft-IIS/4.0Date: Wed, 16 Feb 2000 10:26:53 GMT

Content-Length: 218Content-Type: text/html

Server send back different file

Page 41: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

41

More Information

• Ostrosoft Internet Tools– http://www.ostrosoft.com

• HTTP Protocol and Headers– http://www.w3.org/Protocols/rfc2616/rfc2616.html

• FormChek.js– Look in search engines– http://www.draperconsulting.com/downloads/formchek.zip

Page 42: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

42

More Information

• Wrox Books– Professional Active Server Pages 2.0 (3.0)– ASP 2.0 (2.1) Programmers Reference– ADO 2.0 (2.5) Programmers Reference– http://www.wrox.com– http://webdev.wrox.co.uk/books/BookList.asp

Page 43: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

43

Questions?

Page 44: Tips, Trick and Techniques for ASP Developers Atlanta ASP User Group Don Draper Draper Consulting, Inc. Don@draperconsulting.com

February 10, 2000 Draper Consulting, Inc. http://www.draperconsulting.com

44

Presentation Survey Results