19.3 Collections Reference
Returns a reference to the current HttpSessionState instance.
The example below calls the RemoveAll method through the Contents collection reference and then writes a message:
Sub Page_Load( ) Session.Contents.RemoveAll( ) Message.Text = "Removed all items from current Session." End Sub
This property is provided for backward compatibility with classic ASP. Properties such as the Item property and methods such as Remove and RemoveAll were accessed via the Contents property in classic ASP. In new ASP.NET development, you should access these members directly.
Returns a NameObjectCollectionBase.KeysCollection containing the string keys associated with all of the values stored in the Session collection.
The example loops through the collection of keys in the Session collection and then displays the key name and the value associated with it by using the Text property of the Message label control:
Sub Page_Load( ) Dim Key As String Message.Text = "Session Keys:" For Each Key in Session.Keys Message.Text &= "<br/>Key: " & Key Message.Text &= "<br/>Value: " & _ CStr(Session(Key)) Next End Sub
The Keys property provides one of many ways to iterate over the contents of the Session collection.
Returns an HttpStaticObjectsCollection containing all objects instantiated in global.asax by using the <object runat="server"> syntax whose scope is set to Session.
The example uses the Count property of the HttpStaticObjectsCollection class to display the number of objects in the current application declared with the <object scope="session" runat="server"/> syntax in global.asax. It then checks the type of each object and, if it is a TextBox web control, adds it to the Controls collection of the form:
Sub Page_Load( ) Message.Text = "There are " & Session.StaticObjects.Count & _ " objects declared with the " & _ "<object runat="server"> syntax in Session scope." Dim myobj As Object For Each myObj in Session.StaticObjects If myObj.Value.GetType.ToString( ) = _ "System.Web.UI.WebControls.TextBox" Then myForm.Controls.Add(myObj.Value) End If Next End Sub
You also need to modify the <body> section of the document as follows:
<body> <form id="myForm" runat="server"> <asp:label id="Message" runat="server"/> </form> </body>
This property is provided for backward compatibility with classic ASP. You should think carefully before instantiating objects with Session or Application scope because of the impact such objects have on resource usage and application scalability. In most cases, it is advisable to limit objects to page scope.
Each object in the collection is represented by the DictionaryEntry structure, so its key and value are not directly accessible. To access the key and/or value, use the Key and/or Value members of the DictionaryEntry structure, as shown in the example.