ASP.Net/Session Cookie/Application

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add new name value pair to the Application (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


All application variables (VB.net)

   <source lang="csharp">

<%@ 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:
" For I = 0 to Application.Count - 1 lblMessage.Text = lblMessage.Text _ & Application.GetKey(I) & ": " _ & Application(I) & "
" 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>

      </source>
   
  


Application.AllKeys (VB.net)

   <source lang="csharp">

<%@ 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) + "
" Next I End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application.Clear() (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Application.Count before and after variable setting (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Application.Count (VB.net)

   <source lang="csharp">

<%@ 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) + "
" Next I End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application: GetKey (VB.net)

   <source lang="csharp">

<%@ 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) & _
   "
(Should be GetKeyTest)" End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application.Item by index (VB.net)

   <source lang="csharp">

<%@ 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") & "
" Message.Text &= Application.Item(1) End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application.Item by kay (VB.net)

   <source lang="csharp">

<%@ 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") & "
" Message.Text &= Application.Item(1) End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application(I) (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Application.Lock() and unLock() (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Application property example (VB.net)

   <source lang="csharp">

<%@ 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 " & CStr(Application("Name")) & _
              " has been added to the Application collection."
        End Sub
     </script>
  </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application.RemoveAll() (VB.net)

   <source lang="csharp">

<%@ 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) & _
   "
(Should be GetKeyTest)" End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application Remove At (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Application.Remove (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Application Set (VB.net)

   <source lang="csharp">

<%@ Page Language="vb" %> <html>

  <head>
     <script runat="server">
        Sub Page_Load()
          Application.RemoveAll()
        Application.Set("TotallyNewVariable","Test!")
        Message.Text = "First: " + Application("TotallyNewVariable") + "
" Application.Set("TotallyNewVariable","Test again!") Message.Text = Message.Text & "First after Set: " + _ Application("TotallyNewVariable") + "
" End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Application state

   <source lang="csharp">

<%@ Page language="c#" src="ApplicationState.aspx.cs" AutoEventWireup="false" Inherits="WebForm1" %> <html>

 <body>
 
   <form id="Form1" method="post" runat="server">

Application State page

   </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("

{0} : {1} hits

", 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
 }

--%>

      </source>
   
  


Application.StaticObjects (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


For Each Key in Application.Keys (VB.net)

   <source lang="csharp">

<%@ 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 &= "
Key:   " & Key Message.Text &= "
Value:   " & Application(Key) Next End Sub </script> </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Get variables from global.asax (VB.net)

   <source lang="csharp">

<%@ 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"

/>

<asp:label

   id="lblMessage"
   runat="server"
   text="Enter your order total to 
       see the shipping charge"

/>

<asp:textbox

   id="txtOrderTotal" 
   columns="25"
   maxlength="30"
   runat=server 

/>

<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> --%>

      </source>
   
  

<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>


Give a Application a new value (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Set Application key value pair (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <html>

 <body>
   <%
   Application("CompanyTelephone") = "555 1234"
   %>
   Application state changed successfully
   Company Telephone = <% Response.Write(Application("CompanyTelephone")) %>
 </body>

</html>

      </source>
   
  


Store key value pair for entire application (C#)

   <source lang="csharp">

<%@ Page Language="c#" %> <html>

 <body>
   <% Application["CompanyTelephone"] = "555 1234"; %>
   <b>Application state changed successfully
 </body>

</html>

//////////////////////////////////////////////////////////////////////

<html>

 <body>
   Company Telephone = <% Response.Write(Application("CompanyTelephone")) %>
   
 </body>

</html>


      </source>