Collection of ASP session objects and property method events

Source: Internet
Author: User
Tags character set comments contains include variables terminates valid
session| Objects | collection

Session StaticObjects Collection
The StaticObjects collection contains all objects created with the <OBJECT> tag in the scope of the Session object. This collection can be used to determine the value of an object-specific property, or to traverse the collection and get all the properties of all objects.

Grammar
Session.StaticObjects (Key)

Parameters
Key
The property to retrieve.
Comments
Use the loop control structure to traverse the keywords in the StaticObjects collection. Take a look at the example below.

<%
Dim Objprop
For each objprop in session.staticobjects
Response.Write (Objproperty & ":" & Session.StaticObjects (Objprop) & "<BR>")
Next

%>


Session Contents Collection
The Session.Contents collection includes all the items that were established for the session without <OBJECT> tags. This collection can be used to determine the value of a specified session item or traversal set Merge to retrieve a list of all items in a session.

Grammar
Session.Contents (Key)

Parameters
Key
The name of the property to get.
Comments
You can use a loop control structure to loop through the keywords of the Contents collection. The following example demonstrates this procedure.

<%
Dim Sessitem
For each sessitem in session.contents
Response.Write (Sessitem & ":" & Session.Contents (sessitem) & "<BR>")
Next

%>

The

CodePage
CodePage property determines the code page that will be used to display dynamic content.

Syntax
Session.CodePage (=codepage)

Parameters
CodePage
This is an unsigned integer that represents a valid code page for the system that is running the ASP script engine. The
Comment
code page is a character set that can include numbers, punctuation marks, and other letters. Different code pages can be used for different languages and regions. For example, ANSI code page 1252 is used for American English and most European languages; OEM code page 932 is for kanji. The

code page is a character chart that maps characters to single-byte or multibyte values. Many code pages share characters between 0x00-0x7f in the ASCII character set.

Abandon
The Abandon method deletes all objects stored in the session object and releases the source of those objects. If you do not explicitly call the Abandon method, the server deletes these objects once the session times out.

Grammar
Session.Abandon

Comments
When the Abandon method is invoked, the current session object is deleted sequentially, but the object is actually deleted when all script commands are processed in the current page. This means that when Abandon is invoked, the variables stored in the Session object can be accessed on the current page, but not on subsequent Web pages.

For example, in the following script, the third line prints the Mary value. This is because the session object is not deleted before the server finishes processing the script.

<%
Session.Abandon
Session ("myname") = "Mary"
Reponse.write (Session ("MyName"))
%>

If you access the MyName variable on a subsequent Web page, you will find that it is empty. This is because when the page that contains the previous example finishes processing, myname is deleted with the previous session object.

When you discard a session and open a subsequent Web page, the server creates a new sessions object. You can store variables and objects in a new Session object.

Example
The following example releases session state when the server finishes processing the current page.

<% Session.Abandon%>

LCID
The LCID property determines the location identification used to display dynamic content.

Grammar
Session.LCID (=LCID)

Parameters
Lcid
A valid field identification.
Comments
The LCID specifies the field identification, which is the international standard abbreviation for the field that uniquely identifies a system definition.

SessionID
The SessionID property returns the user's session identity. When you create a session, the server generates a separate identity for each session. The session identity is returned with a long shaping data type.

Grammar
Session.SessionID

Comments
Do not use the SessionID property to create a primary key for your database application. This is because if the WEB server restarts, the partial SessionID value may be the same as the value produced before the server terminates. You can use an automatically incremented column data type instead, such as IDENTITY in Microsoft®sql Server, or COUNTER in microsoft®access.

Session_OnStart
The Session_OnStart event occurs when a new session is created by the server. The server processes the requested page before it is executed. The Session_OnStart event is the best time to set session variables, because they are set before any pages are accessed. All built-in objects (application, ObjectContext, Request, Response, Server, and session) can be used and referenced in Session_OnStart event scripts.

Grammar
<script language=scriptlanguage runat=server> Sub Session_OnStart ... End Sub ' by aspxuexi.com

</SCRIPT>

Parameters
ScriptLanguage
Specifies the scripting language used to write event scripts. Can be any language that supports scripting, such as VBScript or JScript. If you have multiple events that use the same scripting language, you can organize them under a set of <SCRIPT> tags.
Example
Although the session object persists when the Session_OnStart event contains a Redirect or End method call, the server stops processing the Global.asa file and triggers the script in the file of the Session_OnStart event.

For example, to ensure that a user always starts a session when a particular Web page is opened, the Redirect method can be invoked in the Session_OnStart event. When the user enters the application, the server creates a session for the user and processes the Session_OnStart event script. You can include the script in this event to check that the user opens a page that is not a startup page, and if not, instruct the user to invoke the Response.Redirect method to start the Web page. It is illustrated in the following example.

<script Runat=server language=vbscript>
Sub Session_OnStart
' Make sure ' this new users start on the correct
' Page of the ASP application. ' By aspxuexi.com
' Replace the ' value given to startpage below
' With the virtual path to your application ' s
' Start page.

StartPage = "/myapp/starthere.asp"
CurrentPage = Request.ServerVariables ("Script_name")

' Do a case-insensitive compare, and if they
' Don ' t match, send the user to the start page.
If StrComp (currentpage,startpage,1) then Response.Redirect (startpage) End IfEnd Sub</script>
The above example runs only in browsers that support cookies. Because browsers that do not support cookies cannot return the SessionID cookie, the server creates a new session whenever the user requests a Web page. Thus, for each request, the server processes the Session_OnStart script and redirects the user to the startup page. If you want to use the following script, it is recommended that you put a notice on the startup page telling the user that the site requires a browser that supports cookies.

Comments
Note that any Session_OnStart event scripts after the Redirect method are not executed. Therefore, you should call the Redirect method at the end of your event script. It is illustrated in the following example.

<script Language=vbscript runat=server>
Sub Session_OnStart
' Session initialization script

' By aspxuexi.com
Response.Redirect "Http:/server/app/starthere.asp"
End Sub
</SCRIPT>
In the above example, the Redirect method hides all text displayed to the customer during the session initialization script.

Session_OnEnd
The Session_OnEnd event was discarded or timed out during the session. In a server-built object, only application, server, and session objects are available.

Grammar
<script language=scriptlanguage runat=server> Sub session_onend ... End Sub

</SCRIPT>

Parameters
ScriptLanguage
Specifies the scripting language used to write event scripts. Can be any language that supports scripting, such as VBScript or JScript. If you have multiple events that use the same scripting language, you can organize them under a set of <SCRIPT> tags.
Comments
The MapPath method cannot be invoked in the Session_OnEnd script.


Timeout
The Timeout property specifies the time-out period for the session object of the application in minutes. If the user does not refresh or request a Web page within the time-out period, the session terminates.

Grammar
Session.Timeout [= Nminutes]

Parameters
Nminutes
Specifies how many minutes the session is idle before the server terminates the session automatically. The default value is 15 minutes. (This data is based on your IIS settings)



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.