Lab 14 Manual 05012023 091712pm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab 14

State Management

Techniques to send data from one webform to another


1- Query Strings
2- Sessions
3- Application State

View State Limitations


The View State variables are used to preserve data across page post back,
but the main limitation of View State is that View State variable of one
webform is not available in another webform. To make the variables of one
web form available in another web form we can use Session State or
Application State variables.

Session state variables


• These variables are available across all pages, but only for a given
single session.
• Only the current session has access to its Session state.
• Session variables are available across all web pages, but for a specific
session id.
• Session state variables are stored on web server.
Example 1:
1- Create a web form with name “SessionState1” with one text item and one
button.

Figure 1 Session State 1 Web form


Figure 2 Session State 2 Web form

2- Create another webform with name “SessionState2” and copy the code from
SessionState1 and paste it into this application.
3- Run the application (SessionState1) in Google Chrome using Ctrl+F5 and
increment the value of text item by clicking the button.
4- Create another instance of web application by opening SessionState2 in Web browser.
5- You will see that same value has been passed in that window.
6- Now close all chrome tabs. Your session will be expired.

To set the time of a session


• Configure Web.config file by writing following code inside
<system.web> tag.
• The session will expire after 1 minute. By default, session expires after
20 minutes.

<sessionState mode="InProc" timeout="1"></sessionState>

Application state variables


• These variables are available across all pages and across all sessions.
• All sessions can read or write Application state variables.
• They are also stored on the web server.

Task 1: Modify above example by creating Application State Variable.

Application Level Events


Global.asax file in ASP.NET contains application level events. Application events
are used to initialize variables that should be available in all current sessions of
an application. Session events are used to initialize variables that needs to be
available only for a given individual session, but not between multiple sessions.

Example 2
Global.asax file
Create a new application and configure the Global.asax file of your application
according to the given code.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["totalApps"] = 0;
Application["totalSessions"] = 0;
Application["totalApps"] = (int)Application["totalApps"] + 1;
}

void Application_End(object sender, EventArgs e)


{
// Code that runs on application shutdown

void Application_Error(object sender, EventArgs e)


{
// Code that runs when an unhandled error occurs

void Session_Start(object sender, EventArgs e)


{
Application["totalSessions"] = (int)Application["totalSessions"] + 1;
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
Application["totalSessions"] = (int)Application["totalSessions"] - 1;
}

About.aspx.cs file

protected void Page_Load(object sender, EventArgs e)


{
Response.Write("Total Applications" + Application["totalApps"] +
"<br/>");
Response.Write("Number of online users " +
Application["totalSessions"]);
}

You might also like