asp

by dan 10/05/06 12:00:00 Article programming.


.. contents:: Uses Request.___() and Response.___() for most operation :: <% Response.Write("hello world
") %> Output: hello world Request -------------- GET -------- Request.QueryString(): mypage.asp?cat=5&name=dan -> Request.QueryString() => "cat=5&name=dan" ::
Your name:
<% dim name name=Request.QueryString("name") If name<>"" Then Response.Write("Name: " & name & "
") End If %> POST ------------ Request.Form(): ==> output: "cat=5&name=dan" if cat and name was posted in form ::
Your name:
<% dim name name=Request.Form("name") If name<>"" Then Response.Write("Name: " & name & "
") End If %> Radio Button:: <% dim cars cars=Request.Form("cars") %>

Select Red or Green Color:

value="Red">Red
value="Green">Green
<% if color <>"" then Response.Write("

Color: " & color & "

") end if %> Cookies ------------- :: <% ; Counter example : dankim dim visit_count response.cookies("visit_count").Expires=date+5 ; lasts 5 days visit_count=request.cookies("visit_count") if visit_count="" then response.cookies("visit_count")=1 response.write("Counter is 0") else response.cookies("visit_count")=visit_count+1 response.write("Counter: " & visit_count) end if %> Redirect -------------- Response.Redirect("somewhere_to_go.asp") :: <% if Request.Form("url")<>"" then Response.Redirect(Request.Form("url")) end if %>
Go to contact page
Go to about page
Buffer ------------------ if Response.Buffer is set to True, it uses buffer system. Response is not sent until the buffer is full or until it is flushed :: <%Response.Buffer=true Response.Write(do_something_complicated_and_long()) ; no output yet. Response.Flush ; print output NOW! %> Response.Clear(): clears the buffer ie no text is sent! <%Response.Clear %> Request - Properties ---------------------------------------- ServerVariables('var') * Request.ServerVariables("http_user_agent"): 'Mozilla...' * Request.ServerVariables("remote_addr")): IP Address of viewer ie "15.1.2.44" * Request.ServerVariables("request_method")): "GET" or "POST" * Request.ServerVariables("server_name"): Domain name of server ie "www.bzdz.com" Request.Totalbytes: Size of request Response - Misc Functions --------------------------------------- Response.End(): Ends response immediately, ignoring anything below it. Response - Properties ---------------------- Response.ContentType="text/html" Response.Charset="ISO8859-1" Response.Expires Session ===================== Session.SessionID: session id # ie "1345123" Session.Timeout: timeout in minutes. Session.Timeout=30 ; 30 minute Dictionary ==================== dict.Count: dict size dict.Keys() : array of keys. dict.Items(): array of items dict.Item("key"): item given the key name dict.Key("key")= "new_key_name" : replace key name :: <% dim dict set dict=Server.CreateObject("Scripting.Dictionary") dict.Add "m", "Male" if dict.Exists("f")= false then dict.Add "f", "Female" end if ' iterate for i = 0 To dict.Count -1 Response.Write( dict.Keys(i) & "="& dict.Items(i) & "
") next set dict=nothing 'erase dict %> Other services ================================= set adrotator=Server.CreateObject("MSWC.AdRotator") Changes ad each time. See doc for more info set nextlink=server.createobject("MSWC.Nextlink") builds table of contents I/O using Scripting.FileSystemObject ======================================================== Read a text File -------------------------------- OpenTextFile(Server.MapPath(path)) ReadAll(): Read entire text file ReadLine(): Read current one line Read(n): Read n characters :: <% Set FSO = Server.CreateObject("Scripting.FileSystemObject") Set file = FSO.OpenTextFile(Server.MapPath("mytexts") & "\readme.txt",1) While not file.AtEndOfStream Response.Write (file.ReadLine & "
") Wend %> File-based hit counter:: <% 'writing is disabled due to write-access security (ie file.Write) Set FSO=Server.CreateObject("Scripting.FileSystemObject") Set file=FSO.OpenTextFile(Server.MapPath("counter.txt"), 1, False) counter=file.ReadLine file.Close counter=counter+1 Set file=Nothing 'free mem Set FSO=Nothing %> Counter:<%=counter%>. Functions ----------------- File exist test:: If (FSO.FileExists("c:\web\myfile.txt"))=true Then ... Folder Exists FolderExists("c:\web") = true GetParentFolderName("d:\full\path\file.txt") GetExtensionName("something.txt") : "txt" GetBaseName("c:\web\index.html") : "index" GetBaseName("c:\web\tmp\") : "tmp" Date ---------------- Day of the week:: <% response.Write(WeekdayName(weekday(date))) response.Write("
") response.Write(WeekdayName(weekday(date), true)) %> Output:: Monday mo Month and Date:: <%response.write(MonthName(month(date)))%> <% if IsDate("5/5/2006") then response.Write("OK") end if response.write("Invalid Date Format") %> String ---------------- ucase, lcase:: <% response.write(ucase("lowly")) response.write(lcase("MAN")) %> trim, ltrim, rtrim strReverse Number ---------------- Round(x): rounds up a number (or string number) rnd(): return random # after randomize()

Digg! del.icio.us reddit furl