..
The development and deployment of web applications requires monitoring of the state of the same at all times. One of the most important states is associated with the session (session state) and ASP.NET provides a great support for its management.
At this point the guide should be clear that in programming web applications developed serve multiple users, distributed across a large area, based on a disconnected protocol (HTTP).
When the session state is enabled for each request, ASP.NET creates a new Session object, which becomes part of the context and is accessible through the page. In this object is assigned an identifier and it becomes a handy container of information whose duration is greater than the page it refers to.
The Session object is a dictionary of name / value pairs, and through it you can bind any object to a key so you can access it when necessary, using its own key.
For example, if we want to store some information about a user in a session object should write something like
StoreInfoInSession void ()
{
String = TextBox1.Text strInputUtente;
Session ["strInputUtente"] = strInputUtente;
}
and a subsequent request if we want to retrieve this value we should write
GetInfoFromSession void ()
{
StrInputUtente String = Session ["strInputUtente"];
TextBox1.Text = strInputUtente;
}
Managing Session State in ASP.NET is extremely convenient, especially because this technology in this type of object can be placed in different places: in the current process, was separated on a server, a SQL Server database.
To better understand how the session was an example where we see we will create a website with a page that stores a value as part of the session state. This makes it clear the difference between the state of a page during a request and the information associated with a session that, as mentioned, persist even after the request.
We create a new web application and web form Default.aspx insert a text box to enter the value to be stored in session state. We also add two buttons, one to store data and to display a status

Also insert text box next to a label to display the data to call you back later. Insert a variable of type string in our page and in the Page_Load set the value of this string in our text box
sessionString string;
protected void Page_Load (object sender, EventArgs e)
{
this.Label1.Text = this.sessionString;
}
Double-clicking on the button Click event Stores generate the corresponding inside and insert the code that retrieves the string contained in the text box and stores it in variable sessionString, setting the label side with the same text
MemorizzaBtn_Click protected void (object sender, EventArgs e)
{
this.sessionString = this.TextBox1.Text;
this.Label1.Text = this.sessionString;
}
We start the application and write something in the text box and click on Save.
We should see something like

If we click the View button the label takes on a value of empty string as the variable is instantiated sessionString again (because the page is recreated). This is because we set it in Page_Load the Text property of the label is set to the value of the variable sessinString. As we have mentioned several times in fact, the pages are very short-lived objects, the lifetime of a request and then are destroyed by all the data in them. So if the goal is to store some data that goes beyond the duration of a page as soon as the approach is not good.
The use of session state is the way to solve this problem. To better explain the concept we add a second label to our web form and set it as that already present

This second label will be useful to display data retrieved through the Session object. MemorizzaBtn_Click modify the event so that the text taken from the text box is also stored in the Session
MemorizzaBtn_Click protected void (object sender, EventArgs e)
{
/ / Store the value in local variable
this.sessionString = this.TextBox1.Text;
/ / Store the value in the Session
this.Session ["sessionString"] = this.TextBox1.Text;
/ / Show the value of the variable
this.Label1.Text = this.sessionString;
/ / Show the value of the session
this.Label2.Text = (string) this.Session ["sessionString"];
}
Also modify the Page_Load event so that the second set the label string retrieved from the Session
protected void Page_Load (object sender, EventArgs e)
{
this.Label1.Text = this.sessionString;
this.Label2.Text = (string) this.Session ["sessionString"];
}
We start the application now and write something again in the text box, then clicking on the button Save. We should see something like

If you now click on Show will see that the first parameter will be set to empty string, while the latter will maintain the value

While the value of the local variable is lost, because the page is recreated, the value stored in the Session object persists and can be consulted.
With regard to the configuration of various parameters for the management of the session I invite you to consult the official Microsoft documentation.
| |
ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €. |
| |
ASP.NET (Course)
Full course for building Web applications from 49 €. |
| |
SQL and Database (Course)
Create and manage relational databases. From 39 €. |