Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you need to persist data in memory between page requests that's unique to each individual user and those pages aren't connected to each other through the web form system, that is, they aren't posting to each other, you can choose to use session variables. A session variable is stored in memory on the server but a copy of the session variable is stored for each individual user session and that session in turn is managed with cookies. A cookie is a bit of data that's created on the server and sent back to the browser along with the response to what an http request.
In the ASP.NET Framework and with many other application servers, session cookies are created automatically. Whenever a page is loaded, the server will detect that a new user session is starting and it creates a session cookie known as the User Session Token and sends that back to the browser with its first response. The browser then includes that cookie with each subsequent request, essentially reminding the server of which the user session is making the request. You can add arbitrary session variables. Each session variable can be of many types: strings, numbers or even complex arrays containing complete data sets.
In this video, I'm going to demonstrate how to create a session variable in response to a button click and then how to read that session variable in a totally separate page. I'll start at the file SessionVarCreate.aspx. In this page, there is currently a TextBox control with an ID of TextBox1 and a button control with the text label of Set Variable. I will double-click on the button and that takes me to the buttons, Button1_ Click method. This is the event handler function responding to the Click event on the server. Within the server environment, I'm going to create a variable in the session scope. There is an object named Session. It's a dynamic object, which means that you can add arbitrary property names. Within the function I'll say Session and then I'll put in a bracket and within the brackets, I'll add the ID or the key for the property I want to add. You can use any string value as the key for the property.
I will use a property of mySessionVar, then I'll close the expression with the closing bracket, then I'll put in the equal assignment operator and then I'll assign the value of the SessionVar to TextBox1.txt. The data typing of this value is a string because the text property of the TextBox returns a string. But again, you can put in many kinds of complex data into the Session object. Now, after I have stored the data in memory, I'll then redirect to the read page using Response.Redirect and I'll pass in the name of the page I want to go to. SessionVarRead.aspx. Because I'm using the Redirect command, I'm not posting to the other page and therefore, I would not be able to use the Viewstate object to pass the data, but when I set the value in the Session object, I'm storing the data in server memory and when the other page loads, it will send that cookie and it will have access to this data.
I will save that page and then go to the page SessionVarRead.aspx. In this file there is a button with a label of Read Variable and a label control with an ID of Output label. I'll double-click the Read Variable button and that takes me to the Click event handler for that button and here I'll retrieve the value of that session variable. Within the button Click event handler, I'll set the Output Label's Text property and then once again, I'll use the value Session["mySessionVar"].
There is one more bit of code you must include though. The text property of the Label object expects a string but there is no way for the ASP.NET Framework at this point to know that the value I stored in the session with the key of mySessionVar is a string. In C#, you explicitly datatype the returned value. Using this sort of code, place the cursor before the Session object and then to clear the datatype of the object, use the datatype within a pair of parenthesis. If you are working in Visual Basic, you would instead use the syntax as string after Session mySessionVar.
Now I'll save the changes and I'll run the page initially without coming from the first page. When the page loads in the browser, I'll click the button and you will see that the Label text property is left blank. That's because that variable is not in session or server memory yet, so I'll close the browser and I'll return to the Create Page and run that page in the browser pressing Ctrl+F5. I will type in a value into the TextBox and click the button Set Variable. That takes me to the Read page, then I'll click the Read Variable button and you will see that the value is retrieved and displayed.
Now, one of the questions that comes up about sessions is do they persist between browser sessions. That is to say if the user closes all of the browser windows and then opens the website again, is that session data still accessible to them? And the answer is No. I will go back to the Read page and load it into the browser again. Notice that I have closed all of my browser windows before this part of the test and I'll click the button and show that the data does not show up. Here is what's going on. Remember I said that cookies are used to link the web browser session to the data on the server. The types of cookies that are being used here are called memory or session cookies and the browser doesn't remember those cookies between its on-sessions. These kinds of cookies are not stored persistently on disk by the browser; they are just stored in memory.
So when the user closes all of the browser windows, its session cookies are lost and therefore, the data on the server is lost as well. So data that you placed into the session scope or into the Session object is remembered between page requests, but only for the duration of the current browser session. When the user closes all of their browser windows, that results in destroying the session cookies that are in browser memory and the next time the browser goes to the website, a new session is started automatically. Session variables again can store many different types of data: strings, numbers, arrays and many other complex objects. The most common use of sessions is to store things like shopping cards, user information after authenticating the user and so on and it's a valuable way to glue together the pages of a dynamic website built in ASP.NET.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98192 Viewers
61 Video lessons · 85490 Viewers
71 Video lessons · 69457 Viewers
56 Video lessons · 101801 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.