Asp tutorial. net session simple test application
<% @ Page language = "c #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> session test </title>
<Script type = "text/Webpage effects">
Function ajaxget ()
{
Var ajaxobject = window. activexobject? New activexobject ("microsoft. xmlhttp"): new xmlhttprequest ();
Ajaxobject. onreadystatechange = function ajaxstatechange (){
If (ajaxobject. readystate = 4)
{
If (ajaxobject. status = 200)
{
Var strdata = ajaxobject. responsetext;
If (strdata = 1)
{
Window. cleartimeout (auto );
Alert (ajaxcount );
}
Return;
}
Else
{
Alert ('network is busy. Please refresh and try again! ');
}
}
};
Ajaxobject. open ('get', 'default. aspx? Ajax = 1', true );
Ajaxobject. setrequestheader ("content-type", "application/x-www-form-urlencoded; charset = UTF-8 ");
Ajaxobject. send ();
}
Var ajaxcount = 0;
Var auto = null;
Function startajax ()
{
// When the aspx page is periodically requested through ajax, the session on the current page can be prevented from being lost!
Var btnajax = document. getelementbyid ('btnaja ');
Btnajax. disabled = true;
Ajaxcount ++;
Btnajax. value = ajaxcount;
Ajaxget ();
Auto = window. settimeout (startajax, 1000 );
}
</Script>
</Head>
<Body style = "font-family: georgia;">
<Form id = "form1" runat = "server">
<Input id = "btnajax" type = "button" value = "Heartbeat" onclick = "startajax ();"/>
<Asp: textbox id = "textbox1" runat = "server"> </asp: textbox>
<Asp: button id = "button1" runat = "server" text = "add session" onclick = "button#click"/>
<Asp: button id = "button2" runat = "server" text = "Release session" onclick = "button2_click"/>
Number of values contained in a session: [<% = session. count %>]
<% = Session ["test"] %>
<Div>
Current session count: <% = constants. sessioncount %> <br/>
Cumulative sessions: <% = constants. sessionallcount %>
</Div>
<Table border = "1" cellpadding = "5" style = "border-collaps tutorial e: collapse;">
<Tr style = "background-color: # dff;">
<Th> sessionid </th>
<Th> Start time </th>
</Tr>
<% Foreach (dictionaryentry r in constants. sessionlist ){
Bool iscur = r. key. tostring () = session. sessionid; %>
<Tr style = "color: <% = iscur? "Blue": "black" % >; ">
<Td> <% = r. key %> </td>
<Td> <% = r. value %> </td>
</Tr>
<% }%>
</Table>
</Form>
</Body>
</Html>
Cs files
Using system;
Using system. data;
Using system. configuration;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system.web.ui.html controls;
Using system. collections;
Public partial class _ default: system. web. ui. page
{
Protected void page_load (object sender, eventargs e)
{
// Note that when ajax requests the aspx page, the aspx page should be set to not allow cache!
Response. appendheader ("pragma", "no-cache ");
Response. appendheader ("cache-control", "no-cache, must-revalidate ");
Response. appendheader ("expires", "0 ");
If (request. querystring ["ajax"]! = Null)
{
// The number of sessions on the current website returned by ajax
Response. write (constants. sessionlist. count. tostring ());
Response. end ();
}
}
Protected void button#click (object sender, eventargs e)
{
// Add a session
// Session ["test"] = datetime. now;
Session. add ("test", datetime. now );
Response. redirect (request. url. tostring ());
}
Protected void button2_click (object sender, eventargs e)
{
// Remove the specified session
// Session ["test"] = null;
// Session. remove ("test ");
// Remove all sessions and session. removeall (); Same Function
// Session. clear ();
// Terminate the current session status. Note that the effect of session_end is the same as that of session Timeout.
// After this sentence is called, sessionid is not reset.
// When only this sentence is called (when the session times out and expires), If you refresh the current page, session_start and session_end will always be called
// This status will not exit unless you add a value to the session or reset the sessionid.
Session. abandon ();
// Reset the current sessionid
// If the specified sessionid is the same as the existing sessionid, session hijacking occurs.
// If it does not exist, the system will automatically create a new session
// Response. cookies. add (new httpcookie ("asp.net tutorial _ sessionid", this. textbox1.text ));
Response. redirect (request. url. tostring ());
}
}
Constanst. cs
Using system;
Using system. data;
Using system. configuration;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system.web.ui.html controls;
Using system. collections;
/// <Summary>
/// Constants Summary
/// </Summary>
Public class constants
{
Public constants ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// List of all current sessions
/// </Summary>
Public static hashtable sessionlist = new hashtable ();
/// <Summary>
/// Number of current sessions
/// </Summary>
Public static int sessioncount = 0;
/// <Summary>
/// The total number of sessions
/// </Summary>
Public static int sessionallcount = 0;
}
Global. asax
<% @ Application language = "c #" %>
<Script runat = "server">
Void application_start (object sender, eventargs e)
{
// Code that runs when the application starts
}
Void application_end (object sender, eventargs e)
{
// Code that runs when the application is closed
}
Void application_error (object sender, eventargs e)
{
// Code that runs when an unhandled error occurs
}
Void session_start (object sender, eventargs e)
{
Constants. sessionallcount ++;
Constants. sessioncount ++;
Constants. sessionlist. add (session. sessionid, datetime. now );
// Session ["sessionstartdatetime"] = datetime. now;
}
Void session_end (object sender, eventargs e)
{
Constants. sessioncount --;
Constants. sessionlist. remove (session. sessionid );
}
</Script>