Instance learning Dreamweaver mx asp programming basics 2

Source: Internet
Author: User
Tags dreamweaver

Press "F12" to try the effect.

Note: common time functions:

Now () gets the current system Date and Time. asp output can be written as follows: <% = now () %>

Year (now () gets the year, ASP output: <% = year (now () %>

Month (now () gets the current month, ASP output: <% = month (now () %>

When day (now () is obtained, ASP outputs: <% = Day (now () %>

Minute (now () gets the number of minutes, ASP output: <% = minute (now () %>

Second (now () obtains the number of seconds. asp outputs: <% = second (now () %>

Date () gets the current system date in the format of 2004-2-28

Time () gets the current system time in the format of 22: 24: 59

When the response. Write method is used to output string information, a pair of double quotation marks (") in the" () "set must be in the English format. Function and numeric data are not used, for example:

Text string output: <% response. Write ("output text") %>

Function output: <% response. Write (now () %>

Number output: <% response. Write (1142568147) %>

You can also use "&" to correctly distinguish between numbers or functions and text mixed output:

<% YY = "the value of the output variable here" 'is assigned to the variable "YY".
Response. Write (now () & "& nbsp; output text" & "& nbsp;" & YY) %>

2. response. Redirect Method -- link to the new website

If you want to link the user to another URL in the browser, the response. Redirect method can implement this function. Some may think that JS Code can also implement this function! However, it is executed on the server and is not affected by the browser version of the client.

Enter the following code above the <HTML> tag:

<% Select case request. querystring ("url ")
Case "1"
Response. Redirect ("http://www.163.com ")
Case "2"
Response. Redirect ("http://www.sina.com ")
Case "3"
Response. Redirect ("http://www.sohu.com ")
Case "4"
Response. Redirect ("http://www.6to23.com ")
End select
%>

The complete code is as follows:

<% @ Language = "VBScript" codePage = "936" %>
<% Select case request. querystring ("url ")
Case "1"
Response. Redirect ("http://www.163.com ")
Case "2"
Response. Redirect ("http://www.sina.com ")
Case "3"
Response. Redirect ("http://www.sohu.com ")
Case "4"
Response. Redirect ("http://www.6to23.com ")
End select
%>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> untitled document </title>
</Head>
<Body>
<Form action = "Untitled-6.asp" method = "get" name = "form1" target = "_ blank">
<Select name = "url" id = "url">
<Option value = "1"> Netease </option>
<Option value = "2"> Sina </option>
<Option value = "3"> Sohu </option>
<Option value = "4"> China Student Network </option>
</SELECT>
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>
</Body>
</Html>

3. Implement cyclic output using the response. Write Method

This involves a VBSCRIPT script language, which is very simple but useful. We will use it when going to record set paging.

For... next loop statement structure:

For variable = start value to end value step value

Loop body

Next

The following is a small example:

Instance 5. Number of characters output cyclically

<% @ Language = "VBScript" codePage = "936" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> untitled document </title>
</Head>

<Body>
<% For y = 1 to 50%>
<% Response. Write ("word") %>
<% Next %>
</Body>
</Html>

Press the "F12" key to preview and the execution result is as follows:

It can be seen that it will output a word after 50 times of repetition. If no step value is set, it will be processed by the default value (1. Set the step value:

<% For y = 1 to 50 step 2%>
<% Response. Write (y) %> & nbsp;
<% Next %>

The execution result is as follows:

It can be seen that the output step is "2.

You can also change it:

<% Response. Write ("word") %>

Is:

<% Response. Write (y) %> & nbsp;

The execution result is as follows:

It starts from "1" and outputs the step value of "1" until "50.

We will only learn this loop statement first. There are other do... loop, do while... loop, do... loop, while... here we will not talk about the wend loop structure. Maybe we will not be able to touch these items manually for the time being.

4. Response Object Attributes

The response object has the following attributes:

  • Buffer Server Buffer.
  • Charset attaches the character set name to the end of the Content-Type title in the response object.
  • Contenttype specifies the HTTP content type of the server response.
  • Expires specifies the cache storage page expiration time on the browser.
  • Expiresabsolute specifies the exact expiration time for caching the storage page on the browser.

Response object method:

  • Write directly outputs information to the client.
  • Redirect redirection address.
  • End stops program execution.
  • Flush is used to send the output in the Server Buffer immediately. response. Buffer must be set to true; otherwise, an error is returned.
  • Clear clears all HTML output in the server buffer.

Response set:

  • Cookies are used to set the cookie value.

Response. Cookie set syntax format:

Response. Cookies (variable name) [(key) | attribute] = equal to written information

The variable name specifies the cookie name so that the page can read data from different cookies.

Iii. Session Object-server-specific user sessions

The "session" object is called a "phase variable" in the dw mx help document. When we insert "Login User" and "restrict access to the page" server behavior on the page, it uses session objects to identify whether a user logs in. After a session is created, the server traces the page and allocates memory to store session information. Session information can only be viewed by the user who created the session.

Instance 1: Use the request. Form Method to store information in session

Create an ASP page and enter the code above the <HTML> tag:

<% SESSION ("mm") = request. Form ("Y") %>

Enter the following code between <body> and </body>:

<Form action = "Untitled-6.asp" method = "Post" name = "form1">
<Select name = "Y" id = "Y">
<Option value = "Netease"> Netease </option>
<Option value = "Sina"> Sina </option>
<Option value = "Sohu"> Sohu </option>
<Option value = "China student network"> China Student Network </option>
</SELECT>
<Input type = "Submit" name = "Submit" value = "Submit">
</Form>

<P> <% = SESSION ("mm") %> </P>

<% = SESSION ("mm") %> is the value of the read session variable displayed on the page.

Press "F12" to preview the execution result.

If the request. querystring method is used to store data in the session object, <% SESSION ("mm") = request. Form ("Y") %>

Changed:

<% SESSION ("mm") = request. querystring ("Y") %>

You can.

1. session. adandon Method

The Session object has only one method, namely, the abandon method, which is used to delete all data stored in the session object and access these resources. If the adandon method is not explicitly called, when the session times out (the default timeout time of the IIS server is 900 seconds, which is determined by the Space Server Provider), the server deletes these sessions to release resources.

Syntax:

<% Session. adandon %>

Analysis of the "deregister user" code of DW MX

<%
'*** Logout the current user.
Mm_logoutredirectpage = "index. asp"
Session. Contents. Remove ("mm_username ")
Session. Contents. Remove ("mm_userauthorization ")
If (mm_logoutredirectpage <> "") Then response. Redirect (mm_logoutredirectpage)
%>

The session is used in this Code. contents. remove attribute to specify a session variable to be deleted. when "adandon" is used for "Remove", it will delete all sessions created on the page, but sometimes we only need to delete one or several sessions and keep the other session status.

Delete multiple sessions:

Session. Contents. Remove ("mm_username ")
Session. Contents. Remove ("mm_userauthorization ")
...

The following code is generated by the "Restrict Access To pages" server behavior of DW mx:

<%
'*** Restrict access to page: grant or deny access to this page
Mm_authorizedusers = ""
Mm_authfailedurl = "yesno. asp"
Mm_grantaccess = false
If SESSION ("mm_username") <> "" then
If (true or CSTR (Session ("mm_userauthorization") = "") or _
(Instr (1, mm_authorizedusers, Session ("mm_userauthorization")> = 1) then
Mm_grantaccess = true
End if
End if
If not mm_grantaccess then
Mm_qschar = "? "
If (instr (1, mm_authfailedurl ,"? ")> = 1) Then mm_qschar = "&"
Mm_referrer = request. servervariables ("url ")
If (LEN (request. querystring ()> 0) Then mm_referrer = mm_referrer &"? "& Request. querystring ()
Mm_authfailedurl = mm_authfailedurl & mm_qschar & "accessdenied =" & server. urlencode (mm_referrer)
Response. Redirect (mm_authfailedurl)
End if
%>

The "If SESSION (" mm_username ") <>" then "is used to determine whether the" mm_username "session variable has a value. Otherwise, the" response. redirect (mm_authfailedurl) "turns to the error page.

In fact, we can write a simpler one:

<% If SESSION ("mm_uesrname") = "" then
Response. Redirect ("error page. asp ")
Response. End
End if
%>

This section of code checks whether the "mm_uesrname" session variable is null. If yes, use response. redirect ("error page. ASP) to "error page. ASP, and use response. end method to stop all script processing, so this code is best placed in the page header.

You can also

Response. Redirect ("index. asp ")

Changed:

Response. Write ("Sorry! Please log in! ")

In this way, the error prompt is directly completed on the same page, so that you do not need to transfer the page to facilitate site file management.

Well, if you can skillfully use the above three basic ASP built-in objects, you can modify the server behavior of dw mx to achieve our goal. There are other objects that we will not talk about here, because the server behavior of dw mx can generate these codes for us, and you don't have to compile unusual programs like learning asp from beginning to end.

Previous Page

Keywords:

[Recommended to friends] [close] [add to favorites this article] the latest five comments view all comments

0 comments

Spokesman: ch19880124 time:
Bucuo
Speaker: Guest time:
Good
Speaker: Guest time:
How can code run differently on my computer?
Spokesman: It Time:
In this tutorial, some of them can only stop playing a little bit.
Speaker: Guest time:
How can we make it buffer when dragging it?
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.