ASP.NET Tutorial/Development/Application

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

Application.Add

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


Application.AllKeys

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

   <source lang="csharp">

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


Application.Count

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

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


Application.GetKey

   <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

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

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


Application.Lock() (VB)

   <source lang="csharp">

<%@ 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">
     The page has been requested
     <asp:Label ID="myLabel" runat="server" />
     times!
   </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</source>


Application.Remove

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

   <source lang="csharp">

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


Application.RemoveAt

   <source lang="csharp">

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

<body>

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

</body> </html></source>


Application.Set

   <source lang="csharp">

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

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

<body>

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

</body> </html></source>


Application.UnLock()

   <source lang="csharp">

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


Basic Application Events

   <source lang="csharp">

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.</source>


Displaying a count of user sessions.

   <source lang="csharp">

<%@ 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">
   Total Application Sessions:
   <asp:Label
       id="lblSessionCount"
       Runat="server" />
   </form>

</body> </html></source>


Lock the Application object

   <source lang="csharp">

<%@ 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">
     The page has been requested
     <asp:Label ID="myLabel" runat="server" />
     times!
   </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"]);
   }

}</source>


Save value to Application

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

  Sub Page_Load(Sender As Object, e As EventArgs)
     lblOutput.text = "Page loading" & _
        "Application started at: " & Application("Time") & "
" & _ "Current time: " & DateTime.Now & "
" 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></source>