ASP.Net/Session Cookie/Application
Содержание
- 1 Add new name value pair to the Application (VB.net)
- 2 All application variables (VB.net)
- 3 Application.AllKeys (VB.net)
- 4 Application.Clear() (VB.net)
- 5 Application.Count before and after variable setting (VB.net)
- 6 Application.Count (VB.net)
- 7 Application: GetKey (VB.net)
- 8 Application.Item by index (VB.net)
- 9 Application.Item by kay (VB.net)
- 10 Application(I) (VB.net)
- 11 Application.Lock() and unLock() (VB.net)
- 12 Application property example (VB.net)
- 13 Application.RemoveAll() (VB.net)
- 14 Application Remove At (VB.net)
- 15 Application.Remove (VB.net)
- 16 Application Set (VB.net)
- 17 Application state
- 18 Application.StaticObjects (VB.net)
- 19 For Each Key in Application.Keys (VB.net)
- 20 Get variables from global.asax (VB.net)
- 21 Give a Application a new value (VB.net)
- 22 Set Application key value pair (VB.net)
- 23 Store key value pair for entire application (C#)
Add new name value pair to the Application (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Add("Added", "AddedValue")
Message.Text = Application("added")
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
All application variables (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
Dim I as Integer
lblMessage.Text = "All Application variables:<BR>"
For I = 0 to Application.Count - 1
lblMessage.Text = lblMessage.Text _
& Application.GetKey(I) & ": " _
& Application(I) & "<BR>"
Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Application Variables</TITLE>
</HEAD>
<form runat="server">
<asp:label
id="lblMessage"
font-size="12pt"
font-name="Tahoma"
runat="server"
text="Please provide your name."
/>
</form>
</BODY>
</HTML>
Application.AllKeys (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Dim I as Integer
Dim StateVars(Application.Count) As String
StateVars = Application.AllKeys
For I = 0 to StateVars.Length - 1
Message.Text = Message.Text + StateVars(I) + "<br/>"
Next I
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Clear() (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Clear()
Application("foo") = "Hello, "
Application("bar") = "World!"
Message.Text = "The Application collection contains " & _
Application.Count & " items. "
Dim I as Integer
For I = 0 To Application.Count - 1
Message.Text &= Application(I)
Next
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Count before and after variable setting (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Clear()
Application("foo") = "Hello, "
Application("bar") = "World!"
Message.Text = "The Application collection contains " & _
Application.Count & " items. "
Dim I as Integer
For I = 0 To Application.Count - 1
Message.Text &= Application(I)
Next
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Count (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Dim I as Integer
Dim StateVars(Application.Count) As String
StateVars = Application.AllKeys
For I = 0 to StateVars.Length - 1
Message.Text = Message.Text + StateVars(I) + "<br/>"
Next I
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application: GetKey (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.RemoveAll()
Application("GetKeyTest") = "Got it!"
Message.Text = "Key of Application(0) = " & Application.GetKey(0) & _
"<br/>(Should be GetKeyTest)"
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Item by index (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Clear()
Application.Item("foo") = "foo"
Application.Item("foo2") = "foo2"
Message.Text = Application.Item("foo") & "<br/>"
Message.Text &= Application.Item(1)
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Item by kay (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Clear()
Application.Item("foo") = "foo"
Application.Item("foo2") = "foo2"
Message.Text = Application.Item("foo") & "<br/>"
Message.Text &= Application.Item(1)
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application(I) (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Clear()
Application("foo") = "Hello, "
Application("bar") = "World!"
Message.Text = "The Application collection contains " & _
Application.Count & " items. "
Dim I as Integer
For I = 0 To Application.Count - 1
Message.Text &= Application(I)
Next
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Lock() and unLock() (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Lock()
Application("Counter") = Application("Counter") + 1
Application.UnLock()
Message.Text = "Counter = " & Application("ounter")
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application property example (VB.net)
<%@ Page Language="vb" Strict="True" %>
<html>
<head>
<title>Application property example</title>
<script runat="server">
Sub Page_Load()
Application("Name") = "John Doe"
Message.Text = "The value <em>" & CStr(Application("Name")) & _
"</em> has been added to the Application collection."
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.RemoveAll() (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.RemoveAll()
Application("GetKeyTest") = "Got it!"
Message.Text = "Key of Application(0) = " & Application.GetKey(0) & _
"<br/>(Should be GetKeyTest)"
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application Remove At (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
If Application.Count > 0 Then
Application.RemoveAt(0)
Message.Text = "The item at index 0 was removed."
Else
Message.Text = "The item at index 0 does not exist."
End If
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application.Remove (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
If Not Application("foo") Is Nothing Then
Application.Remove("foo")
Message.Text = "Item "foo" was removed."
Else
Message.Text = "Item "foo" does not exist."
End If
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application Set (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.RemoveAll()
Application.Set("TotallyNewVariable","Test!")
Message.Text = "First: " + Application("TotallyNewVariable") + "<br/>"
Application.Set("TotallyNewVariable","Test again!")
Message.Text = Message.Text & "First after Set: " + _
Application("TotallyNewVariable") + "<br/>"
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Application state
<%@ Page language="c#" src="ApplicationState.aspx.cs" AutoEventWireup="false" Inherits="WebForm1" %>
<html>
<body>
<form id="Form1" method="post" runat="server">
<h1>Application State page</h1>
</form>
</body>
</html>
<%-- ApplicationState.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Application.Lock();
if (Application[Request.Browser.Browser] != null)
Application[Request.Browser.Browser] = (int)Application[Request.Browser.Browser] + 1;
else
Application[Request.Browser.Browser] = 1;
Application.UnLock();
for (int i=0; i<Application.Count; i++)
Response.Output.Write("<p>{0} : {1} hits</p>", Application.GetKey(i), Application[i]);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
--%>
Application.StaticObjects (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Message.Text = "There are " & Application.StaticObjects.Count & _
" objects declared with the " & _
"<object runat="server"> syntax in Application scope."
Dim myobj As Object
For Each myObj in Application.StaticObjects
If myObj.Value.GetType.ToString() = _
"System.Web.UI.WebControls.TextBox" Then
Page.Controls.Add(myObj.Value)
End If
Next
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
For Each Key in Application.Keys (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Dim Key As String
Message.Text = "Application Keys:"
For Each Key in Application.Keys
Message.Text &= "<br/>Key: " & Key
Message.Text &= "<br/>Value: " & Application(Key)
Next
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Get variables from global.asax (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
lblAppName.Text = Application("ApplicationName")
lblAppName.ForeColor = System.Drawing.Color.FromName _
(Application("FontColor"))
lblMessage.ForeColor = System.Drawing.Color.FromName _
(Application("FontColor"))
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblMessage.Text = FormatCurrency(txtOrderTotal.Text * _
Application("ShippingCharge"))
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Coding the Application_OnStart Event</TITLE>
</HEAD>
<form runat="server">
<asp:label
id="lblAppName"
font-size="14"
runat="server"
/>
<BR><BR>
<asp:label
id="lblMessage"
runat="server"
text="Enter your order total to
see the shipping charge"
/>
<BR><BR>
<asp:textbox
id="txtOrderTotal"
columns="25"
maxlength="30"
runat=server
/>
<BR><BR>
<asp:button
id="butOK"
text=" OK "
onclick="SubmitBtn_Click"
runat="server"
/>
</Form>
</BODY>
</HTML>
<%--
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<SCRIPT LANGUAGE="VB" RUNAT="Server">
Sub Application_OnStart()
Application("TaxRate") = 0.5125
Application("appConn") = New OleDbConnection( _
"PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" & Server.MapPath _
("EmployeeDatabase.mdb;"))
Application("ApplicationName") = "My Site"
Application("FontColor") = "Red"
Application("ShippingCharge") = .03
End Sub
Sub Application_OnEnd()
" Dim MyLog as New EventLog
" MyLog.Log = "Application"
" MyLog.Source = "My Test ASP.NET Application"
" MyLog.WriteEntry("The application ended at " _
" & Now() & ".")
End Sub
Sub Session_OnStart()
Application("SessionStarts") = Application("SessionStarts") + 1
Session.TimeOut = 1
" Session("TheEventLog") = New EventLog
" Session("TheEventLog").Log = "Application"
" Application.Lock
Application("TotalUsers") = Application("TotalUsers") _
+ 1
Application.Unlock
"If Len(Session("WhenEntered")) = 0 Then
" Response.Redirect("welcome.aspx")
" Session("WhenEntered") = Now()
"End If
End Sub
Sub Session_OnEnd()
Dim MyLog as New EventLog
MyLog.Log = "Application"
MyLog.Source = "A session that started on " _
& Session("WhenEntered") & " ended at " _
& Now()
Application("SessionStops") = _
Application("SessionStops") + 1
End Sub
"Sub Application_Error(Sender as Object, e as EventArgs)
" Response.Redirect("errorshappen.aspx")
"End Sub
</SCRIPT>
--%>
<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>
Give a Application a new value (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application("GetTest") = "Got it!"
Message.Text = "GetTest = " & Application.Get("GetTest")
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Set Application key value pair (VB.net)
<%@ Page Language="VB" %>
<html>
<body>
<%
Application("CompanyTelephone") = "555 1234"
%>
<b>Application state changed successfully</b>
<b>Company Telephone = <% Response.Write(Application("CompanyTelephone")) %>
</body>
</html>
Store key value pair for entire application (C#)
<%@ Page Language="c#" %>
<html>
<body>
<% Application["CompanyTelephone"] = "555 1234"; %>
<b>Application state changed successfully</b>
</body>
</html>
//////////////////////////////////////////////////////////////////////
<html>
<body>
<b>Company Telephone = <% Response.Write(Application("CompanyTelephone")) %>
</b>
</body>
</html>