This tutorial shows how we can store small amounts of data in the Application and Session objects - like the number of users on the site. VB Version.
We're going to use the Application and Session objects to keep count of how many users are visiting our site.
First, we build our page which is going to use variables stored in the Application and Session objects. It might look something like this:
| <form id="form1" runat="server"> <div> <asp:Label ID="labelUserCount" runat="server"></asp:Label><br /> </div>Please enter your name: <asp:TextBox ID="textBoxName" runat="server"></asp:TextBox> <br /> <asp:Button ID="buttonSubmit1" runat="server" OnClick="buttonSubmit1_Click" Text="Button" /><br /> <asp:Label ID="labelOutput" runat="server"></asp:Label> </form> |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
The Global.asax is where we access the Application and Session objects to initialize our variables when they both start. The Application object exists as long as the website is online; the Session object exists as long as there is a user visiting the site - every unique visitor gets their own unique Session ID.Upon Application Start, we add a variable to the object, and set it to zero. Upon Session Start, we increment that variable by one. This will give us an idea of how many current visitors there are, because each user has their own Session. And finally, upon Session End, we decrement the same variable by one - indicating that a user left the site.
The Global.asax file should look something like:
| <%@ Application Language="vb" %> <script runat="server"> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application startup End SubApplication.Add("userCount", 0) Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application shutdown End SubSub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when an unhandled error occurs End SubSub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a new session is started End SubDim userCount As Integer = Integer.Parse(Application.Get("userCount").ToString()) userCount += 1 Application.Set("userCount", userCount) Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a session ends. End Sub' Note: The Session_End event is raised only when the sessionstate mode ' is set to InProc in the Web.config file. If session mode is set to StateServer ' or SQLServer, the event is not raised. Dim userCount As Integer = Integer.Parse(Application.Get("userCount").ToString()) userCount -= 1 Application.Set("userCount", userCount) </script> |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
The code-behind will look something like this:| Imports Microsoft.VisualBasic Imports System Imports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Partial Public Class _Default Inherits System.Web.UI.Page End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Page.SetFocus(textBoxName) End SublabelUserCount.Text = "Number of users visiting: " & Application.Get("userCount").ToString() Protected Sub buttonSubmit1_Click(ByVal sender As Object, ByVal e As EventArgs) labelOutput.Text = "Your name is: " & textBoxName.Text End SubtextBoxName.Text = "" |
No comments:
Post a Comment