Tuesday, May 20, 2014

Events

In a web application, events can occur at 3 levels
   1.   At the application level(application start)
   2.   At the page level (page load)
   3.   At the control level(Button click)

Techniques to send data from one web form to another
   1.   Query string
   2.   Cookies
   3.   Session state
   4.   Application state

  • Session state variables are available across all pages, but only for a given single session.
  • Session variable are like single-user global data. Only current session has access to its session state.
  • Application state variables are available across all pages and across all sessions.
  • Application state variables are like multi-user global data. All sessions can read and write application state
  • variables.
Here application to find the number of application running and the number of users in online :-
Global.asax.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.Security;  
 using System.Web.SessionState;  
 namespace video4_session  
 {  
   public class Global : System.Web.HttpApplication  
   {  
     void Application_Start(object sender, EventArgs e)  
     {  
       // Code that runs on application startup  
       Application["TotalApplications"] = 0;  
       Application["TotalUserSessions"]=0;  
       Application["TotalApplications"] = (int)Application["TotalApplications"]+1;  
     }  
     void Session_Start(object sender, EventArgs e)  
     {  
       // Code that runs when a new session is started  
       Application["TotalUserSessions"] = (int)Application["TotalUserSessions"] + 1;  
     }  
     void Session_End(object sender, EventArgs e)  
     {  
       // Code that runs when a session ends.   
       // 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.  
       Application["TotalUserSessions"] = (int)Application["TotalUserSessions"] - 1;  
     }  
   }  
 }  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace video4_session  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       Response.Write("Number of Applications : "+Application["TotalApplications"]);  
       Response.Write("<br/>");  
       Response.Write("Number of Total users online : " + Application["TotalUserSessions"]);  
     }  
   }  
 }  
How to get a new session-id and force the Session_Start() event to execute??????
1.   Close the existing browser window and then open new instance or the browser window.
2.   Open new instance of a different browser
3.   Use Cookie-less Sessions
< sessionState mode="InProc" cookieless="true" ></sessionState> in Web.config file

No comments:

Post a Comment