Sponsored Links
ASP.NET Tutorials
- Securing ASP.NET Applications with C#
- Configuring and Deploying ASP.NET Applications
- Caching in ASP.NET
- Managing State with ASP.NET and C#
- ASP .NET Migration and Interoperability
- Creating and consuming XML Web Services with C#
- Managing Data with ADO.NET DataSets and C#
- ASP.NET Using the DataList and Repeater, Datagrid Controls
- Accessing Data with C#
- Using Rich Server Controls with C#
- ASP .NET: Validating User Input with C#
- ASP.NET Web Forms Controls
- Introduction to ASP.NET with C#
- ASP.NET with C# Training Course Outline
- ASP.NET with C# Training Launch
Tutorials
ASP.NETManaging State with ASP.NET and C#
Table of Contents
Managing State with ASP.NET and C#
Managing State with ASP.NET and C# - Page 2
Managing State with ASP.NET and C# - Page 3Managing State with ASP.NET and C#
Preserving State in Web Applications
There are number of ways that you can use to preserve the State of the Web Application. Here are some of the possible ways listed.
1) Using Cookies
2) Using Session States
3) Using Application States
4) Using HttpContext collection
5) Using ViewState
These are only some ways of storing the state of the user as well as the application. In this article we will see few of them in detail.
Using Cookies to maintain the state of the application
Cookies are created on the server side but saved on the client side. In the button click event of 'Cookies', write this code:
HttpCookie cName = new HttpCookie("Name");
cName.Value = txtName.Text;
Response.Cookies.Add(cName);
Response.Redirect("WebForm5.aspx");
First, we create a cookie named "cName". Since one cookie instance can hold many values, we tell the compiler that this cookie will hold "Name" value. We assign to it the value of the TextBox and finally add it in the Response stream, and sent it to the other page using Response.Redirect method.
Cookies can also have expiration time which means they can expire at some specified time or you can also set the sliding time which means that will expire is that cookies is not being used.
Using Cookieless Sessions in Asp.net
For all those users who have configured not to accept cookies they can use cookie less sessions. Here is a simple syntax of using the cookieless sessions.
sessionState
mode="Off"
stateConnectionString=
"tcpip=127.0.0.1:42424"
sqlConnectionString="data source=(local);
Integrated Security=SSPI"
cookieless="true"
timeout="20"
The only downside of using the cookieless sessions is that you cannot use the absolute urls and must code each link manually.
Using Session Variables to store the state of the application
Sessions are created as soon as the first response is being sent from the client to the server, and session ends when the user closes his browser window or some abnormal operation takes place. Here is how you can use session variables for transferring values. Below you can see a Session is created for the user and "Name" is the key, also known as the Session key, which is assigned the TextBox value.
// Session Created
Session["Name"] = txtName.Text;
Response.Redirect("WebForm5.aspx");
// The code below shows how to get the session value.
// This code must be placed in other page.
if(Session["Name"] != null)
Label3.Text = Session["Name"].ToString();
We can also store collections in the Session variables. These collections can be ArrayList or HashTables. Later you can use the casting to retrieve those collections.
Next Page: Managing State with ASP.NET and C# - Page 2
Comments
vishal soni said:
| This site is fantastic for beginners and is very helpful |
Salim Ahamed said:
| How I can use session for Login page in asp.net and how I can sign out through session. Please help me. |
Sponsored Links
