ASP.NET Tutorial/Development/Application

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

Application.Add

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


Application.AllKeys

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

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
     Application.Clear()
     Message.Text = "There are " & Application.Count & _
        " items in the Application collection."
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Application.Count

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

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


Application.GetKey

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

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

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


Application.Lock() (VB)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      The page has been requested
      <asp:Label ID="myLabel" runat="server" />
      times!
    </div>
    </form>
</body>
</html>
File: Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load
        If Application("PageCounter") >= 10 Then
            Application.Remove("PageCounter")
        End If
        If Application("PageCounter") Is Nothing Then
            Application("PageCounter") = 1
        Else
            Application.Lock()
            Application("PageCounter") += 1
            Application.UnLock()
        End If
        myLabel.Text = Application("PageCounter")
    End Sub
End Class


Application.Remove

<%@ 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.RemoveAll()

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
  Sub Page_Load()
     If Application.Count > 0 Then
        Application.RemoveAll()
        Message.Text = "Application collection cleared."
     Else
        Message.Text = "Application collection is already empty."
     End If
  End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Application.RemoveAt

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
  Sub Page_Load()
        Application.RemoveAll()
      Application.RemoveAt(1)
      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.Set

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
          Application.RemoveAll()
          Application.Set("TotallyNewVariable","Test!")
          myMessage.Text = "First: " + Application("TotallyNewVariable") + "<br/>"
          Application.Set("TotallyNewVariable","Test again!")
          myMessage.Text = myMessage.Text & "First after Set: " + _ 
        Application("TotallyNewVariable") + "<br/>"
         End Sub
      </script>
   </head>
<body>
   <asp:label id="myMessage" runat="server"/>
</body>
</html>


Application.UnLock()

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
  Sub Page_Load()
     Application.Lock()
     Application("Counter") = 1
     Application.UnLock()
     Message.Text = "Counter = " & Application("Counter") 
  End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Basic Application Events

Event-Handling Method             Description
Application_Start()               Occurs when the application starts
                                  which is the first time it receives a request from any user. 
Application_End()                 Occurs when the application is shutting down, generally because the web server is being restarted. 
Application_BeginRequest()        Occurs with each request the application receives, just before the page code is executed.
Application_EndRequest()          Occurs with each request the application receives, just after the page code is executed.
Session_Start()                   Occurs whenever a new user request is received and a session is started. 
Session_End()                     Occurs when a session times out or is programmatically ended.
Application_Error()               Occurs in response to an unhandled error.


Displaying a count of user sessions.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load()
    {
        lblSessionCount.Text = Application["SessionCount"].ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Session Count</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Total Application Sessions:
    <asp:Label
        id="lblSessionCount"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Lock the Application object

<%@ 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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      The page has been requested
      <asp:Label ID="myLabel" runat="server" />
      times!
    </div>
    </form>
</body>
</html>
File: Default.aspx.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.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["PageCounter"] != null &&
            (int)Application["PageCounter"] >= 10)
        {
            Application.Remove("PageCounter");
        }
        if (Application["PageCounter"] == null)
        {
            Application["PageCounter"] = 1;
        }
        else
        {
            Application.Lock();
            Application["PageCounter"] =
                (int)Application["PageCounter"] + 1;
            Application.UnLock();
        }
        myLabel.Text = Convert.ToString(Application["PageCounter"]);
    }
}


Save value to Application

<%@ Page Language="VB" %>
<script runat="server">
   Sub Page_Load(Sender As Object, e As EventArgs)
      lblOutput.text = "Page loading" & _
         "Application started at: " & Application("Time") & "<br>" & _
         "Current time: " & DateTime.Now & "<br>"
   End Sub
   Sub Click(obj As Object, E As EventArgs)
      Session.Abandon()
   End Sub
</script>
<html><body>
   <form runat="server">
      <asp:label id="lblOutput" runat="server"/>
      <asp:Button id="btSubmit" runat="server" OnClick="Click" Text="End This Session"/>
   </form>
</body></html>